aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/ls.sh
blob: 1083dfca42087795c505c6d015d37d7395677e46 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Our ~/.profile should already have made a directory with the supported
# options for us; if not, we won't be wrapping ls(1) with a function at all
[ -d "$HOME"/.cache/sh/opt/ls ] || return

# If the system has already aliased ls(1) for us, like Slackware or OpenBSD
# does, just get rid of it
unalias ls >/dev/null 2>&1

# Discard GNU ls(1) environment variables if the environment set them
unset -v LS_OPTIONS LS_COLORS

# Define function proper
ls() {

    # POSIX options:
    ## -F to show trailing indicators of the filetype
    ## -q to replace control chars with '?'
    set -- -Fq "$@"
    ## -x to format entries across, not down, if output looks like a terminal
    if [ -t 1 ] ; then
        set -- -x "$@"
    fi

    # GNU options:
    ## Add --block-size=K to always show the filesize in kibibytes
    if [ -e "$HOME"/.cache/sh/opt/ls/block-size ] ; then
        set -- --block-size=1024 "$@"
    fi
    ## Add --color if the terminal has at least 8 colors
    if [ -e "$HOME"/.cache/sh/opt/ls/color ] &&
        [ "$(exec 2>/dev/null;tput colors||tput Co||echo 0)" -ge 8 ] ; then
        set -- --color=auto "$@"
    fi
    ## Add --time-style='+%Y-%m-%d %H:%M:%S' to show the date in my preferred
    ## (fixed) format
    if [ -e "$HOME"/.cache/sh/opt/ls/time-style ] ; then
        set -- --time-style='+%Y-%m-%d %H:%M:%S' "$@"
    fi

    # If the operating system is FreeBSD, there are some specific options we
    # can add that might mean different things to e.g. GNU ls(1)
    case $OS in
        FreeBSD)
            # -D: Timestamp format
            # -G: Use color
            set -- -D '%Y-%m-%d %H:%M:%S' -G "$@"
            ;;
    esac

    # Run ls(1) with the concluded arguments
    command ls "$@"
}