Sfoglia il codice sorgente

refactor: rendering Blocksize like file sizes

Revise rendering of Blocksize to appear like file sizes
Sandro-Alessio Gierens 2 anni fa
parent
commit
dcdb7115c6
2 ha cambiato i file con 53 aggiunte e 9 eliminazioni
  1. 52 8
      src/output/render/blocks.rs
  2. 1 1
      src/output/table.rs

+ 52 - 8
src/output/render/blocks.rs

@@ -1,14 +1,58 @@
 use ansi_term::Style;
+use locale::Numeric as NumericLocale;
+use number_prefix::Prefix;
 
 use crate::fs::fields as f;
-use crate::output::cell::TextCell;
-
-
-impl f::Blocks {
-    pub fn render<C: Colours>(&self, colours: &C) -> TextCell {
-        match self {
-            Self::Some(blk)  => TextCell::paint(colours.block_count(), blk.to_string()),
-            Self::None       => TextCell::blank(colours.no_blocks()),
+use crate::output::cell::{TextCell, DisplayWidth};
+use crate::output::table::SizeFormat;
+
+
+impl f::Blocksize {
+    pub fn render<C: Colours>(self, colours: &C, size_format: SizeFormat, numerics: &NumericLocale) -> TextCell {
+        use number_prefix::NumberPrefix;
+
+        let size = match self {
+            Self::Some(s)             => s,
+            Self::None                => return TextCell::blank(colours.no_blocksize()),
+        };
+
+        let result = match size_format {
+            SizeFormat::DecimalBytes  => NumberPrefix::decimal(size as f64),
+            SizeFormat::BinaryBytes   => NumberPrefix::binary(size as f64),
+            SizeFormat::JustBytes     => {
+
+                // Use the binary prefix to select a style.
+                let prefix = match NumberPrefix::binary(size as f64) {
+                    NumberPrefix::Standalone(_)   => None,
+                    NumberPrefix::Prefixed(p, _)  => Some(p),
+                };
+
+                // But format the number directly using the locale.
+                let string = numerics.format_int(size);
+
+                return TextCell::paint(colours.blocksize(prefix), string);
+            }
+        };
+
+        let (prefix, n) = match result {
+            NumberPrefix::Standalone(b)   => return TextCell::paint(colours.blocksize(None), numerics.format_int(b)),
+            NumberPrefix::Prefixed(p, n)  => (p, n),
+        };
+
+        let symbol = prefix.symbol();
+        let number = if n < 10_f64 {
+            numerics.format_float(n, 1)
+        } else {
+            numerics.format_int(n.round() as isize)
+        };
+
+        TextCell {
+            // symbol is guaranteed to be ASCII since unit prefixes are hardcoded.
+            width: DisplayWidth::from(&*number) + symbol.len(),
+            contents: vec![
+                colours.blocksize(Some(prefix)).paint(number),
+                colours.unit(Some(prefix)).paint(symbol),
+            ].into(),
         }
     }
 }

+ 1 - 1
src/output/table.rs

@@ -516,7 +516,7 @@ impl<'a> Table<'a> {
             }
             #[cfg(unix)]
             Column::Blocks => {
-                file.blocks().render(self.theme)
+                file.blocksize().render(self.theme, self.size_format, &self.env.numeric)
             }
             #[cfg(unix)]
             Column::User => {