Merge pull request #438 from hehelego/main

fix: query stdout terminal size to see if the output gose to a tty.
This commit is contained in:
Christina Sørensen 2023-10-02 13:04:38 +02:00 committed by GitHub
commit 6e9d53f320
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 11 deletions

1
Cargo.lock generated
View file

@ -380,6 +380,7 @@ dependencies = [
"trycmd",
"unicode-width",
"uzers",
"windows-sys",
"zoneinfo_compiled",
]

View file

@ -101,6 +101,9 @@ proc-mounts = "0.3"
[target.'cfg(unix)'.dependencies]
uzers = "0.11.3"
[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = "0.48.0"
[build-dependencies]
chrono = { version = "0.4.31", default-features = false, features = ["clock"] }

View file

@ -342,7 +342,7 @@ These options are available when running with `--long` (`-l`):
Some of the options accept parameters:
- Valid **--color** options are **always**, **automatic**, and **never**.
- Valid **--co{u}lor** options are **always**, **automatic** (or **auto** for short), and **never**.
- Valid sort fields are **accessed**, **changed**, **created**, **extension**, **Extension**, **inode**, **modified**, **name**, **Name**, **size**, **type**, and **none**. Fields starting with a capital letter sort uppercase before lowercase. The modified field has the aliases **date**, **time**, and **newest**, while its reverse has the aliases **age** and **oldest**.
- Valid time fields are **modified**, **changed**, **accessed**, and **created**.
- Valid time styles are **default**, **iso**, **long-iso**, **full-iso**, and **relative**.

View file

@ -9,7 +9,7 @@ _eza() {
;;
--colour)
mapfile -t COMPREPLY < <(compgen -W 'always auto never' -- "$cur")
mapfile -t COMPREPLY < <(compgen -W 'always automatic auto never' -- "$cur")
return
;;

View file

@ -14,6 +14,7 @@ complete -c eza -l color \
-l colour -d "When to use terminal colours" -x -a "
always\t'Always use colour'
auto\t'Use colour if standard output is a terminal'
automatic\t'Use colour if standard output is a terminal'
never\t'Never use colour'
"
complete -c eza -l color-scale \

View file

@ -19,7 +19,7 @@ __eza() {
{-R,--recurse}"[Recurse into directories]" \
{-T,--tree}"[Recurse into directories as a tree]" \
{-F,--classify}"[Display type indicator by file names]" \
--colo{,u}r="[When to use terminal colours]:(when):(always auto never)" \
--colo{,u}r="[When to use terminal colours]:(when):(always auto automatic never)" \
--colo{,u}r-scale"[Highlight levels of file sizes distinctly]" \
--icons"[Display icons]" \
--no-icons"[Hide icons]" \

View file

@ -62,9 +62,15 @@ DISPLAY OPTIONS
`-x`, `--across`
: Sort the grid across, rather than downwards.
`--color`, `--colour=WHEN`
: When to use terminal colours.
Valid settings are `always`, `automatic`, and `never`.
`--color=WHEN`, `--colour=WHEN`
: When to use terminal colours (using ANSI escape code to colorize the output).
Valid settings are `always`, `automatic` (or `auto` for short), and `never`.
The default value is `automatic`.
The default behavior (`automatic` or `auto`) is to colorize the output only when the standard output is connected to a real terminal. If the output of `eza` is redirected to a file or piped into another program, terminal colors will not be used. Setting this option to `always` causes `eza` to always output terminal color, while `never` disables the use of terminal color.
Manually setting this option overrides `NO_COLOR` environment.
`--color-scale`, `--colour-scale`
: Colour file sizes on a scale.

View file

@ -23,7 +23,7 @@
use std::env;
use std::ffi::{OsStr, OsString};
use std::io::{self, ErrorKind, Write};
use std::io::{self, ErrorKind, IsTerminal, Write};
use std::path::{Component, PathBuf};
use std::process::exit;
@ -58,6 +58,8 @@ fn main() {
warn!("Failed to enable ANSI support: {}", e);
}
let stdout_istty = io::stdout().is_terminal();
let args: Vec<_> = env::args_os().skip(1).collect();
match Options::parse(args.iter().map(std::convert::AsRef::as_ref), &LiveVars) {
OptionsResult::Ok(options, mut input_paths) => {
@ -71,9 +73,7 @@ fn main() {
let writer = io::stdout();
let console_width = options.view.width.actual_terminal_width();
let theme = options
.theme
.to_theme(terminal_size::terminal_size().is_some());
let theme = options.theme.to_theme(stdout_istty);
let exa = Exa {
options,
writer,

View file

@ -50,10 +50,26 @@ impl TerminalWidth {
// terminal, but were only interested in stdout because its
// where the output goes.
#[cfg(unix)]
let stdout_term_width = {
use std::os::fd::AsRawFd;
terminal_size::terminal_size_using_fd(std::io::stdout().as_raw_fd())
.map(|(w, _h)| w.0 as _)
};
#[cfg(windows)]
let stdout_term_width = {
use std::os::windows::io::RawHandle;
use windows_sys::Win32::System::Console::{GetStdHandle, STD_OUTPUT_HANDLE};
terminal_size::terminal_size_using_handle(unsafe {
GetStdHandle(STD_OUTPUT_HANDLE) as RawHandle
})
.map(|(w, _h)| w.0 as _)
};
#[rustfmt::skip]
return match self {
Self::Set(width) => Some(width),
Self::Automatic => terminal_size::terminal_size().map(|(w, _)| w.0.into()),
Self::Automatic => stdout_term_width,
};
}
}