feat(flags)!: add --classify=always,auto,never

BREAKING CHANGE:
--classify now accepts WHEN (always,auto,never). When none specified defaults to "auto" instead of previous "always" behavior
This commit is contained in:
Guillermo Perez 2024-01-17 12:49:49 +01:00 committed by Christina E. Sørensen
parent 3c31fe09d2
commit 367ff558a6
10 changed files with 41 additions and 16 deletions

View File

@ -88,7 +88,7 @@ ezas 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`

View File

@ -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)" \

View File

@ -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).

View File

@ -36,6 +36,12 @@ commands:
? - -F
- --classify
:
? - -F
- --classify
: values:
- auto
- always
- never
? - null
- --color
: values:

View File

@ -28,12 +28,17 @@ impl Options {
impl Classify {
fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
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),
}
}
}

View File

@ -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 };

View File

@ -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)

View File

@ -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));
}

View File

@ -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)

View File

@ -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)