summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeltoche <dev@halium.fr>2018-11-30 14:53:54 +0100
committerPeltoche <dev@halium.fr>2018-11-30 14:53:54 +0100
commitf1fec9352df1997abbeacdb69277b556f22abd63 (patch)
treec5b017bb8ae67bac7eaac8c71fa27b996f5be0cf
parent4ff3ed063362521c499329c1abd465f67ef459d1 (diff)
Fix the file size values
-rw-r--r--Cargo.lock60
-rw-r--r--Cargo.toml19
-rw-r--r--src/core.rs12
-rw-r--r--src/formatter.rs8
-rw-r--r--src/main.rs1
-rw-r--r--src/meta/mod.rs (renamed from src/meta.rs)14
-rw-r--r--src/meta/size.rs136
7 files changed, 184 insertions, 66 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b43ad8b..458dee9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -11,7 +11,7 @@ name = "atty"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -23,7 +23,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -34,7 +34,7 @@ version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -82,7 +82,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
- "syn 0.15.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -93,30 +93,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
-version = "0.2.43"
+version = "0.2.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lsd"
-version = "0.3.0"
+version = "0.3.1-pre"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "size 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term_grid 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"terminal_size 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
- "users 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "users 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
-name = "num-traits"
-version = "0.2.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "proc-macro2"
version = "0.4.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -134,7 +128,7 @@ dependencies = [
[[package]]
name = "redox_syscall"
-version = "0.1.40"
+version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -142,7 +136,7 @@ name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -151,21 +145,13 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "size"
-version = "0.1.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "strsim"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
-version = "0.15.21"
+version = "0.15.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -180,7 +166,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
- "syn 0.15.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -197,7 +183,7 @@ name = "terminal_size"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -206,8 +192,8 @@ name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
- "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -224,8 +210,8 @@ name = "time"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
- "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -241,10 +227,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "users"
-version = "0.8.0"
+version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -283,16 +269,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7"
"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
-"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
-"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
+"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
-"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
+"checksum redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "679da7508e9a6390aeaf7fbd02a800fdc64b73fe2204dd2c8ae66d22d9d5ad5d"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395"
-"checksum size 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "baf1794b187cb1fd42cbe074cafc027be10b275c68aa7d039dcbef41e94d01d4"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
-"checksum syn 0.15.21 (registry+https://github.com/rust-lang/crates.io-index)" = "816b7af21405b011a23554ea2dc3f6576dc86ca557047c34098c1d741f10f823"
+"checksum syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)" = "ae8b29eb5210bc5cf63ed6149cbf9adfc82ac0be023d8735c176ee74a2db4da7"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum term_grid 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "230d3e804faaed5a39b08319efb797783df2fd9671b39b7596490cb486d702cf"
"checksum terminal_size 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "023345d35850b69849741bd9a5432aa35290e3d8eb76af8717026f270d1cf133"
@@ -301,7 +285,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
-"checksum users 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb3c80625ae5e77e1b402f8a0fa89afbd50622a6cae65128844720bd4e26b657"
+"checksum users 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fed7d0912567d35f88010c23dbaf865e9da8b5227295e8dc0f2fdd109155ab7"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
diff --git a/Cargo.toml b/Cargo.toml
index 13da218..2d4828b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,21 +10,22 @@ readme = "./README.md"
repository = "https://github.com/Peltoche/lsd"
version = "0.3.1-pre"
-[metadata.deb]
-assets = [["target/release/lsd", "usr/bin/", "755"], ["README.md", "usr/share/doc/cargo-deb/README", "644"]]
-depends = "$auto"
-license-file = ["LICENSE", "2"]
-maintainer = "Peltoche <dev@halium.fr>"
-priority = "optional"
-section = "utility"
-
[dependencies]
ansi_term = "0.11.0"
clap = "2.32.0"
failure = "0.1.3"
lazy_static = "1.2.0"
-size = "0.1.1"
term_grid = "0.1.7"
terminal_size = "0.1.8"
time = "0.1.40"
users = "0.8.0"
+
+[metadata]
+
+[metadata.deb]
+assets = [["target/release/lsd", "usr/bin/", "755"], ["README.md", "usr/share/doc/cargo-deb/README", "644"]]
+depends = "$auto"
+license-file = ["LICENSE", "2"]
+maintainer = "Peltoche <dev@halium.fr>"
+priority = "optional"
+section = "utility"
diff --git a/src/core.rs b/src/core.rs
index c351b14..61d2a78 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -171,17 +171,17 @@ impl<'a> Core<'a> {
max
}
- fn detect_size_lenghts(&self, paths: &[Meta]) -> (usize, usize) {
+ fn detect_size_lenghts(&self, metas: &[Meta]) -> (usize, usize) {
let mut max_value_length: usize = 0;
let mut max_unit_size: usize = 0;
- for path in paths {
- if path.size_value.len() > max_value_length {
- max_value_length = path.size_value.len();
+ for meta in metas {
+ if meta.size.render_value().len() > max_value_length {
+ max_value_length = meta.size.render_value().len();
}
- if path.size_unit.len() > max_unit_size {
- max_unit_size = path.size_unit.len();
+ if meta.size.render_unit().len() > max_unit_size {
+ max_unit_size = meta.size.render_unit().len();
}
}
diff --git a/src/formatter.rs b/src/formatter.rs
index adfc7d7..b139432 100644
--- a/src/formatter.rs
+++ b/src/formatter.rs
@@ -181,15 +181,15 @@ impl Formatter {
) -> String {
let mut content = String::with_capacity(max_value_length + max_unit_size + 1);
- for _ in 0..(max_value_length - meta.size_value.len()) {
+ for _ in 0..(max_value_length - meta.size.render_value().len()) {
content.push(' ');
}
- content += meta.size_value.as_str();
+ content += meta.size.render_value().as_str();
content.push(' ');
- content += meta.size_unit.as_str();
+ content += meta.size.render_unit().as_str();
- for _ in 0..(max_unit_size - meta.size_unit.len()) {
+ for _ in 0..(max_unit_size - meta.size.render_unit().len()) {
content.push(' ');
}
diff --git a/src/main.rs b/src/main.rs
index d720433..ce8d98d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,6 @@ extern crate clap;
extern crate lazy_static;
extern crate ansi_term;
extern crate failure;
-extern crate size;
extern crate term_grid;
extern crate terminal_size;
extern crate time;
diff --git a/src/meta.rs b/src/meta/mod.rs
index 4f2ae24..cc5076f 100644
--- a/src/meta.rs
+++ b/src/meta/mod.rs
@@ -1,11 +1,13 @@
+use self::size::Size;
use failure::*;
-use size::*;
use std::fs::{read_link, Metadata};
use std::io;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use users::{get_group_by_gid, get_user_by_uid};
+mod size;
+
#[derive(Debug, Fail)]
pub enum MetaError {
#[fail(display = "file name not readable for {}", path)]
@@ -41,8 +43,7 @@ pub struct Meta {
pub group: String,
pub user: String,
pub node_type: Type,
- pub size_value: String,
- pub size_unit: String,
+ pub size: Size,
}
impl Meta {
@@ -115,9 +116,7 @@ impl Meta {
.expect("failed to convert group name to str")
.to_string();
- let size = Size::Bytes(meta.len()).to_string(Base::Base10, Style::Abbreviated);
- let size_parts: Vec<&str> = size.split(' ').collect();
-
+ let size = meta.len();
Ok(Meta {
path: path.to_path_buf(),
metadata: meta,
@@ -125,8 +124,7 @@ impl Meta {
user,
group,
node_type: node_type,
- size_value: size_parts[0].to_string(),
- size_unit: size_parts[1].to_string(),
+ size: Size::from_bytes(size as usize),
})
}
}
diff --git a/src/meta/size.rs b/src/meta/size.rs
new file mode 100644
index 0000000..73ade4c
--- /dev/null
+++ b/src/meta/size.rs
@@ -0,0 +1,136 @@
+#[derive(Debug)]
+pub enum Unit {
+ Byte,
+ Kilo,
+ Mega,
+ Giga,
+ Tera,
+}
+
+#[derive(Debug)]
+pub struct Size {
+ value: usize,
+ unit: Unit,
+}
+
+impl Size {
+ pub fn from_bytes(bytes: usize) -> Self {
+ if bytes < 1024 {
+ Size {
+ value: bytes * 1024,
+ unit: Unit::Byte,
+ }
+ } else if bytes < 1024 * 1024 {
+ Size {
+ value: bytes,
+ unit: Unit::Kilo,
+ }
+ } else if bytes < 1024 * 1024 * 1024 {
+ Size {
+ value: bytes / 1024,
+ unit: Unit::Mega,
+ }
+ } else if bytes < 1024 * 1024 * 1024 * 1024 {
+ Size {
+ value: bytes / (1024 * 1024),
+ unit: Unit::Giga,
+ }
+ } else {
+ Size {
+ value: bytes / (1024 * 1024 * 1024),
+ unit: Unit::Tera,
+ }
+ }
+ }
+
+ pub fn render_value(&self) -> String {
+ let size_str = (self.value as f32 / 1024.0).to_string();
+
+ // Check if there is a fraction.
+ if let Some(fraction_idx) = size_str.find('.') {
+ // If the fraction start with 0 (like 32.01), the result is rounded
+ // by removing the fraction.
+ if size_str.chars().nth(fraction_idx + 1) == Some('0') {
+ let (res, _) = size_str.split_at(fraction_idx); // Split before the fraction
+ res.to_string()
+ } else {
+ //
+ let (res, _) = size_str.split_at(fraction_idx + 2); // Split after the '.' and the first fraction digit.
+ res.to_string()
+ }
+ } else {
+ size_str
+ }
+ }
+
+ pub fn render_unit(&self) -> String {
+ match self.unit {
+ Unit::Byte => String::from("B"),
+ Unit::Kilo => String::from("KB"),
+ Unit::Mega => String::from("MB"),
+ Unit::Giga => String::from("GB"),
+ Unit::Tera => String::from("TB"),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Size;
+
+ #[test]
+ fn render_byte() {
+ let size = Size::from_bytes(42); // == 42 bytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "B");
+ }
+
+ #[test]
+ fn render_kilobyte() {
+ let size = Size::from_bytes(42 * 1024); // 42 kilobytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "KB");
+ }
+
+ #[test]
+ fn render_megabyte() {
+ let size = Size::from_bytes(42 * 1024 * 1024); // 42 megabytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "MB");
+ }
+
+ #[test]
+ fn render_gigabyte() {
+ let size = Size::from_bytes(42 * 1024 * 1024 * 1024); // 42 gigabytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "GB");
+ }
+
+ #[test]
+ fn render_terabyte() {
+ let size = Size::from_bytes(42 * 1024 * 1024 * 1024 * 1024); // 42 terabytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "TB");
+ }
+
+ #[test]
+ fn render_with_a_fraction() {
+ let size = Size::from_bytes(42 * 1024 + 103); // 42.1 kilobytes
+
+ assert_eq!(size.render_value().as_str(), "42.1");
+ assert_eq!(size.render_unit().as_str(), "KB");
+ }
+
+ #[test]
+ fn render_with_a_truncated_fraction() {
+ let size = Size::from_bytes(42 * 1024 + 1); // 42.001 kilobytes == 42 kilobytes
+
+ assert_eq!(size.render_value().as_str(), "42");
+ assert_eq!(size.render_unit().as_str(), "KB");
+ }
+}