From fda88bedc2437b09f8ffbbece613a0e78d0fd9b0 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sat, 20 May 2017 21:43:04 +0100 Subject: [PATCH] Now move file size --- src/output/details.rs | 59 ++-------------------------------------- src/output/mod.rs | 1 + src/output/size.rs | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 src/output/size.rs diff --git a/src/output/details.rs b/src/output/details.rs index 9f7e31b..3efab59 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -97,7 +97,7 @@ use fs::{Dir, File, fields as f}; use fs::feature::xattr::{Attribute, FileAttributes}; use options::{FileFilter, RecurseOptions}; use output::colours::Colours; -use output::column::{Alignment, Column, Columns, SizeFormat}; +use output::column::{Alignment, Column, Columns}; use output::cell::{TextCell, TextCellContents, DisplayWidth}; use output::tree::TreeTrunk; use output::file_name::{FileName, LinkStyle, Classify}; @@ -497,7 +497,7 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { match *column { Column::Permissions => file.permissions().render(&self.opts.colours, file.type_char(), xattrs), - Column::FileSize(fmt) => self.render_size(file.size(), fmt), + Column::FileSize(fmt) => file.size().render(&self.opts.colours, fmt, &self.env.numeric), Column::Timestamp(Modified) => self.render_time(file.modified_time()), Column::Timestamp(Created) => self.render_time(file.created_time()), Column::Timestamp(Accessed) => self.render_time(file.accessed_time()), @@ -528,61 +528,6 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { TextCell::paint(self.opts.colours.inode, inode.0.to_string()) } - fn render_size(&self, size: f::Size, size_format: SizeFormat) -> TextCell { - use number_prefix::{binary_prefix, decimal_prefix}; - use number_prefix::{Prefixed, Standalone, PrefixNames}; - - let size = match size { - f::Size::Some(s) => s, - f::Size::None => return TextCell::blank(self.opts.colours.punctuation), - f::Size::DeviceIDs { major, minor } => return self.render_device_ids(major, minor), - }; - - let result = match size_format { - SizeFormat::DecimalBytes => decimal_prefix(size as f64), - SizeFormat::BinaryBytes => binary_prefix(size as f64), - SizeFormat::JustBytes => { - let string = self.env.numeric.format_int(size); - return TextCell::paint(self.opts.colours.file_size(size), string); - }, - }; - - let (prefix, n) = match result { - Standalone(b) => return TextCell::paint(self.opts.colours.file_size(b as u64), b.to_string()), - Prefixed(p, n) => (p, n) - }; - - let symbol = prefix.symbol(); - let number = if n < 10f64 { self.env.numeric.format_float(n, 1) } - else { self.env.numeric.format_int(n as isize) }; - - // The numbers and symbols are guaranteed to be written in ASCII, so - // we can skip the display width calculation. - let width = DisplayWidth::from(number.len() + symbol.len()); - - TextCell { - width: width, - contents: vec![ - self.opts.colours.file_size(size).paint(number), - self.opts.colours.size.unit.paint(symbol), - ].into(), - } - } - - fn render_device_ids(&self, major: u8, minor: u8) -> TextCell { - let major = major.to_string(); - let minor = minor.to_string(); - - TextCell { - width: DisplayWidth::from(major.len() + 1 + minor.len()), - contents: vec![ - self.opts.colours.size.major.paint(major), - self.opts.colours.punctuation.paint(","), - self.opts.colours.size.minor.paint(minor), - ].into(), - } - } - #[allow(trivial_numeric_casts)] fn render_time(&self, timestamp: f::Time) -> TextCell { // TODO(ogham): This method needs some serious de-duping! diff --git a/src/output/mod.rs b/src/output/mod.rs index fd1b1c2..0c3a843 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -19,3 +19,4 @@ mod escape; mod users; mod groups; mod permissions; +mod size; diff --git a/src/output/size.rs b/src/output/size.rs new file mode 100644 index 0000000..15dda16 --- /dev/null +++ b/src/output/size.rs @@ -0,0 +1,63 @@ +use fs::fields as f; +use output::column::SizeFormat; +use output::cell::{TextCell, DisplayWidth}; +use output::colours::Colours; +use locale; + + +impl f::Size { + pub fn render(&self, colours: &Colours, size_format: SizeFormat, numerics: &locale::Numeric) -> TextCell { + use number_prefix::{binary_prefix, decimal_prefix}; + use number_prefix::{Prefixed, Standalone, PrefixNames}; + + let size = match *self { + f::Size::Some(s) => s, + f::Size::None => return TextCell::blank(colours.punctuation), + f::Size::DeviceIDs { major, minor } => return render_device_ids(colours, major, minor), + }; + + let result = match size_format { + SizeFormat::DecimalBytes => decimal_prefix(size as f64), + SizeFormat::BinaryBytes => binary_prefix(size as f64), + SizeFormat::JustBytes => { + let string = numerics.format_int(size); + return TextCell::paint(colours.file_size(size), string); + }, + }; + + let (prefix, n) = match result { + Standalone(b) => return TextCell::paint(colours.file_size(b as u64), b.to_string()), + Prefixed(p, n) => (p, n) + }; + + let symbol = prefix.symbol(); + let number = if n < 10f64 { numerics.format_float(n, 1) } + else { numerics.format_int(n as isize) }; + + // The numbers and symbols are guaranteed to be written in ASCII, so + // we can skip the display width calculation. + let width = DisplayWidth::from(number.len() + symbol.len()); + + TextCell { + width: width, + contents: vec![ + colours.file_size(size).paint(number), + colours.size.unit.paint(symbol), + ].into(), + } + } +} + +fn render_device_ids(colours: &Colours, major: u8, minor: u8) -> TextCell { + let major = major.to_string(); + let minor = minor.to_string(); + + TextCell { + width: DisplayWidth::from(major.len() + 1 + minor.len()), + contents: vec![ + colours.size.major.paint(major), + colours.punctuation.paint(","), + colours.size.minor.paint(minor), + ].into(), + } +}