|
2 | 2 | ## @brief Color terminal support.
|
3 | 3 | ## @author Vasiliy Polyakov
|
4 | 4 | ## @date 2019
|
5 |
| -## @pre lib.bash (Bash scripting library). |
6 |
| -## @pre ncurses (tput). |
| 5 | +## @pre lib.bash (Bash scripting library). |
| 6 | +## @pre ncurses (tput). |
7 | 7 |
|
8 | 8 | import_once || return $?
|
9 | 9 |
|
10 |
| -declare -gi COLORS="$(tput colors)" |
| 10 | +declare -gi COLORS |
11 | 11 | declare -ga FG BG
|
12 |
| -declare -gr SGR0=$'\e[m' |
| 12 | +declare -gA SGR |
13 | 13 |
|
14 |
| -local -i c |
15 |
| -for (( c = 0; c < 8 && COLORS >= 8; c++ )); do |
16 |
| - FG[c]=$'\e'"[$((30+c))m" |
17 |
| - BG[c]=$'\e'"[$((40+c))m" |
18 |
| -done |
19 |
| -for (( c = 8; c < 16 && COLORS >= 16; c++ )); do |
20 |
| - FG[c]=$'\e'"[$((82+c))m" |
21 |
| - BG[c]=$'\e'"[$((92+c))m" |
22 |
| -done |
23 |
| -for (( c = 16; c < 256 && COLORS >= 256; c++ )); do |
24 |
| - FG[c]=$'\e'"[38;5;${c}m" |
25 |
| - BG[c]=$'\e'"[48;5;${c}m" |
26 |
| -done |
| 14 | +local -i terminfo=0 c |
| 15 | +case $1 in |
| 16 | + 0) declare -r COLORS FG BG SGR; return 0 ;; |
| 17 | + terminfo) terminfo=1 ;; |
| 18 | +esac |
| 19 | + |
| 20 | +COLORS="$(tput colors)" |
| 21 | + |
| 22 | +if (( terminfo )); then |
| 23 | + for (( c = 0; c < COLORS; c++ )); do |
| 24 | + FG[c]="$(tput setaf "$c")" |
| 25 | + BG[c]="$(tput setab "$c")" |
| 26 | + done |
| 27 | + SGR[bold]="$(tput bold)" |
| 28 | + SGR[dim]="$(tput dim)" |
| 29 | + SGR[italics]="$(tput sitm)" |
| 30 | + SGR[uline]="$(tput smul)" |
| 31 | + SGR[blink]="$(tput blink)" |
| 32 | + SGR[reverse]="$(tput rev)" |
| 33 | + SGR[reset]="$(tput sgr0)" |
| 34 | +else |
| 35 | + local CSI=$'\e[' |
| 36 | + for (( c = 0; c < 8 && COLORS >= 8; c++ )); do |
| 37 | + FG[c]="${CSI}$((30+c))m" |
| 38 | + BG[c]="${CSI}$((40+c))m" |
| 39 | + done |
| 40 | + for (( c = 8; c < 16 && COLORS >= 16; c++ )); do |
| 41 | + FG[c]="${CSI}$((82+c))m" |
| 42 | + BG[c]="${CSI}$((92+c))m" |
| 43 | + done |
| 44 | + for (( c = 16; c < 256 && COLORS >= 256; c++ )); do |
| 45 | + FG[c]="${CSI}38;5;${c}m" |
| 46 | + BG[c]="${CSI}48;5;${c}m" |
| 47 | + done |
| 48 | + SGR[bold]="${CSI}1m" |
| 49 | + SGR[dim]="${CSI}2m" |
| 50 | + SGR[italics]="${CSI}3m" |
| 51 | + SGR[uline]="${CSI}4m" |
| 52 | + SGR[blink]="${CSI}5m" |
| 53 | + SGR[reverse]="${CSI}7m" |
| 54 | + SGR[reset]="${CSI}m" |
| 55 | + unset CSI |
| 56 | +fi; unset terminfo c |
| 57 | +# TODO: add one-letter and digit aliases for SGR |
| 58 | +SGR[rev]="${SGR[reverse]}" |
| 59 | +SGR[exit]="${SGR[reset]}" |
| 60 | +SGR[0]="${SGR[reset]}" |
| 61 | + |
| 62 | +declare -r COLORS FG BG SGR |
0 commit comments