From 537cde0c72071e4adfeec2d103f394ab1889c8cf Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 23 May 2024 20:40:10 -0500 Subject: [PATCH] Avoid auto-colorization of ls output if CLICOLOR_FORCE is set We don't set this variable ourselves, but some might set it in their config out of habit coming from shells that don't automatically colorize ls output. This variable overrides stdout tty detection for `ls --color=auto` (but does not modify the behavior of `ls --color=never` or `ls --color=always` regardless of its value) under at least the BSD version of `ls`. (Under the GNU version, it influences colorization only if stdout *is* a tty.) If we detect CLICOLOR_FORCE *and* we are not writing directly to the tty, we skip colorization (by clearing-but-not-erasing `$__fish_ls_color_opt`, so that we don't end up accidentally using its value from another scope). --- share/functions/ls.fish | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/share/functions/ls.fish b/share/functions/ls.fish index 7cb16c625..eeff35f69 100644 --- a/share/functions/ls.fish +++ b/share/functions/ls.fish @@ -46,5 +46,12 @@ function ls --description "List contents of directory" test "$TERM_PROGRAM" = Apple_Terminal and set -lx CLICOLOR 1 + # If CLICOLOR_FORCE is set, don't colorize `ls` (if piped) because the results + # might not be what we want; i.e. `ls --color=auto | cat` might still emit color + # output (e.g. under BSD and macOS). + # We don't just unset CLICOLOR_FORCE because the user might theoretically *want* + # this behavior by explicitly including `--color=auto` in $argv themselves. + set -qx CLICOLOR_FORCE && not isatty stdout; and set __fish_ls_color_opt + command $__fish_ls_command $__fish_ls_color_opt $indicators_opt $argv end