feat: Add EZA_ environment variables with fallback to EXA_

This commit is contained in:
Robert Minsk 2023-09-19 00:04:13 -07:00
parent 7e60ba16b7
commit 23d4293b8b
13 changed files with 67 additions and 41 deletions

View file

@ -1,7 +1,7 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("logger", |b| b.iter(|| eza::logger::configure(black_box(std::env::var_os(eza::options::vars::EXA_DEBUG)))));
c.bench_function("logger", |b| b.iter(|| eza::logger::configure(black_box(std::env::var_os(eza::options::vars::EZA_DEBUG)))));
}
criterion_group!(benches, criterion_benchmark);

View file

@ -197,7 +197,7 @@ These options are available when running with `--long` (`-l`):
`--git` [if eza was built with git support]
: List each files Git status, if tracked.
This adds a two-character column indicating the staged and unstaged statuses respectively. The status character can be `-` for not modified, `M` for a modified file, `N` for a new file, `D` for deleted, `R` for renamed, `T` for type-change, `I` for ignored, and `U` for conflicted. :Directories will be shown to have the status of their contents, which is how deleted is possible if a directory contains a file that has a certain status, it will be shown to have that status.
This adds a two-character column indicating the staged and unstaged statuses respectively. The status character can be `-` for not modified, `M` for a modified file, `N` for a new file, `D` for deleted, `R` for renamed, `T` for type-change, `I` for ignored, and `U` for conflicted. Directories will be shown to have the status of their contents, which is how deleted is possible if a directory contains a file that has a certain status, it will be shown to have that status.
`--git-repos` [if eza was built with git support]
: List each directorys Git status, if tracked.
@ -215,6 +215,8 @@ All Git repository directories will be shown as (themed) `-` without status indi
ENVIRONMENT VARIABLES
=====================
If an environment variable prefixed with `EZA_` is not set, for backward compatibility, it will default to its counterpart starting with `EXA_`.
eza responds to the following environment variables:
## `COLUMNS`
@ -225,7 +227,7 @@ For example, `COLUMNS=80 eza` will show a grid view with a maximum width o
This option wont do anything when ezas output doesnt wrap, such as when using the `--long` view.
## `EXA_STRICT`
## `EZA_STRICT`
Enables _strict mode_, which will make eza error when two command-line options are incompatible.
@ -235,14 +237,14 @@ In strict mode, the two options will not co-operate, and eza will error.
This option is intended for use with automated scripts and other situations where you want to be certain youre typing in the right command.
## `EXA_GRID_ROWS`
## `EZA_GRID_ROWS`
Limits the grid-details view (`eza --grid --long`) so its only activated when at least the given number of rows of output would be generated.
With widescreen displays, its possible for the grid to look very wide and sparse, on just one or two lines with none of the columns lining up.
By specifying a minimum number of rows, you can only use the view if its going to be worth using.
## `EXA_ICON_SPACING`
## `EZA_ICON_SPACING`
Specifies the number of spaces to print between an icon (see the `--icons` option) and its file name.
@ -254,7 +256,7 @@ Disables colours in the output (regardless of its value). Can be overridden by `
See `https://no-color.org/` for details.
## `LS_COLORS`, `EXA_COLORS`
## `LS_COLORS`, `EZA_COLORS`
Specifies the colour scheme used to highlight files based on their name and kind, as well as highlighting metadata and parts of the UI.

View file

@ -8,22 +8,22 @@ eza provides its own built\-in set of file extension mappings that cover a large
Any mappings in the environment variables will override this default set: running eza with `LS_COLORS="*.zip=32"` will turn zip files green but leave the colours of other compressed files alone.
You can also disable this built\-in set entirely by including a
`reset` entry at the beginning of `EXA_COLORS`.
So setting `EXA_COLORS="reset:*.txt=31"` will highlight only text
files; setting `EXA_COLORS="reset"` will highlight nothing.
`reset` entry at the beginning of `EZA_COLORS`.
So setting `EZA_COLORS="reset:*.txt=31"` will highlight only text
files; setting `EZA_COLORS="reset"` will highlight nothing.
## Examples
- Disable the "current user" highlighting: `EXA_COLORS="uu=0:gu=0"`
- Turn the date column green: `EXA_COLORS="da=32"`
- Highlight Vagrantfiles: `EXA_COLORS="Vagrantfile=1;4;33"`
- Override the existing zip colour: `EXA_COLORS="*.zip=38;5;125"`
- Disable the "current user" highlighting: `EZA_COLORS="uu=0:gu=0"`
- Turn the date column green: `EZA_COLORS="da=32"`
- Highlight Vagrantfiles: `EZA_COLORS="Vagrantfile=1;4;33"`
- Override the existing zip colour: `EZA_COLORS="*.zip=38;5;125"`
- Markdown files a shade of green, log files a shade of grey:
`EXA_COLORS="*.md=38;5;121:*.log=38;5;248"`
`EZA_COLORS="*.md=38;5;121:*.log=38;5;248"`
## BUILT\-IN EXTENSIONS
- eza now supports bright colours! As supported by most modern 256\-colour terminals, you can now choose from `bright` colour codes when selecting your custom colours in your `#EXA_COLORS` environment variable.
- eza now supports bright colours! As supported by most modern 256\-colour terminals, you can now choose from `bright` colour codes when selecting your custom colours in your `#EZA_COLORS` environment variable.
- Build (Makefile, Cargo.toml, package.json) are yellow and underlined.
- Images (png, jpeg, gif) are purple.

View file

@ -14,7 +14,7 @@ eza_colors — customising the file and UI colours of eza
SYNOPSIS
========
The `EXA_COLORS` environment variable can be used to customise the colours that `eza` uses to highlight file names, file metadata, and parts of the UI.
The `EZA_COLORS` environment variable can be used to customise the colours that `eza` uses to highlight file names, file metadata, and parts of the UI.
You can use the `dircolors` program to generate a script that sets the variable from an input file, or if you dont mind editing long strings of text, you can just type it out directly. These variables have the following structure:
@ -24,23 +24,25 @@ You can use the `dircolors` program to generate a script that sets the variable
The key half of the pair can either be a two-letter code or a file glob, and anything thats not a valid code will be treated as a glob, including keys that happen to be two letters long.
For backwards compatability `EXA_COLORS` environment variables is checked if `EZA_COLORS` is unset.
EXAMPLES
========
`EXA_COLORS="uu=0:gu=0"`
`EZA_COLORS="uu=0:gu=0"`
: Disable the “current user” highlighting
`EXA_COLORS="da=32"`
`EZA_COLORS="da=32"`
: Turn the date column green
`EXA_COLORS="Vagrantfile=1;4;33"`
`EZA_COLORS="Vagrantfile=1;4;33"`
: Highlight Vagrantfiles
`EXA_COLORS="*.zip=38;5;125"`
`EZA_COLORS="*.zip=38;5;125"`
: Override the existing zip colour
`EXA_COLORS="*.md=38;5;121:*.log=38;5;248"`
`EZA_COLORS="*.md=38;5;121:*.log=38;5;248"`
: Markdown files a shade of green, log files a shade of grey
@ -77,7 +79,7 @@ LIST OF CODES
: symlinks with no target
`EXA_COLORS` can use many more:
`EZA_COLORS` can use many more:
`ur`
: the user-read permission bit
@ -253,7 +255,7 @@ LIST OF CODES
`bu`
: a regular file that is used to build a project (ex: Makefile)
Values in `EXA_COLORS` override those given in `LS_COLORS`, so you dont need to re-write an existing `LS_COLORS` variable with proprietary extensions.
Values in `EZA_COLORS` override those given in `LS_COLORS`, so you dont need to re-write an existing `LS_COLORS` variable with proprietary extensions.
LIST OF STYLES
@ -328,8 +330,8 @@ Many terminals will treat bolded text as a different colour, or at least provide
eza provides its own built-in set of file extension mappings that cover a large range of common file extensions, including documents, archives, media, and temporary files.
Any mappings in the environment variables will override this default set: running eza with `LS_COLORS="*.zip=32"` will turn zip files green but leave the colours of other compressed files alone.
You can also disable this built-in set entirely by including a `reset` entry at the beginning of `EXA_COLORS`.
So setting `EXA_COLORS="reset:*.txt=31"` will highlight only text files; setting `EXA_COLORS="reset"` will highlight nothing.
You can also disable this built-in set entirely by including a `reset` entry at the beginning of `EZA_COLORS`.
So setting `EZA_COLORS="reset:*.txt=31"` will highlight only text files; setting `EZA_COLORS="reset"` will highlight nothing.
AUTHOR

View file

@ -52,7 +52,7 @@ fn main() {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
logger::configure(env::var_os(vars::EXA_DEBUG));
logger::configure(env::var_os(vars::EZA_DEBUG).or_else(|| env::var_os(vars::EXA_DEBUG)));
#[cfg(windows)]
if let Err(e) = ansiterm::enable_ansi_support() {

View file

@ -29,13 +29,13 @@ impl ShowIcons {
if matches.has(&flags::NO_ICONS)? || !matches.has(&flags::ICONS)? {
Ok(Self::Off)
}
else if let Some(columns) = vars.get(vars::EXA_ICON_SPACING).and_then(|s| s.into_string().ok()) {
else if let Some(columns) = vars.get_with_fallback(vars::EZA_ICON_SPACING, vars::EXA_ICON_SPACING).and_then(|s| s.into_string().ok()) {
match columns.parse() {
Ok(width) => {
Ok(Self::On(width))
}
Err(e) => {
let source = NumberSource::Env(vars::EXA_ICON_SPACING);
let source = NumberSource::Env(vars.source(vars::EZA_ICON_SPACING, vars::EXA_ICON_SPACING).unwrap());
Err(OptionsError::FailedParse(columns, source, e))
}
}

View file

@ -133,7 +133,7 @@ impl Options {
{
use crate::options::parser::{Matches, Strictness};
let strictness = match vars.get(vars::EXA_STRICT) {
let strictness = match vars.get_with_fallback(vars::EZA_STRICT, vars::EXA_STRICT) {
None => Strictness::UseLastArguments,
Some(ref t) if t.is_empty() => Strictness::UseLastArguments,
Some(_) => Strictness::ComplainAboutRedundantArguments,

View file

@ -59,8 +59,10 @@ impl ColourScale {
impl Definitions {
fn deduce<V: Vars>(vars: &V) -> Self {
let ls = vars.get(vars::LS_COLORS) .map(|e| e.to_string_lossy().to_string());
let exa = vars.get(vars::EXA_COLORS).map(|e| e.to_string_lossy().to_string());
let ls = vars.get(vars::LS_COLORS)
.map(|e| e.to_string_lossy().to_string());
let exa = vars.get_with_fallback(vars::EZA_COLORS, vars::EXA_COLORS)
.map(|e| e.to_string_lossy().to_string());
Self { ls, exa }
}
}
@ -148,7 +150,7 @@ mod terminal_test {
if name == vars::LS_COLORS && ! self.ls.is_empty() {
Some(OsString::from(self.ls))
}
else if name == vars::EXA_COLORS && ! self.exa.is_empty() {
else if (name == vars::EZA_COLORS || name == vars::EXA_COLORS) && ! self.exa.is_empty() {
Some(OsString::from(self.exa))
}
else if name == vars::NO_COLOR && ! self.no_color.is_empty() {

View file

@ -25,6 +25,7 @@ pub static NO_COLOR: &str = "NO_COLOR";
/// enabled. This includes all the colours that `LS_COLORS` would recognise,
/// overriding them if necessary. It can also contain exa-specific codes.
pub static EXA_COLORS: &str = "EXA_COLORS";
pub static EZA_COLORS: &str = "EZA_COLORS";
/// Environment variable used to switch on strict argument checking, such as
/// complaining if an argument was specified twice, or if two conflict.
@ -32,26 +33,45 @@ pub static EXA_COLORS: &str = "EXA_COLORS";
/// behaviour in a script, rather than for general command-line use.
/// Any non-empty value will turn strict mode on.
pub static EXA_STRICT: &str = "EXA_STRICT";
pub static EZA_STRICT: &str = "EZA_STRICT";
/// Environment variable used to make exa print out debugging information as
/// it runs. Any non-empty value will turn debug mode on.
pub static EXA_DEBUG: &str = "EXA_DEBUG";
pub static EZA_DEBUG: &str = "EZA_DEBUG";
/// Environment variable used to limit the grid-details view
/// (`--grid --long`) so its only activated if theres at least the given
/// number of rows of output.
pub static EXA_GRID_ROWS: &str = "EXA_GRID_ROWS";
pub static EZA_GRID_ROWS: &str = "EZA_GRID_ROWS";
/// Environment variable used to specify how many spaces to print between an
/// icon and its file name. Different terminals display icons differently,
/// with 1 space bringing them too close together or 2 spaces putting them too
/// far apart, so this may be necessary depending on how they are shown.
pub static EXA_ICON_SPACING: &str = "EXA_ICON_SPACING";
pub static EZA_ICON_SPACING: &str = "EZA_ICON_SPACING";
/// Mockable wrapper for `std::env::var_os`.
pub trait Vars {
fn get(&self, name: &'static str) -> Option<OsString>;
/// Get the variable `name` and if not set get the variable `fallback`.
fn get_with_fallback(&self, name: &'static str, fallback: &'static str) -> Option<OsString> {
self.get(name).or_else(|| self.get(fallback))
}
/// Get the source of the value. If the variable `name` is set return
/// `Some(name)` else if the variable `fallback` is set return
/// `Some(fallback)` else `None`.
fn source(&self, name: &'static str, fallback: &'static str) -> Option<&'static str> {
match self.get(name) {
Some(_) => Some(name),
None => self.get(fallback).and(Some(fallback)),
}
}
}

View file

@ -189,13 +189,13 @@ impl RowThreshold {
fn deduce<V: Vars>(vars: &V) -> Result<Self, OptionsError> {
use crate::options::vars;
if let Some(columns) = vars.get(vars::EXA_GRID_ROWS).and_then(|s| s.into_string().ok()) {
if let Some(columns) = vars.get_with_fallback(vars::EZA_GRID_ROWS, vars::EXA_GRID_ROWS).and_then(|s| s.into_string().ok()) {
match columns.parse() {
Ok(rows) => {
Ok(Self::MinimumRows(rows))
}
Err(e) => {
let source = NumberSource::Env(vars::EXA_GRID_ROWS);
let source = NumberSource::Env(vars.source(vars::EZA_GRID_ROWS, vars::EXA_GRID_ROWS).unwrap());
Err(OptionsError::FailedParse(columns, source, e))
}
}

View file

@ -196,7 +196,7 @@ impl<'a> Render<'a> {
let last_column_count = if the_grid_fits { column_count } else { column_count - 1 };
// If weve figured out how many columns can fit in the users terminal,
// and it turns out there arent enough rows to make it worthwhile
// (according to EXA_GRID_ROWS), then just resort to the lines view.
// (according to EZA_GRID_ROWS), then just resort to the lines view.
if let RowThreshold::MinimumRows(thresh) = self.row_threshold {
if last_working_grid.fit_into_columns(last_column_count).row_count() < thresh {
return None;

View file

@ -94,7 +94,7 @@ impl Definitions {
/// colours into the `ExtensionMappings` that gets returned, and using the
/// two-character UI codes to modify the mutable `Colours`.
///
/// Also returns if the `EXA_COLORS` variable should reset the existing file
/// Also returns if the `EZA_COLORS` variable should reset the existing file
/// type mappings or not. The `reset` code needs to be the first one.
fn parse_color_vars(&self, colours: &mut UiStyles) -> (ExtensionMappings, bool) {
use log::*;
@ -472,7 +472,7 @@ mod customs_test {
test!(ls_ln: ls "ln=34", exa "" => colours c -> { c.filekinds.symlink = Blue.normal(); });
test!(ls_or: ls "or=33", exa "" => colours c -> { c.broken_symlink = Yellow.normal(); });
// EXA_COLORS can affect all those colours too:
// EZA_COLORS can affect all those colours too:
test!(exa_di: ls "", exa "di=32" => colours c -> { c.filekinds.directory = Green.normal(); });
test!(exa_ex: ls "", exa "ex=33" => colours c -> { c.filekinds.executable = Yellow.normal(); });
test!(exa_fi: ls "", exa "fi=34" => colours c -> { c.filekinds.normal = Blue.normal(); });
@ -483,12 +483,12 @@ mod customs_test {
test!(exa_ln: ls "", exa "ln=33" => colours c -> { c.filekinds.symlink = Yellow.normal(); });
test!(exa_or: ls "", exa "or=32" => colours c -> { c.broken_symlink = Green.normal(); });
// EXA_COLORS will even override options from LS_COLORS:
// EZA_COLORS will even override options from LS_COLORS:
test!(ls_exa_di: ls "di=31", exa "di=32" => colours c -> { c.filekinds.directory = Green.normal(); });
test!(ls_exa_ex: ls "ex=32", exa "ex=33" => colours c -> { c.filekinds.executable = Yellow.normal(); });
test!(ls_exa_fi: ls "fi=33", exa "fi=34" => colours c -> { c.filekinds.normal = Blue.normal(); });
// But more importantly, EXA_COLORS has its own, special list of colours:
// But more importantly, EZA_COLORS has its own, special list of colours:
test!(exa_ur: ls "", exa "ur=38;5;100" => colours c -> { c.perms.user_read = Fixed(100).normal(); });
test!(exa_uw: ls "", exa "uw=38;5;101" => colours c -> { c.perms.user_write = Fixed(101).normal(); });
test!(exa_ux: ls "", exa "ux=38;5;102" => colours c -> { c.perms.user_execute_file = Fixed(102).normal(); });
@ -583,7 +583,7 @@ mod customs_test {
test!(exa_mp3: ls "", exa "lev.*=38;5;153" => exts [ ("lev.*", Fixed(153).normal()) ]);
test!(exa_mak: ls "", exa "Cargo.toml=4;32;1" => exts [ ("Cargo.toml", Green.bold().underline()) ]);
// Testing whether a glob from EXA_COLORS overrides a glob from LS_COLORS
// Testing whether a glob from EZA_COLORS overrides a glob from LS_COLORS
// cant be tested here, because theyll both be added to the same vec
// Values get separated by colons:

View file

@ -169,7 +169,7 @@ impl UiStyles {
}
/// Sets a value on this set of colours using one of the keys understood
/// by the `EXA_COLORS` environment variable. Invalid keys set nothing,
/// by the `EZA_COLORS` environment variable. Invalid keys set nothing,
/// but return false. This doesnt take the `LS_COLORS` keys into account,
/// so `set_ls` should have been run first.
pub fn set_exa(&mut self, pair: &Pair<'_>) -> bool {