summaryrefslogtreecommitdiffstats
path: root/remote/lib/print.sh
blob: a0fdcbe490f4a26382d2c3922b0296a7cd9cd317 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# underlines    4;30,...        blinking 5:30,...
# backgrounds:  40,...

declare -A colors=(
    [black]='0;30'
    [red]='0;31'
    [green]='0;32'
    [orange]='0;33'
    [blue]='0;34'
    [purple]='0;35'
    [cyan]='0;36'
    [lightgray]='0;37'
    [gray]='1;30'
    [lightred]='1;31'
    [lightgreen]='1;32'
    [yellow]='1;33'
    [lightblue]='1;34'
    [lightpurple]='1;35'
    [lightcyan]='1;36'
    [white]='1;37'
)

background=""
foreground=""




# Fixing color map
#echo -ne "\e[44m"
#echo -ne '\x1B[K'

function set_background {
    if [ -n "$background" ]; then
        echo -ne "$background"
        echo -ne '\x1B[K'
    fi
}

function set_color {
    local color=${1:-""}
    local effect=${2:-""}
    
    c=${colors[$color]} 

    
    case "$effect" in
      'u')
        c=$(echo $c | sed -r "s/^0/4/ ; s/^1/1;4/")
        ;;
    esac
    
    [ -n "$color" ] && echo -ne "\033[${c}m"
    set_background
}

function reset_color {
    echo -ne "\033[0m"

    set_background
    [ -n "$foreground" ] && set_color "$foreground"
}

function finish {
    echo -ne "\033[0m"
    echo
}

function configure_palete {
    case $1 in
      blue)
        background="\e[1;44m"
        foreground="white"
        ;;
      *)
        # Lightgray and Yellow are almost invisible on white
        colors[yellow]=${colors[orange]}
        colors[white]=${colors[black]}
        colors[lightgray]=${colors[gray]}
        colors[cyan]=${colors[blue]}
        ;;
    esac
    
    set_background
    reset_color
}

function decorate {
    local output="$1"
    local color=${2:-""}
    local effect=${3:-""}
    
    if [ -n "$color" ]; then
        set_color $color $effect
        echo -ne "${output}"
        reset_color
    else
        echo -ne "$output"
    fi
}

function eol {
    sed 's/$/\x1B[K/'
}

function print_eol {
    echo -e '\x1B[K'
}



function print {
    local output="$1"
    local color=${2:-""}
    local char_limit=${3:-$fold}
    local line_limit=${4:-0}
    
    local cmd="decorate \"${output}\" \"$color\" | fold -s -w $char_limit"
    [ $line_limit -gt 0 ] && cmd="$cmd | head -n $line_limit"

    eval "$cmd" | eol
}


function print_hline {
    local color=${1:-"gray"}
    local cols=${COLUMNS:-$(tput cols)}

    decorate $(printf '%*s' $cols '' | tr ' ' -) "$color" 
    echo | eol
}

function print_table {
    # The problem here is that all escapes for colors are treated as normal visible symbols
    sed "s/ ::: /@/g" | column -t -s "@" -o "    " | eol
}

function decorate_table {
    print_hline
    cat -
    print_hline
}

function print_status {
    local status="$1"

    if [[ $status =~ ^.$ ]]; then
        case $status in
          0)
            decorate "*" "red"
            ;;
          1)
            decorate "*" "green"
            ;;
          2)
            decorate "*" "yellow"
            ;;
          *)
            decorate "$status" "red"
            ;;
        esac    
    else
        decorate "?" "red"
    fi
    echo -n " "
}

clear