mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-22 12:05:11 +00:00
Use number_prefix crate for number prefixes
This commit is contained in:
parent
e005174577
commit
24628f97be
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -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"
|
||||
|
@ -9,8 +9,11 @@ name = "exa"
|
||||
[dependencies.ansi_term]
|
||||
git = "https://github.com/ogham/rust-ansi-term.git"
|
||||
|
||||
[dependencies.users]
|
||||
git = "https://github.com/ogham/rust-users.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"
|
||||
|
@ -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,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;
|
||||
|
25
src/file.rs
25
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)
|
||||
};
|
||||
let result = match size_format {
|
||||
SizeFormat::DecimalBytes => decimal_prefix(self.stat.size as f64),
|
||||
SizeFormat::BinaryBytes => binary_prefix(self.stat.size as f64),
|
||||
};
|
||||
|
||||
return format!("{}{}", Green.bold().paint(size.as_slice()), Green.paint(suffix.as_slice()));
|
||||
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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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"));
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user