From 24628f97becbcff534ac93a17f378cafd48fd79f Mon Sep 17 00:00:00 2001 From: Ben S Date: Thu, 18 Dec 2014 07:00:31 +0000 Subject: [PATCH] Use number_prefix crate for number prefixes --- Cargo.lock | 6 ++++ Cargo.toml | 9 ++++-- src/column.rs | 9 +++++- src/exa.rs | 4 +-- src/file.rs | 27 ++++++++++------- src/format.rs | 80 -------------------------------------------------- src/options.rs | 9 ++++-- 7 files changed, 45 insertions(+), 99 deletions(-) delete mode 100644 src/format.rs diff --git a/Cargo.lock b/Cargo.lock index d74cdd6..b731939 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "ansi_term 0.4.0 (git+https://github.com/ogham/rust-ansi-term.git)", "natord 1.0.0 (git+https://github.com/lifthrasiir/rust-natord.git)", + "number_prefix 0.2.0 (git+https://github.com/ogham/rust-number-prefix.git)", "users 0.1.0 (git+https://github.com/ogham/rust-users.git)", ] @@ -17,6 +18,11 @@ name = "natord" version = "1.0.0" source = "git+https://github.com/lifthrasiir/rust-natord.git#83ebf6e7999fe2646bca45d5f6800728a0bbd5c5" +[[package]] +name = "number_prefix" +version = "0.2.0" +source = "git+https://github.com/ogham/rust-number-prefix.git#3f690804a3f1704ee92901561189514d0ea0a6ab" + [[package]] name = "users" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0cd0744..f790c44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,11 @@ name = "exa" [dependencies.ansi_term] git = "https://github.com/ogham/rust-ansi-term.git" +[dependencies.natord] +git = "https://github.com/lifthrasiir/rust-natord.git" + +[dependencies.number_prefix] +git = "https://github.com/ogham/rust-number-prefix.git" + [dependencies.users] git = "https://github.com/ogham/rust-users.git" - -[dependencies.natord] -git = "https://github.com/lifthrasiir/rust-natord.git" \ No newline at end of file diff --git a/src/column.rs b/src/column.rs index 06394ea..0731494 100644 --- a/src/column.rs +++ b/src/column.rs @@ -1,7 +1,7 @@ pub enum Column { Permissions, FileName, - FileSize(bool), + FileSize(SizeFormat), Blocks, User, Group, @@ -11,6 +11,13 @@ pub enum Column { impl Copy for Column { } +pub enum SizeFormat { + DecimalBytes, + BinaryBytes, +} + +impl Copy for SizeFormat { } + // Each column can pick its own alignment. Usually, numbers are // right-aligned, and text is left-aligned. diff --git a/src/exa.rs b/src/exa.rs index 9c8b302..5690298 100644 --- a/src/exa.rs +++ b/src/exa.rs @@ -2,8 +2,9 @@ extern crate regex; #[phase(plugin)] extern crate regex_macros; extern crate ansi_term; -extern crate users; +extern crate number_prefix; extern crate unicode; +extern crate users; use std::io::FileType; use std::io::fs; @@ -24,7 +25,6 @@ use users::OSUsers; pub mod column; pub mod dir; -pub mod format; pub mod file; pub mod filetype; pub mod options; diff --git a/src/file.rs b/src/file.rs index d494ab8..4189a72 100644 --- a/src/file.rs +++ b/src/file.rs @@ -8,9 +8,10 @@ use ansi_term::Colour::{Red, Green, Yellow, Blue, Purple, Cyan, Fixed}; use users::Users; -use column::Column; +use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames}; + +use column::{Column, SizeFormat}; use column::Column::*; -use format::{format_metric_bytes, format_IEC_bytes}; use dir::Dir; use filetype::HasType; @@ -224,21 +225,25 @@ impl<'a> File<'a> { } } - fn file_size(&self, use_iec_prefixes: bool) -> String { + fn file_size(&self, size_format: SizeFormat) -> String { // Don't report file sizes for directories. I've never looked // at one of those numbers and gained any information from it. if self.stat.kind == io::FileType::Directory { GREY.paint("-").to_string() } else { - let (size, suffix) = if use_iec_prefixes { - format_IEC_bytes(self.stat.size) - } - else { - format_metric_bytes(self.stat.size) - }; - - return format!("{}{}", Green.bold().paint(size.as_slice()), Green.paint(suffix.as_slice())); + let result = match size_format { + SizeFormat::DecimalBytes => decimal_prefix(self.stat.size as f64), + SizeFormat::BinaryBytes => binary_prefix(self.stat.size as f64), + }; + + match result { + Standalone(bytes) => Green.bold().paint(bytes.to_string().as_slice()).to_string(), + Prefixed(prefix, n) => { + let number = if n < 10f64 { format!("{:.1}", n) } else { format!("{:.0}", n) }; + format!("{}{}", Green.bold().paint(number.as_slice()), Green.paint(prefix.symbol())) + } + } } } diff --git a/src/format.rs b/src/format.rs deleted file mode 100644 index cae08e5..0000000 --- a/src/format.rs +++ /dev/null @@ -1,80 +0,0 @@ -static METRIC_PREFIXES: &'static [&'static str] = &[ - "", "K", "M", "G", "T", "P", "E", "Z", "Y" -]; - -static IEC_PREFIXES: &'static [&'static str] = &[ - "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" -]; - -fn format_bytes<'s>(mut amount: f64, kilo: f64, prefixes: &[&'s str]) -> (String, &'s str) { - let mut prefix = 0; - while amount >= kilo { - amount /= kilo; - prefix += 1; - } - - if amount < 10.0 && prefix != 0 { - (format!("{:.1}", amount), prefixes[prefix]) - } - else { - (format!("{:.0}", amount), prefixes[prefix]) - } -} - -#[allow(non_snake_case)] -pub fn format_IEC_bytes<'s>(amount: u64) -> (String, &'s str) { - format_bytes(amount as f64, 1024.0, IEC_PREFIXES) -} - -pub fn format_metric_bytes<'s>(amount: u64) -> (String, &'s str) { - format_bytes(amount as f64, 1000.0, METRIC_PREFIXES) -} - -#[test] -fn test_0() { - let kk = format_metric_bytes(0); - assert!(kk == ("0".to_string(), "")); -} - -#[test] -fn test_999() { - let kk = format_metric_bytes(999); - assert!(kk == ("999".to_string(), "")); -} - -#[test] -fn test_1000() { - let kk = format_metric_bytes(1000); - assert!(kk == ("1.0".to_string(), "K")); -} - -#[test] -fn test_1030() { - let kk = format_metric_bytes(1030); - assert!(kk == ("1.0".to_string(), "K")); -} - -#[test] -fn test_1100() { - let kk = format_metric_bytes(1100); - assert!(kk == ("1.1".to_string(), "K")); -} - -#[test] -fn test_1111() { - let kk = format_metric_bytes(1111); - assert!(kk == ("1.1".to_string(), "K")); -} - -#[test] -fn test_104857() { - let kk = format_IEC_bytes(126456); - assert!(kk == ("123".to_string(), "Ki")); -} - -#[test] -fn test_1048576() { - let kk = format_IEC_bytes(1048576); - assert!(kk == ("1.0".to_string(), "Mi")); -} - diff --git a/src/options.rs b/src/options.rs index a74c448..441e1e2 100644 --- a/src/options.rs +++ b/src/options.rs @@ -2,7 +2,7 @@ extern crate getopts; extern crate natord; use file::File; -use column::Column; +use column::{Column, SizeFormat}; use column::Column::*; use term::dimensions; @@ -114,7 +114,12 @@ impl Options { columns.push(HardLinks); } - columns.push(FileSize(matches.opt_present("binary"))); + if matches.opt_present("binary") { + columns.push(FileSize(SizeFormat::BinaryBytes)) + } + else { + columns.push(FileSize(SizeFormat::DecimalBytes)) + } if matches.opt_present("blocks") { columns.push(Blocks);