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).
This commit is contained in:
Mahmoud Al-Qudsi 2024-05-23 20:40:10 -05:00
parent 6d8b4214d5
commit 537cde0c72

View File

@ -46,5 +46,12 @@ function ls --description "List contents of directory"
test "$TERM_PROGRAM" = Apple_Terminal test "$TERM_PROGRAM" = Apple_Terminal
and set -lx CLICOLOR 1 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 command $__fish_ls_command $__fish_ls_color_opt $indicators_opt $argv
end end