Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IEC and SI units/unit prefixes #826

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Add IEC and SI units and unit prefixes to `--size`
- In keeping with the coreutils change, add quotes and escapes for necessary filenames from [merelymyself](https://github.com/merelymyself)
- Add support for icon theme from [zwpaper](https://github.com/zwpaper)
- Add icon for kt and kts from [LeeWeeder](https://github.com/LeeWeeder)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ recursion:

# == Size ==
# Specifies the format of the size column.
# Possible values: default, short, bytes
# Possible values: default, short, iec, si, bytes
size: default

# == Permission ==
Expand Down
2 changes: 1 addition & 1 deletion doc/lsd.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich
: How to display permissions [default: rwx] [possible values: rwx, octal]

`--size <size>...`
: How to display size [default: default] [possible values: default, short, bytes]
: How to display size [default: default] [possible values: default, short, iec, si, bytes]

`--sort <WORD>...`
: Sort by WORD instead of name [possible values: size, time, version, extension]
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Cli {
pub permission: Option<String>,

/// How to display size [default: default]
#[arg(long, value_name = "MODE", value_parser = ["default", "short", "bytes"])]
#[arg(long, value_name = "MODE", value_parser = ["default", "short", "iec", "si", "bytes"])]
pub size: Option<String>,

/// Display the total size of directories
Expand Down
2 changes: 1 addition & 1 deletion src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ recursion:

# == Size ==
# Specifies the format of the size column.
# Possible values: default, short, bytes
# Possible values: default, short, iec, si, bytes
size: default

# == Permission ==
Expand Down
34 changes: 34 additions & 0 deletions src/flags/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum SizeFlag {
Default,
/// The variant to show file size with only the SI unit prefix.
Short,
/// The variant to show file size with IEC unit prefix and a B for bytes
Iec,
/// The variant to show file size with SI units and SI unit prefixes and a B for bytes
Si,
/// The variant to show file size in bytes.
Bytes,
}
Expand All @@ -26,6 +30,8 @@ impl SizeFlag {
match value {
"default" => Self::Default,
"short" => Self::Short,
"iec" => Self::Iec,
"si" => Self::Si,
"bytes" => Self::Bytes,
// Invalid value should be handled by `clap` when building an `Cli`
other => unreachable!("Invalid value '{other}' for 'size'"),
Expand Down Expand Up @@ -97,6 +103,20 @@ mod test {
assert_eq!(Some(SizeFlag::Short), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_iec() {
let argv = ["lsd", "--size", "iec"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Iec), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_si() {
let argv = ["lsd", "--size", "si"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Si), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_bytes() {
let argv = ["lsd", "--size", "bytes"];
Expand Down Expand Up @@ -143,6 +163,20 @@ mod test {
assert_eq!(Some(SizeFlag::Short), SizeFlag::from_config(&c));
}

#[test]
fn test_from_config_iec() {
let mut c = Config::with_none();
c.size = Some(SizeFlag::Iec);
assert_eq!(Some(SizeFlag::Iec), SizeFlag::from_config(&c));
}

#[test]
fn test_from_config_si() {
let mut c = Config::with_none();
c.size = Some(SizeFlag::Si);
assert_eq!(Some(SizeFlag::Si), SizeFlag::from_config(&c));
}

#[test]
fn test_from_config_bytes() {
let mut c = Config::with_none();
Expand Down
Loading