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 all 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
6 changes: 5 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub struct Cli {
#[arg(short, long)]
human_readable: bool,

/// Shows size using SI units (powers of 1000)
#[arg(long)]
pub si: bool,

/// Recurse into directories and present the result as a tree
#[arg(long)]
pub tree: bool,
Expand All @@ -73,7 +77,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
46 changes: 42 additions & 4 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 All @@ -36,14 +42,18 @@ impl SizeFlag {
impl Configurable<Self> for SizeFlag {
/// Get a potential `SizeFlag` variant from [Cli].
///
/// If any of the "default", "short" or "bytes" arguments is passed, the corresponding
/// `SizeFlag` variant is returned in a [Some]. If neither of them is passed, this returns
/// [None].
/// If any of the "default", "short", "iec", "si", or "bytes" arguments is passed,
/// the corresponding `SizeFlag` variant is returned in a [Some]. If neither of them is passed,
/// this returns [None].
fn from_cli(cli: &Cli) -> Option<Self> {
if cli.classic {
Some(Self::Bytes)
} else {
cli.size.as_deref().map(Self::from_arg_str)
if cli.si {
Some(Self::Si)
} else {
cli.size.as_deref().map(Self::from_arg_str)
}
}
}

Expand Down Expand Up @@ -97,6 +107,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 +167,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