From 450ebbf158802dd59f61acf3a91cef378b3623d2 Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sat, 16 Sep 2023 17:38:12 -0600 Subject: [PATCH 1/6] human_readable flag (-h) functionality enabled This re-enables a quick, easy to type command line flag that `ls` users are familiar with. In the event a user has changed the default configuration of `lsd` to use a size setting of something other than `default`, this lets a user use the historically established CLI flag (`-h`) to enable the human readable option without having to type `--size=default` on the command line. --- src/app.rs | 2 +- src/flags/size.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index bd6defaeb..2306dd4ec 100644 --- a/src/app.rs +++ b/src/app.rs @@ -54,7 +54,7 @@ pub struct Cli { /// For ls compatibility purposes ONLY, currently set by default #[arg(short, long)] - human_readable: bool, + pub human_readable: bool, /// Recurse into directories and present the result as a tree #[arg(long)] diff --git a/src/flags/size.rs b/src/flags/size.rs index 4a116cc50..ad9554382 100644 --- a/src/flags/size.rs +++ b/src/flags/size.rs @@ -27,6 +27,7 @@ impl SizeFlag { "default" => Self::Default, "short" => Self::Short, "bytes" => Self::Bytes, + "h" => Self::Default, // Invalid value should be handled by `clap` when building an `Cli` other => unreachable!("Invalid value '{other}' for 'size'"), } @@ -40,6 +41,9 @@ impl Configurable for SizeFlag { /// `SizeFlag` variant is returned in a [Some]. If neither of them is passed, this returns /// [None]. fn from_cli(cli: &Cli) -> Option { + if cli.human_readable { + return Some(Self::Default) + } if cli.classic { Some(Self::Bytes) } else { From 0b8b40ab0f75be636d3c66c2314019bf78b947a7 Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sat, 16 Sep 2023 17:46:02 -0600 Subject: [PATCH 2/6] remove superfluous value match --- src/flags/size.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/flags/size.rs b/src/flags/size.rs index ad9554382..b6f84d124 100644 --- a/src/flags/size.rs +++ b/src/flags/size.rs @@ -27,7 +27,6 @@ impl SizeFlag { "default" => Self::Default, "short" => Self::Short, "bytes" => Self::Bytes, - "h" => Self::Default, // Invalid value should be handled by `clap` when building an `Cli` other => unreachable!("Invalid value '{other}' for 'size'"), } From 28700b1124ba08d9e3ce4636092b0b914fbe21ce Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sat, 16 Sep 2023 18:06:57 -0600 Subject: [PATCH 3/6] documentation updates --- CHANGELOG.md | 10 ++++++++++ doc/lsd.md | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ee01ae26..6700f2e07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Added + +### Fixed + +### Changed +- Human readable size flag `-h` re-enabled. [sdorr0](https://github.com/sdorr0) + ## [v1.0.0] - 2023-08-25 ### Added diff --git a/doc/lsd.md b/doc/lsd.md index b86cdd0de..1c0e79929 100644 --- a/doc/lsd.md +++ b/doc/lsd.md @@ -45,7 +45,7 @@ lsd is a ls command with a lot of pretty colours and some other stuff to enrich : Prints help information `-h`, `--human-readable` -: For ls compatibility purposes ONLY, currently set by default +: Use the `default` display size to show sizes as e.g., 1.7 KB, 3.0 GB, etc. `--ignore-config` : Ignore the configuration file From f6e1bf030a90a3899b53bb4ccb3007ed4f99b069 Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sat, 16 Sep 2023 18:11:22 -0600 Subject: [PATCH 4/6] `cargo fmt` added a ';' --- src/flags/size.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flags/size.rs b/src/flags/size.rs index b6f84d124..4ceda68e8 100644 --- a/src/flags/size.rs +++ b/src/flags/size.rs @@ -41,7 +41,7 @@ impl Configurable for SizeFlag { /// [None]. fn from_cli(cli: &Cli) -> Option { if cli.human_readable { - return Some(Self::Default) + return Some(Self::Default); } if cli.classic { Some(Self::Bytes) From de82d7ba85950a78e7a4aa5811e15f2bde483bb8 Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sat, 16 Sep 2023 18:33:40 -0600 Subject: [PATCH 5/6] Added tests for -h and --human-readable --- src/flags/size.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/flags/size.rs b/src/flags/size.rs index 4ceda68e8..d102b68ec 100644 --- a/src/flags/size.rs +++ b/src/flags/size.rs @@ -100,6 +100,20 @@ mod test { assert_eq!(Some(SizeFlag::Short), SizeFlag::from_cli(&cli)); } + #[test] + fn test_from_cli_human_readable() { + let argv = ["lsd", "--human-readable"]; + let cli = Cli::try_parse_from(argv).unwrap(); + assert_eq!(Some(SizeFlag::Default), SizeFlag::from_cli(&cli)); + } + + #[test] + fn test_from_cli_minus_h() { + let argv = ["lsd", "-h"]; + let cli = Cli::try_parse_from(argv).unwrap(); + assert_eq!(Some(SizeFlag::Default), SizeFlag::from_cli(&cli)); + } + #[test] fn test_from_cli_bytes() { let argv = ["lsd", "--size", "bytes"]; From 0758258875fe5751e24a866f43177a37855981f4 Mon Sep 17 00:00:00 2001 From: Scott Dorr Date: Sun, 17 Sep 2023 14:40:15 -0600 Subject: [PATCH 6/6] fix comment --- src/app.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 2306dd4ec..e7904991b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -52,7 +52,7 @@ pub struct Cli { #[arg(short = 'R', long, conflicts_with = "tree")] pub recursive: bool, - /// For ls compatibility purposes ONLY, currently set by default + /// This will take precedence over any --size= option #[arg(short, long)] pub human_readable: bool,