Skip to content

Commit

Permalink
lsd-rs#533 thousands separator for the --size=bytes option would be v…
Browse files Browse the repository at this point in the history
…ery useful | Added support for thousand separated bytes
  • Loading branch information
Arkadiusz Bielewicz committed Oct 15, 2021
1 parent 94f3b68 commit 932d7eb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for theme from [zwpaper](https://github.com/zwpaper) [#452](https://github.com/Peltoche/lsd/pull/452)
- Update minimal rust version to 1.42.0 from [zwpaper](https://github.com/zwpaper) [#534](https://github.com/Peltoche/lsd/issues/534)
- [`NO_COLOR`](https://no-color.org/) environment variable support from [AnInternetTroll](https://github.com/aninternettroll)
- Added `--size bytes-with-separator` flag to print bytes with thousands separated by `,` [#533](https://github.com/Peltoche/lsd/issues/533)
### Changed
- Change size to use btyes in classic mode from [meain](https://github.com/meain)
- Show tree edge before name block or first column if no name block from [zwpaper](https://github.com/zwpaper) [#468](https://github.com/Peltoche/lsd/issues/468)
Expand Down
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ xdg = "2.1.*"
yaml-rust = "0.4.*"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
num-format = "0.4.0"

[target.'cfg(unix)'.dependencies]
users = "0.11.*"
Expand Down
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub fn build() -> App<'static, 'static> {
.possible_value("default")
.possible_value("short")
.possible_value("bytes")
.possible_value("bytes-with-separator")
.default_value("default")
.multiple(true)
.number_of_values(1)
Expand Down
15 changes: 14 additions & 1 deletion src/flags/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum SizeFlag {
Short,
/// The variant to show file size in bytes.
Bytes,
/// The variant to show file size in bytes with thousand separated by delimiter.
BytesWithSeparator,
}

impl SizeFlag {
Expand All @@ -26,9 +28,10 @@ impl SizeFlag {
"default" => Some(Self::Default),
"short" => Some(Self::Short),
"bytes" => Some(Self::Bytes),
"bytes-with-separator" => Some(Self::BytesWithSeparator),
_ => {
panic!(
"Size can only be one of default, short or bytes, but got {}.",
"Size can only be one of default, short, bytes or bytes-with-separator, but got {}.",
value
);
}
Expand Down Expand Up @@ -170,4 +173,14 @@ mod test {
c.classic = Some(true);
assert_eq!(Some(SizeFlag::Bytes), SizeFlag::from_config(&c));
}

#[test]
fn test_from_arg_matches_size_bytes_with_separators() {
let args = vec!["lsd", "--size", "bytes-with-separator"];
let matches = app::build().get_matches_from_safe(args).unwrap();
assert_eq!(
Some(SizeFlag::BytesWithSeparator),
SizeFlag::from_arg_matches(&matches)
);
}
}
32 changes: 29 additions & 3 deletions src/meta/size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::{Flags, SizeFlag};
use num_format::{Locale, ToFormattedString};
use std::fs::Metadata;
use std::iter::repeat;

Expand Down Expand Up @@ -38,8 +39,19 @@ impl Size {
format!("{0:.1$}", number, if number < 10.0 { 1 } else { 0 })
}

fn format_bytes(&self, flags: &Flags) -> String {
if flags.size == SizeFlag::BytesWithSeparator {
self.bytes.to_formatted_string(&Locale::en)
} else {
self.bytes.to_string()
}
}

pub fn get_unit(&self, flags: &Flags) -> Unit {
if self.bytes < 1024 || flags.size == SizeFlag::Bytes {
if self.bytes < 1024
|| flags.size == SizeFlag::Bytes
|| flags.size == SizeFlag::BytesWithSeparator
{
Unit::Byte
} else if self.bytes < 1024 * 1024 {
Unit::Kilo
Expand Down Expand Up @@ -111,7 +123,7 @@ impl Size {

match unit {
Unit::None => "".to_string(),
Unit::Byte => self.bytes.to_string(),
Unit::Byte => self.format_bytes(flags),
Unit::Kilo => self.format_size(((self.bytes as f64) / 1024.0 * 10.0).round() / 10.0),
Unit::Mega => {
self.format_size(((self.bytes as f64) / (1024.0 * 1024.0) * 10.0).round() / 10.0)
Expand Down Expand Up @@ -151,7 +163,7 @@ impl Size {
Unit::Giga => String::from("G"),
Unit::Tera => String::from("T"),
},
SizeFlag::Bytes => String::from(""),
SizeFlag::Bytes | SizeFlag::BytesWithSeparator => String::from(""),
}
}
}
Expand Down Expand Up @@ -336,4 +348,18 @@ mod test {
assert_eq!(size.render(&colors, &flags, Some(2)).to_string(), "42K");
assert_eq!(size.render(&colors, &flags, Some(3)).to_string(), " 42K");
}

#[test]
fn render_bytes_with_separator() {
let size = Size::new(42 * 1024 * 1024); // == 42 megabytes
let mut flags = Flags::default();

flags.size = SizeFlag::Bytes;
assert_eq!(size.value_string(&flags).as_str(), "44040192");
assert_eq!(size.unit_string(&flags).as_str(), "");

flags.size = SizeFlag::BytesWithSeparator;
assert_eq!(size.value_string(&flags).as_str(), "44,040,192");
assert_eq!(size.unit_string(&flags).as_str(), "");
}
}

0 comments on commit 932d7eb

Please sign in to comment.