From 488e208cca9da3e6ecb07a6bfe13646805db59ed Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Mon, 21 Jan 2019 06:18:17 -0800 Subject: [PATCH] ls.fish: also show indicators on non-GNU ls, refactor GNU ls's --indicator-style=classify is the same as POSIX -F. Refactor and change command testing logic so that we define the function in the same place for all platforms, and use -F on all the platforms when stdout is a TTY. --- share/functions/ls.fish | 46 ++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/share/functions/ls.fish b/share/functions/ls.fish index d162c8c44..1cd4e659f 100644 --- a/share/functions/ls.fish +++ b/share/functions/ls.fish @@ -1,20 +1,22 @@ # -# Make ls use colors if we are on a system that supports that feature and writing to stdout. +# Make ls use colors and show indicators if we are on a system that supports that feature and writing to stdout. # -if command ls --version >/dev/null 2>/dev/null - # This appears to be GNU ls. - function ls --description "List contents of directory" - set -l param --color=auto - if isatty 1 - set -a param --indicator-style=classify + +# BSD, macOS and others support colors with ls -G. +# 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. +# Solaris 11's ls command takes a --color flag. +# Also test a no-op -- because we'll want to define this function even with an ls that can't do colors (like NetBSD). + +for opt in --color=auto -G --color -- + if command ls $opt / >/dev/null 2>/dev/null + + function ls --description "List contents of directory" -V opt + isatty stdout + and set -a opt -F + command ls $opt $argv end - command ls $param $argv - end - if not set -q LS_COLORS - set -l dircolors (command -s {g,}dircolors)[1] - - if set -q dircolors[1] + if [ $opt = --color=auto ] &&! set -qx LS_COLORS && set -l cmd (command -s {g,}dircolors)[1] set -l colorfile for file in ~/.dir_colors ~/.dircolors /etc/DIR_COLORS if test -f $file @@ -22,23 +24,15 @@ if command ls --version >/dev/null 2>/dev/null break end end - # Here we rely on the legacy behavior of `dircolors -c` producing output suitable for - # csh in order to extract just the data we're interested in. - set -gx LS_COLORS ($dircolors -c $colorfile | string split ' ')[3] + # Here we rely on the legacy behavior of `dircolors -c` producing output + # suitable for csh in order to extract just the data we're interested in. + set -gx LS_COLORS ($cmd -c $colorfile | string split ' ')[3] # The value should always be quoted but be conservative and check first. if string match -qr '^([\'"]).*\1$' -- $LS_COLORS set LS_COLORS (string match -r '^.(.*).$' $LS_COLORS)[2] end end - end -else if command ls -G / >/dev/null 2>/dev/null - # It looks like BSD, OS X and a few more which support colors through the -G switch instead. - function ls --description "List contents of directory" - command ls -G $argv - end -else if command ls --color / >/dev/null 2>/dev/null - # Solaris 11's ls command takes a --color flag - function ls --description "List contents of directory" - command ls --color $argv + + break end end