Repository: lsd-rs/lsd (language: rust)

Issue:
FR: show fraction in file sizes only for values less than 100
From a certain size on the fraction is not that important anymore when viewed in `default` mode (which is the equivalent to ls' `human-readable`).

So maybe values less than `100` should be displayed with a fraction, equal/larger values as integer.

But those sizes should be displayed always as a number with fraction:
```
.rw-r--r-- me user 2020-04-04 12:36:29 5.9 MB  IMG_20200308_162055.jpg
.rw-r--r-- me user 2020-04-04 12:36:33   6 MB  IMG_20200308_162058.jpg
```
-> better to see `6.0 MB` instead of integer value.



Discussion / hints:
I would be happy to implement this if nobody else has picked it up yet.
Nobody has picked it up yet. Feel free to take it up :)

Fix patch:
diff --git a/src/meta/size.rs b/src/meta/size.rs
index f026fd5d9..f9090b808 100644
--- a/src/meta/size.rs
+++ b/src/meta/size.rs
@@ -34,6 +34,10 @@ impl Size {
         self.bytes
     }
 
+    fn format_size(&self, number: f64) -> String {
+        format!("{0:.1$}", number, if number < 10.0 { 1 } else { 0 })
+    }
+
     pub fn get_unit(&self, flags: &Flags) -> Unit {
         if self.bytes < 1024 || flags.size == SizeFlag::Bytes {
             Unit::Byte
@@ -93,17 +97,16 @@ impl Size {
         match unit {
             Unit::None => "".to_string(),
             Unit::Byte => self.bytes.to_string(),
-            Unit::Kilo => (((self.bytes as f64) / 1024.0 * 10.0).round() / 10.0).to_string(),
+            Unit::Kilo => self.format_size(((self.bytes as f64) / 1024.0 * 10.0).round() / 10.0),
             Unit::Mega => {
-                (((self.bytes as f64) / (1024.0 * 1024.0) * 10.0).round() / 10.0).to_string()
-            }
-            Unit::Giga => (((self.bytes as f64) / (1024.0 * 1024.0 * 1024.0) * 10.0).round()
-                / 10.0)
-                .to_string(),
-            Unit::Tera => {
-                (((self.bytes as f64) / (1024.0 * 1024.0 * 1024.0 * 1024.0) * 10.0).round() / 10.0)
-                    .to_string()
+                self.format_size(((self.bytes as f64) / (1024.0 * 1024.0) * 10.0).round() / 10.0)
             }
+            Unit::Giga => self.format_size(
+                ((self.bytes as f64) / (1024.0 * 1024.0 * 1024.0) * 10.0).round() / 10.0,
+            ),
+            Unit::Tera => self.format_size(
+                ((self.bytes as f64) / (1024.0 * 1024.0 * 1024.0 * 1024.0) * 10.0).round() / 10.0,
+            ),
         }
     }
 
@@ -158,6 +161,17 @@ mod test {
         assert_eq!(size.unit_string(&flags).as_str(), "");
     }
 
+    #[test]
+    fn render_10_minus_kilobyte() {
+        let size = Size::new(4 * 1024); // 4 kilobytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "4.0");
+        assert_eq!(size.unit_string(&flags).as_str(), "KB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "K");
+    }
+
     #[test]
     fn render_kilobyte() {
         let size = Size::new(42 * 1024); // 42 kilobytes
@@ -169,6 +183,28 @@ mod test {
         assert_eq!(size.unit_string(&flags).as_str(), "K");
     }
 
+    #[test]
+    fn render_100_plus_kilobyte() {
+        let size = Size::new(420 * 1024 + 420); // 420.4 kilobytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "420");
+        assert_eq!(size.unit_string(&flags).as_str(), "KB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "K");
+    }
+
+    #[test]
+    fn render_10_minus_megabyte() {
+        let size = Size::new(4 * 1024 * 1024); // 4 megabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "4.0");
+        assert_eq!(size.unit_string(&flags).as_str(), "MB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "M");
+    }
+
     #[test]
     fn render_megabyte() {
         let size = Size::new(42 * 1024 * 1024); // 42 megabytes
@@ -180,6 +216,28 @@ mod test {
         assert_eq!(size.unit_string(&flags).as_str(), "M");
     }
 
+    #[test]
+    fn render_100_plus_megabyte() {
+        let size = Size::new(420 * 1024 * 1024 + 420 * 1024); // 420.4 megabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "420");
+        assert_eq!(size.unit_string(&flags).as_str(), "MB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "M");
+    }
+
+    #[test]
+    fn render_10_minus_gigabyte() {
+        let size = Size::new(4 * 1024 * 1024 * 1024); // 4 gigabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "4.0");
+        assert_eq!(size.unit_string(&flags).as_str(), "GB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "G");
+    }
+
     #[test]
     fn render_gigabyte() {
         let size = Size::new(42 * 1024 * 1024 * 1024); // 42 gigabytes
@@ -191,6 +249,28 @@ mod test {
         assert_eq!(size.unit_string(&flags).as_str(), "G");
     }
 
+    #[test]
+    fn render_100_plus_gigabyte() {
+        let size = Size::new(420 * 1024 * 1024 * 1024 + 420 * 1024 * 1024); // 420.4 gigabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "420");
+        assert_eq!(size.unit_string(&flags).as_str(), "GB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "G");
+    }
+
+    #[test]
+    fn render_10_minus_terabyte() {
+        let size = Size::new(4 * 1024 * 1024 * 1024 * 1024); // 4 terabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "4.0");
+        assert_eq!(size.unit_string(&flags).as_str(), "TB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "T");
+    }
+
     #[test]
     fn render_terabyte() {
         let size = Size::new(42 * 1024 * 1024 * 1024 * 1024); // 42 terabytes
@@ -202,12 +282,23 @@ mod test {
         assert_eq!(size.unit_string(&flags).as_str(), "T");
     }
 
+    #[test]
+    fn render_100_plus_terabyte() {
+        let size = Size::new(420 * 1024 * 1024 * 1024 * 1024 + 420 * 1024 * 1024 * 1024); // 420.4 terabytes
+        let mut flags = Flags::default();
+
+        assert_eq!(size.value_string(&flags).as_str(), "420");
+        assert_eq!(size.unit_string(&flags).as_str(), "TB");
+        flags.size = SizeFlag::Short;
+        assert_eq!(size.unit_string(&flags).as_str(), "T");
+    }
+
     #[test]
     fn render_with_a_fraction() {
         let size = Size::new(42 * 1024 + 103); // 42.1 kilobytes
         let flags = Flags::default();
 
-        assert_eq!(size.value_string(&flags).as_str(), "42.1");
+        assert_eq!(size.value_string(&flags).as_str(), "42");
         assert_eq!(size.unit_string(&flags).as_str(), "KB");
     }
 


Test patch: