Просмотр исходного кода

Merge pull request #438 from hehelego/main

fix: query stdout terminal size to see if the output gose to a tty.
Christina Sørensen 2 лет назад
Родитель
Сommit
6e9d53f320
9 измененных файлов с 38 добавлено и 11 удалено
  1. 1 0
      Cargo.lock
  2. 3 0
      Cargo.toml
  3. 1 1
      README.md
  4. 1 1
      completions/bash/eza
  5. 1 0
      completions/fish/eza.fish
  6. 1 1
      completions/zsh/_eza
  7. 9 3
      man/eza.1.md
  8. 4 4
      src/main.rs
  9. 17 1
      src/output/mod.rs

+ 1 - 0
Cargo.lock

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

+ 3 - 0
Cargo.toml

@@ -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"] }
 

+ 1 - 1
README.md

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

+ 1 - 1
completions/bash/eza

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

+ 1 - 0
completions/fish/eza.fish

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

+ 1 - 1
completions/zsh/_eza

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

+ 9 - 3
man/eza.1.md

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

+ 4 - 4
src/main.rs

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

+ 17 - 1
src/output/mod.rs

@@ -50,10 +50,26 @@ impl TerminalWidth {
         // terminal, but we’re only interested in stdout because it’s
         // 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,
         };
     }
 }