Просмотр исходного кода

Use number_prefix crate for number prefixes

Ben S 11 лет назад
Родитель
Сommit
24628f97be
7 измененных файлов с 45 добавлено и 99 удалено
  1. 6 0
      Cargo.lock
  2. 6 3
      Cargo.toml
  3. 8 1
      src/column.rs
  4. 2 2
      src/exa.rs
  5. 16 11
      src/file.rs
  6. 0 80
      src/format.rs
  7. 7 2
      src/options.rs

+ 6 - 0
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"

+ 6 - 3
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"

+ 8 - 1
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.
 

+ 2 - 2
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;

+ 16 - 11
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()))
+				}
+			}
         }
     }
 

+ 0 - 80
src/format.rs

@@ -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"));
-}
-

+ 7 - 2
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);