Skip to content

Commit 932d7eb

Browse files
author
Arkadiusz Bielewicz
committed
lsd-rs#533 thousands separator for the --size=bytes option would be very useful | Added support for thousand separated bytes
1 parent 94f3b68 commit 932d7eb

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Add support for theme from [zwpaper](https://github.com/zwpaper) [#452](https://github.com/Peltoche/lsd/pull/452)
1111
- Update minimal rust version to 1.42.0 from [zwpaper](https://github.com/zwpaper) [#534](https://github.com/Peltoche/lsd/issues/534)
1212
- [`NO_COLOR`](https://no-color.org/) environment variable support from [AnInternetTroll](https://github.com/aninternettroll)
13+
- Added `--size bytes-with-separator` flag to print bytes with thousands separated by `,` [#533](https://github.com/Peltoche/lsd/issues/533)
1314
### Changed
1415
- Change size to use btyes in classic mode from [meain](https://github.com/meain)
1516
- 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)

Cargo.lock

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ xdg = "2.1.*"
3737
yaml-rust = "0.4.*"
3838
serde = { version = "1.0", features = ["derive"] }
3939
serde_yaml = "0.8"
40+
num-format = "0.4.0"
4041

4142
[target.'cfg(unix)'.dependencies]
4243
users = "0.11.*"

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub fn build() -> App<'static, 'static> {
131131
.possible_value("default")
132132
.possible_value("short")
133133
.possible_value("bytes")
134+
.possible_value("bytes-with-separator")
134135
.default_value("default")
135136
.multiple(true)
136137
.number_of_values(1)

src/flags/size.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum SizeFlag {
1818
Short,
1919
/// The variant to show file size in bytes.
2020
Bytes,
21+
/// The variant to show file size in bytes with thousand separated by delimiter.
22+
BytesWithSeparator,
2123
}
2224

2325
impl SizeFlag {
@@ -26,9 +28,10 @@ impl SizeFlag {
2628
"default" => Some(Self::Default),
2729
"short" => Some(Self::Short),
2830
"bytes" => Some(Self::Bytes),
31+
"bytes-with-separator" => Some(Self::BytesWithSeparator),
2932
_ => {
3033
panic!(
31-
"Size can only be one of default, short or bytes, but got {}.",
34+
"Size can only be one of default, short, bytes or bytes-with-separator, but got {}.",
3235
value
3336
);
3437
}
@@ -170,4 +173,14 @@ mod test {
170173
c.classic = Some(true);
171174
assert_eq!(Some(SizeFlag::Bytes), SizeFlag::from_config(&c));
172175
}
176+
177+
#[test]
178+
fn test_from_arg_matches_size_bytes_with_separators() {
179+
let args = vec!["lsd", "--size", "bytes-with-separator"];
180+
let matches = app::build().get_matches_from_safe(args).unwrap();
181+
assert_eq!(
182+
Some(SizeFlag::BytesWithSeparator),
183+
SizeFlag::from_arg_matches(&matches)
184+
);
185+
}
173186
}

src/meta/size.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::color::{ColoredString, Colors, Elem};
22
use crate::flags::{Flags, SizeFlag};
3+
use num_format::{Locale, ToFormattedString};
34
use std::fs::Metadata;
45
use std::iter::repeat;
56

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

42+
fn format_bytes(&self, flags: &Flags) -> String {
43+
if flags.size == SizeFlag::BytesWithSeparator {
44+
self.bytes.to_formatted_string(&Locale::en)
45+
} else {
46+
self.bytes.to_string()
47+
}
48+
}
49+
4150
pub fn get_unit(&self, flags: &Flags) -> Unit {
42-
if self.bytes < 1024 || flags.size == SizeFlag::Bytes {
51+
if self.bytes < 1024
52+
|| flags.size == SizeFlag::Bytes
53+
|| flags.size == SizeFlag::BytesWithSeparator
54+
{
4355
Unit::Byte
4456
} else if self.bytes < 1024 * 1024 {
4557
Unit::Kilo
@@ -111,7 +123,7 @@ impl Size {
111123

112124
match unit {
113125
Unit::None => "".to_string(),
114-
Unit::Byte => self.bytes.to_string(),
126+
Unit::Byte => self.format_bytes(flags),
115127
Unit::Kilo => self.format_size(((self.bytes as f64) / 1024.0 * 10.0).round() / 10.0),
116128
Unit::Mega => {
117129
self.format_size(((self.bytes as f64) / (1024.0 * 1024.0) * 10.0).round() / 10.0)
@@ -151,7 +163,7 @@ impl Size {
151163
Unit::Giga => String::from("G"),
152164
Unit::Tera => String::from("T"),
153165
},
154-
SizeFlag::Bytes => String::from(""),
166+
SizeFlag::Bytes | SizeFlag::BytesWithSeparator => String::from(""),
155167
}
156168
}
157169
}
@@ -336,4 +348,18 @@ mod test {
336348
assert_eq!(size.render(&colors, &flags, Some(2)).to_string(), "42K");
337349
assert_eq!(size.render(&colors, &flags, Some(3)).to_string(), " 42K");
338350
}
351+
352+
#[test]
353+
fn render_bytes_with_separator() {
354+
let size = Size::new(42 * 1024 * 1024); // == 42 megabytes
355+
let mut flags = Flags::default();
356+
357+
flags.size = SizeFlag::Bytes;
358+
assert_eq!(size.value_string(&flags).as_str(), "44040192");
359+
assert_eq!(size.unit_string(&flags).as_str(), "");
360+
361+
flags.size = SizeFlag::BytesWithSeparator;
362+
assert_eq!(size.value_string(&flags).as_str(), "44,040,192");
363+
assert_eq!(size.unit_string(&flags).as_str(), "");
364+
}
339365
}

0 commit comments

Comments
 (0)