aboutsummaryrefslogtreecommitdiff
path: root/modules/lib-bash/color-echo.sh
blob: ef708b29c4db3567edd03ef9518575e9ee9fc347 (plain)
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
# The check for terminal output and color support is heavily inspired
# by https://unix.stackexchange.com/a/10065.

function setupColors() {
    normalColor=""
    errorColor=""
    warnColor=""
    noteColor=""

    # Check if stdout is a terminal.
    if [[ -t 1 ]]; then
        # See if it supports colors.
        local ncolors
        ncolors=$(tput colors)

        if [[ -n "$ncolors" && "$ncolors" -ge 8 ]]; then
            normalColor="$(tput sgr0)"
            errorColor="$(tput bold)$(tput setaf 1)"
            warnColor="$(tput setaf 3)"
            noteColor="$(tput bold)$(tput setaf 6)"
        fi
    fi
}

setupColors

function errorEcho() {
    echo "${errorColor}$*${normalColor}"
}

function warnEcho() {
    echo "${warnColor}$*${normalColor}"
}

function noteEcho() {
    echo "${noteColor}$*${normalColor}"
}