diff --git a/README.md b/README.md index cc2a8d76..d2363cab 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ eza’s options are almost, but not quite, entirely unlike `ls`’s. - **-R**, **--recurse**: recurse into directories - **-T**, **--tree**: recurse into directories as a tree - **-x**, **--across**: sort the grid across, rather than downwards -- **-F**, **--classify**: display type indicator by file names +- **-F**, **--classify=(when)**: display type indicator by file names (always, auto, never) - **--colo[u]r=(when)**: when to use terminal colours (always, auto, never) - **--colo[u]r-scale=(field)**: highlight levels of `field` distinctly(all, age, size) - **--color-scale-mode=(mode)**: use gradient or fixed colors in --color-scale. valid options are `fixed` or `gradient` diff --git a/completions/zsh/_eza b/completions/zsh/_eza index f7c7f254..71bf2d88 100644 --- a/completions/zsh/_eza +++ b/completions/zsh/_eza @@ -19,7 +19,7 @@ __eza() { {-R,--recurse}"[Recurse into directories]" \ {-T,--tree}"[Recurse into directories as a tree]" \ {-X,--dereference}"[Dereference symbolic links when displaying information]" \ - {-F,--classify}"[Display type indicator by file names]" \ + {-F,--classify}"[Display type indicator by file names]:(when):(always auto automatic never)" \ --colo{,u}r="[When to use terminal colours]:(when):(always auto automatic never)" \ --colo{,u}r-scale"[highlight levels of 'field' distinctly]:(fields):(all age size)" \ --colo{,u}r-scale-mode"[Use gradient or fixed colors in --color-scale]:(mode):(fixed gradient)" \ diff --git a/man/eza.1.md b/man/eza.1.md index b427d303..c77b613e 100644 --- a/man/eza.1.md +++ b/man/eza.1.md @@ -54,9 +54,14 @@ DISPLAY OPTIONS `-1`, `--oneline` : Display one entry per line. -`-F`, `--classify` +`-F`, `--classify=WHEN` : Display file kind indicators next to file names. +Valid settings are ‘`always`’, ‘`automatic`’ (or ‘`auto`’ for short), and ‘`never`’. +The default value is ‘`automatic`’. + +The default behavior (`automatic` or `auto`) will display file kind indicators only when the standard output is connected to a real terminal. If `eza` is ran while in a `tty`, or the output of `eza` is either redirected to a file or piped into another program, file kind indicators will not be used. Setting this option to ‘`always`’ causes `eza` to always display file kind indicators, while ‘`never`’ disables the use of file kind indicators. + `-G`, `--grid` : Display entries as a grid (default). diff --git a/powertest.yaml b/powertest.yaml index adda5ec6..a3968b24 100644 --- a/powertest.yaml +++ b/powertest.yaml @@ -36,6 +36,12 @@ commands: ? - -F - --classify : + ? - -F + - --classify + : values: + - auto + - always + - never ? - null - --color : values: diff --git a/src/options/file_name.rs b/src/options/file_name.rs index a45d35d4..0a7bfc09 100644 --- a/src/options/file_name.rs +++ b/src/options/file_name.rs @@ -28,12 +28,17 @@ impl Options { impl Classify { fn deduce(matches: &MatchedFlags<'_>) -> Result { - let flagged = matches.has(&flags::CLASSIFY)?; + let mode_opt = matches.get(&flags::CLASSIFY)?; - if flagged { - Ok(Self::AddFileIndicators) - } else { - Ok(Self::JustFilenames) + match mode_opt { + Some(word) => match word.to_str() { + Some("always") => Ok(Self::AddFileIndicators), + Some("auto" | "automatic") => Ok(Self::AutomaticAddFileIndicators), + Some("never") => Ok(Self::JustFilenames), + _ => Err(OptionsError::BadArgument(&flags::CLASSIFY, word.into())), + }, + // No flag given, default to just filenames + None => Ok(Self::JustFilenames), } } } diff --git a/src/options/flags.rs b/src/options/flags.rs index 715f5bbf..16153c65 100644 --- a/src/options/flags.rs +++ b/src/options/flags.rs @@ -11,7 +11,7 @@ pub static GRID: Arg = Arg { short: Some(b'G'), long: "grid", take pub static ACROSS: Arg = Arg { short: Some(b'x'), long: "across", takes_value: TakesValue::Forbidden }; pub static RECURSE: Arg = Arg { short: Some(b'R'), long: "recurse", takes_value: TakesValue::Forbidden }; pub static TREE: Arg = Arg { short: Some(b'T'), long: "tree", takes_value: TakesValue::Forbidden }; -pub static CLASSIFY: Arg = Arg { short: Some(b'F'), long: "classify", takes_value: TakesValue::Forbidden }; +pub static CLASSIFY: Arg = Arg { short: Some(b'F'), long: "classify", takes_value: TakesValue::Optional(Some(WHEN), "auto") }; pub static DEREF_LINKS: Arg = Arg { short: Some(b'X'), long: "dereference", takes_value: TakesValue::Forbidden }; pub static WIDTH: Arg = Arg { short: Some(b'w'), long: "width", takes_value: TakesValue::Necessary(None) }; pub static NO_QUOTES: Arg = Arg { short: None, long: "no-quotes", takes_value: TakesValue::Forbidden }; diff --git a/src/options/help.rs b/src/options/help.rs index 4ef57aee..e4f456a7 100644 --- a/src/options/help.rs +++ b/src/options/help.rs @@ -19,7 +19,7 @@ DISPLAY OPTIONS -R, --recurse recurse into directories -T, --tree recurse into directories as a tree -X, --dereference dereference symbolic links when displaying information - -F, --classify display type indicator by file names + -F, --classify=WHEN display type indicator by file names (always, auto, never) --colo[u]r=WHEN when to use terminal colours (always, auto, never) --colo[u]r-scale highlight levels of 'field' distinctly(all, age, size) --colo[u]r-scale-mode use gradient or fixed colors in --color-scale (fixed, gradient) diff --git a/src/output/file_name.rs b/src/output/file_name.rs index 2b0f768e..5172bd63 100644 --- a/src/output/file_name.rs +++ b/src/output/file_name.rs @@ -74,9 +74,12 @@ pub enum Classify { /// Just display the file names, without any characters. JustFilenames, - /// Add a character after the file name depending on what class of file - /// it is. + /// Always add a character after the file name depending on what class of + /// file it is. AddFileIndicators, + + // Like previous, but only when output is going to a terminal, not otherwise. + AutomaticAddFileIndicators, } impl Default for Classify { @@ -189,6 +192,12 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> { _ => None, }; + let should_add_classify_char = match self.options.classify { + Classify::AddFileIndicators => true, + Classify::AutomaticAddFileIndicators if self.options.is_a_tty => true, + _ => false, + }; + if let Some(spaces_count) = spaces_count_opt { let style = iconify_style(self.style()); let file_icon = icon_for_file(self.file).to_string(); @@ -248,7 +257,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> { bits.push(bit); } - if let Classify::AddFileIndicators = self.options.classify { + if should_add_classify_char { if let Some(class) = self.classify_char(target) { bits.push(Style::default().paint(class)); } @@ -274,7 +283,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> { // Do nothing — the error gets displayed on the next line } } - } else if let Classify::AddFileIndicators = self.options.classify { + } else if should_add_classify_char { if let Some(class) = self.classify_char(self.file) { bits.push(Style::default().paint(class)); } diff --git a/tests/gen/ptest_2439b7d68089135b.stdout b/tests/gen/ptest_2439b7d68089135b.stdout index e47700ca..4715e8ea 100644 --- a/tests/gen/ptest_2439b7d68089135b.stdout +++ b/tests/gen/ptest_2439b7d68089135b.stdout @@ -13,7 +13,7 @@ DISPLAY OPTIONS -R, --recurse recurse into directories -T, --tree recurse into directories as a tree -X, --dereference dereference symbolic links when displaying information - -F, --classify display type indicator by file names + -F, --classify=WHEN display type indicator by file names --colo[u]r=WHEN when to use terminal colours (always, auto, never) --colo[u]r-scale highlight levels of 'field' distinctly(all, age, size) --colo[u]r-scale-mode use gradient or fixed colors in --color-scale (fixed, gradient) diff --git a/tests/ptests/ptest_2439b7d68089135b.stdout b/tests/ptests/ptest_2439b7d68089135b.stdout index e47700ca..4715e8ea 100644 --- a/tests/ptests/ptest_2439b7d68089135b.stdout +++ b/tests/ptests/ptest_2439b7d68089135b.stdout @@ -13,7 +13,7 @@ DISPLAY OPTIONS -R, --recurse recurse into directories -T, --tree recurse into directories as a tree -X, --dereference dereference symbolic links when displaying information - -F, --classify display type indicator by file names + -F, --classify=WHEN display type indicator by file names --colo[u]r=WHEN when to use terminal colours (always, auto, never) --colo[u]r-scale highlight levels of 'field' distinctly(all, age, size) --colo[u]r-scale-mode use gradient or fixed colors in --color-scale (fixed, gradient)