fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerLink terminal workarounds with comments02a6afd2b

master
2.5 KiB59 linesraw
1# localization: skip(uses-apropos)
2function ls
3    # Make ls use colors and show indicators if we are on a system that supports that feature and writing to stdout.
4    #
5
6    # BSD, macOS and others support colors with ls -G.
7    # GNU ls and FreeBSD ls takes --color=auto. Order of this test is important because ls also takes -G but it has a different meaning.
8    # Solaris 11's ls command takes a --color flag.
9    # OpenBSD requires the separate colorls program for color support.
10    # Also test -F because we'll want to define this function even with an ls that can't do colors (like NetBSD).
11    if not set -q __fish_ls_command
12        set -g __fish_ls_command ls
13        set -g __fish_ls_color_opt
14        set -g __fish_ls_indicators_opt
15        # OpenBSD ships a command called "colorls" that takes "-G" and "-F",
16        # but there's also a ruby implementation that doesn't understand "-F".
17        # Since that one's quite different, don't use it.
18        if command -sq colorls
19            and command colorls -GF >/dev/null 2>/dev/null
20            set -g __fish_ls_command colorls
21            set -g __fish_ls_color_opt -G
22            set -g __fish_ls_indicators_opt -F
23        else
24            for opt in --color=auto -G --color
25                if command ls $opt / >/dev/null 2>/dev/null
26                    set -g __fish_ls_color_opt $opt
27                    break
28                end
29            end
30
31            if command ls -F / >/dev/null 2>/dev/null
32                set -g __fish_ls_indicators_opt -F
33            end
34        end
35    end
36
37    set -l indicators_opt
38    isatty stdout
39    and set -a indicators_opt $__fish_ls_indicators_opt
40
41    # TODO(term-workaround)
42    # Terminal.app doesn't set $COLORTERM or $CLICOLOR,
43    # but the new FreeBSD ls requires either to be set,
44    # before it will enable color.
45    # See #8309.
46    # We don't set $COLORTERM because that should be set to
47    # "truecolor" or similar and we don't want to specify that here.
48    test "$TERM_PROGRAM" = Apple_Terminal
49    and set -lx CLICOLOR 1
50
51    # If CLICOLOR_FORCE is set, don't colorize `ls` (if piped) because the results
52    # might not be what we want; i.e. `ls --color=auto | cat` might still emit color
53    # output (e.g. under BSD and macOS).
54    # We don't just unset CLICOLOR_FORCE because the user might theoretically *want*
55    # this behavior by explicitly including `--color=auto` in $argv themselves.
56    set -qx CLICOLOR_FORCE && not isatty stdout; and set __fish_ls_color_opt
57
58    command $__fish_ls_command $__fish_ls_color_opt $indicators_opt $argv
59end