-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathansi-tput.sh
More file actions
263 lines (219 loc) · 7.01 KB
/
ansi-tput.sh
File metadata and controls
263 lines (219 loc) · 7.01 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env bash
if type ansi::fg > /dev/null 2>&1; then return; fi
set -euo pipefail
if [[ -z "$BASH_VERSION" ]]; then
echo "This script requires bash."
exit 1
fi
if [[ "$BASH_VERSION" < "4.3" ]]; then
echo "This script requires bash version 4.3 or higher."
if [[ "$(uname -s)" == "Darwin"* ]]; then
echo "You can install it with Homebrew: brew install bash"
fi
exit 1
fi
_load_dependencies() {
local script_dir=${BASH_SOURCE:-$0}
script_dir="$(dirname "$(realpath "$script_dir")")"
source "${script_dir}/ansi-utils.sh"
}
if ! type ansi::out >/dev/null 2>&1; then
_load_dependencies
fi
unset -f _load_dependencies
declare -g -A ansi_tput_colors=(
[black]=0
[red]=1
[green]=2
[yellow]=3
[blue]=4
[magenta]=5
[cyan]=6
[white]=7
)
declare -g -a ansi_tput_text_styles=(
bold dim italic underline blink reverse hidden strike standout
reset_bold reset_dim reset_italic reset_underline reset_blink
reset_reverse reset_hidden reset_strike reset_standout
reset
)
declare -g -A ansi_tput_controls=(
# default colors
[fg_default]='setaf 9'
[bg_default]='setab 9'
# turn on_styles
[text_bold]='bold'
[text_dim]='dim'
[text_italic]='sitm'
[text_underline]='smul'
[text_blink]='blink'
[text_reverse]='rev'
[text_hidden]='invis'
[text_strike]='smxx'
[text_standout]='smso'
# turn off_styles
[text_reset_bold]='sgr 0 0 0 0 0 1 0 0 0'
[text_reset_dim]='sgr 0 0 0 0 1 0 0 0 0'
[text_reset_italic]='ritm'
[text_reset_underline]='rmul'
[text_reset_blink]='sgr 0 0 0 1 0 0 0 0 0'
[text_reset_reverse]='sgr 0 0 1 0 0 0 0 0 0'
[text_reset_hidden]='sgr 0 0 0 0 0 0 1 0 0'
[text_reset_strike]='rmxx'
[text_reset_standout]='rmso'
[text_reset]='sgr0'
# cursor operations
[cursor_up1]='cuu1'
[cursor_down1]='cud1'
[cursor_left1]='cub1'
[cursor_right1]='cuf1'
[cursor_home]='home'
[cursor_save]='sc'
[cursor_restore]='rc'
[cursor_hide]='civis'
[cursor_show]='cnorm'
# [cursor_last]='ll'
# screen operations
[screen_save]='smcup'
[screen_restore]='rmcup'
# clear operations
[clear_bol]='el1'
[clear_eol]='el'
[clear_eos]='ed'
[clear_screen]='clear'
)
declare -g -A ansi_tput_color_codes=()
ansi::tput::populate_colors() {
for color in "${!ansi_tput_colors[@]}"; do
ansi_tput_color_codes["fg_${color}"]="setaf ${ansi_tput_colors[$color]}"
ansi_tput_color_codes["bg_${color}"]="setab ${ansi_tput_colors[$color]}"
local bright_color=$((${ansi_tput_colors[$color]} + 8))
ansi_tput_color_codes["fg_bright_${color}"]="setaf $bright_color"
ansi_tput_color_codes["bg_bright_${color}"]="setab $bright_color"
done
ansi_tput_color_codes["fg_bg_reset"]="op"
}
ansi::tput::populate_colors
unset -f ansi::tput::populate_colors
ansi_tput_longname="$(tput longname)"
ansi::tput::collect_codes() {
local -n assoc="$1"
local prefix="${2:-}"
prefix="${prefix^^}"
local names=()
local string=""
for name in "${!assoc[@]}"; do
names+=("$name")
string+="${assoc[$name]}\nlongname\n"
done
local result="$(tput -S <<< "$(echo -e "$string")" || true)"
if [[ -z "$result" ]]; then return 1; fi
local delimiter="${ansi_tput_longname}"
local replacement=$'\x01'
local escaped_result="${result//'\\'/'\\\\'}"
local modified_result="${escaped_result//$delimiter/$replacement}"
local -a values
set +e
IFS="$replacement" read -rd '' -a values <<< "$modified_result" # Split into an array
set -e
if [[ ${#values[@]} -ne $((${#names[@]} + 1)) ]]; then return 1; fi
for i in "${!names[@]}"; do
echo "${prefix}${names[$i]^^}=${values[$i]@Q}"
done
}
ansi::tput::define_colors() {
local prefix="${1:-}"
prefix="${prefix^^}"
local fast_result="$(ansi::tput::collect_codes ansi_tput_color_codes)"
if [[ -n "$fast_result" ]]; then
echo "$fast_result"
return
fi
for color in "${!ansi_tput_colors[@]}"; do
local code=$(tput setaf ${ansi_tput_colors[$color]} || true)
if [[ -n "$code" ]]; then echo "${prefix}FG_${color^^}=${code@Q}"; fi
local code=$(tput setaf $((${ansi_tput_colors[$color]} + 8)) || true)
if [[ -n "$code" ]]; then echo "${prefix}FG_BRIGHT_${color^^}=${code@Q}"; fi
local code=$(tput setab ${ansi_tput_colors[$color]} || true)
if [[ -n "$code" ]]; then echo "${prefix}BG_${color^^}=${code@Q}"; fi
local code=$(tput setab $((${ansi_tput_colors[$color]} + 8)) || true)
if [[ -n "$code" ]]; then echo "${prefix}BG_BRIGHT_${color^^}=${code@Q}"; fi
done
local code=$(tput op || true)
echo "${prefix}FG_BG_RESET=${code@Q}"
}
ansi::tput::alias_simple_color_names() {
local prefix="${1:-}"
prefix="${prefix^^}"
for color in "${!ansi_tput_colors[@]}"; do
echo "${prefix}${color^^}=\"\${${prefix}FG_${color^^}:-}\""
echo "${prefix}BRIGHT_${color^^}=\"\${${prefix}FG_BRIGHT_${color^^}:-}\""
done
}
ansi::tput::define_controls() {
local prefix="${1:-}"
prefix="${prefix^^}"
local fast_result="$(ansi::tput::collect_codes ansi_tput_controls)"
if [[ -n "$fast_result" ]]; then
echo "$fast_result"
echo "${prefix}TEXT_RESET_ALL=\"\${${prefix}FG_BG_RESET}\${${prefix}TEXT_RESET}\""
return
fi
for style in "${!ansi_tput_controls[@]}"; do
local code=$(tput ${ansi_tput_controls[$style]} || true)
if [[ -n "$code" ]]; then echo "${prefix}${style^^}=${code@Q}"; fi
done
echo "${prefix}TEXT_RESET_ALL=\"\${${prefix}FG_BG_RESET}\${${prefix}TEXT_RESET}\""
}
ansi::tput::alias_simple_style_names() {
local prefix="${1:-}"
prefix="${prefix^^}"
for name in "${ansi_tput_text_styles[@]}"; do
echo "${prefix}${name^^}=\"\${${prefix}TEXT_${name^^}:-}\""
done
echo "${prefix}RESET_ALL=\"\${${prefix}TEXT_RESET_ALL:-}\""
}
ansi::alias_simple_command_names() {
echo "$(ansi::tput::alias_simple_color_names "$@")"
echo "$(ansi::tput::alias_simple_style_names "$@")"
}
ansi::get() {
local name="$1"
local var_name="${name^^}"
if [[ -v "${var_name}" ]]; then
echo "${!var_name}"
return 0
fi
var_name="TEXT_${name^^}"
if [[ -v "${var_name}" ]]; then
echo "${!var_name}"
return 0
fi
var_name="FG_${name^^}"
if [[ -v "${var_name}" ]]; then
echo "${!var_name}"
return 0
fi
}
ansi::fg() { tput setaf "$1" || true; }
ansi::bg() { tput setab "$1" || true; }
# params (bool): standout, underline, reverse, blink, dim, bold, invis, protect, altcharset
ansi::tput::sgr() { tput sgr "$@" || true; }
ansi::cursor::pos() { tput cup "$1" "$2" || true; }
ansi::cursor::left() { tput cub "$1" || true; }
ansi::cursor::right() { tput cuf "$1" || true; }
ansi::cursor::insert() { tput ich "$1" || true; }
ansi::cursor::insert_lines() { tput il "$1" || true; }
ansi::screen::lines() { tput lines || true; }
ansi::screen::cols() { tput cols || true; }
ansi::screen::colors() { tput colors || true; }
ansi::terminal::name() { tput longname || true; }
ansi_tput_simple_names=""
if [ -z "${ANSI_NO_SIMPLE_COMMAND_NAMES:-}" ]; then
ansi_tput_simple_names="$(ansi::alias_simple_command_names)"
fi
eval "$(printf '%s\n%s\n%s\n' \
"$(ansi::tput::define_colors)" \
"$(ansi::tput::define_controls)" \
"${ansi_tput_simple_names}")"
unset ansi_tput_simple_names