Now move file size

This commit is contained in:
Benjamin Sago 2017-05-20 21:43:04 +01:00
parent 3f8b547f2d
commit fda88bedc2
3 changed files with 66 additions and 57 deletions

View File

@ -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!

View File

@ -19,3 +19,4 @@ mod escape;
mod users;
mod groups;
mod permissions;
mod size;

63
src/output/size.rs Normal file
View File

@ -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(),
}
}