aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ls.bash
blob: 0dedfdccd9b20d32afb31924dbe074b4837db674 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Return appropriate options for ls
lsopts() {

    # Snarf the output of `ls --help` into a variable
    local lshelp=$(ls --help 2>/dev/null)

    # Start collecting available options
    local -a lsopts

    # If the --color option is available and we have a terminal that supports
    # at least eight colors, add --color=auto to the options
    local colors=$(tput colors)
    if [[ $lshelp == *--color* ]] && ((colors >= 8)) ; then
        lsopts=("${lsopts[@]}" '--color=auto')
    fi

    # Print the options as a single string, space-delimited
    printf %s "${lsopts[*]}"
}

# Alias ls with these options
alias ls="ls $(lsopts)"

# Unset helper function
unset -f lsopts