-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansi.sh
331 lines (278 loc) · 5.47 KB
/
ansi.sh
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#
# Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
# https://kekse.biz/ https://github.com/kekse1/scripts/
# v1.3.1
#
# for the `line()`:
[[ -z "$LINE" ]] && export LINE=",.-'\`'-.,"
[[ -z "$LINE_COLOR" ]] && export LINE_COLOR="auto"
#export LINE=",.-'\`'-.,"
#export LINE_COLOR="auto"
#export LINE_COLOR="255 255 0"
# for the `progress()`:
PROGRESS_DONE=( 230 200 60 )
PROGRESS_TODO=( 30 70 130 )
PROGRESS_OB="("
PROGRESS_CB=")"
#
repeat()
{
local count=$1; shift
local result=''
[[ $count -le 0 ]] && return
local i; for (( i=0; i<$count; ++i )); do
result="${result}${*}"
done
echo "$result"
}
progress()
{
local current="$1"
local total="$2"; [[ -z "$total" || $total -le 0 ]] && total=100
local width="$3"
local prec="$4"
local space="$5"; [[ -z "$space" ]] && space=0
if [[ -z "$prec" ]]; then
prec=0
elif [[ $prec -lt 0 ]]; then
prec=-1
elif [[ $prec -gt 6 ]]; then
prec=6
fi
if [[ -z "$width" ]]; then
width=`width`
elif [[ "${width: -1}" == "%" ]]; then
width=$((`width`*${width::-1}/100))
elif [[ $width -eq 0 ]]; then
width=`width`
elif [[ $width -lt 0 ]]; then
width=$((`width`+${width}))
fi
[[ $width -le 0 ]] && width=${width:1}
width=$(mod $width $((`width`+1)))
[[ $space -gt 0 ]] && width=$((${width}-${space}*2))
if [[ $current -lt 0 ]]; then
current=0
elif [[ $current -gt $total ]]; then
current=$total
fi
local factor="`div $current $total`"
local text=""; local len; if [[ $prec -ge 0 ]]; then
len=$((${prec}+4))
text="$(mul $factor 100)"
text="$(int "$text" $prec)"
text="$(printf "%${len}s" "$text")"
fi
width=$((${width}-${#text}-4))
local done="$(int `mul $factor $width`)"
local todo="$(int `sub $width $done`)"
done="`repeat $done $(bg ${PROGRESS_DONE[@]} ' ')`"
todo="`repeat $todo $(bg ${PROGRESS_TODO[@]} ' ')`"
done="`fg "${PROGRESS_DONE[@]}"``bold "${PROGRESS_OB}"`${done}"
todo="`fg "${PROGRESS_TODO[@]}"`${todo}`fg "${PROGRESS_DONE[@]}"``bold "${PROGRESS_CB}"`"
if [[ -n "$text" ]]; then
text="`bold``info`${text}`error`%`none`"
done="${text} ${done}"
fi
if [[ $space -gt 0 ]]; then
done="`repeat $space ' '`${done}"
todo="${todo}`repeat $space ' '`"
fi
echo "${done}${todo}`none`"
}
int()
{
local value="$1"; local scale="$2"
[[ -z "$scale" ]] && scale=0
echo "scale=${scale}; (${value}/1)" | bc
}
add()
{
echo "$(awk "BEGIN {print ($1)+($2)}")"
}
sub()
{
echo "$(awk "BEGIN {print ($1)-($2)}")"
}
mul()
{
echo "$(awk "BEGIN {print ($1)*($2)}")"
}
div()
{
echo "$(awk "BEGIN {print ($1)/($2)}")"
}
mod()
{
echo "$(awk "BEGIN {print ($1)%($2)}")"
}
#
line()
{
local _line="$1"; [[ -z "$_line" ]] && _line="$LINE"; [[ -z "$_line" ]] && _line=",.-'\`'-.,"
local _string="$2"
local _start="$3"; [[ -z "$_start" ]] && _start=6
local _space="$4"; [[ -z "$_space" ]] && _space=" "
_string="${_space}${_string}${_space}"
local w=`width`; if [[ $w -eq 0 ]]; then echo; return; fi
local lineColor="$LINE_COLOR"; [[ -n "$lineColor" && "$lineColor" != "auto" ]] && IFS=' ' lineColor=( $lineColor )
[[ $lineColor != "auto" ]] && fg "${lineColor[@]}"
local i; local char; local mod; for (( i=0; i<$w; ++i )); do
if [[ -n "$_string" && $i -ge $_start ]]; then
char="${_string::1}"
_string="${_string:1}"
none
else
mod=$(($i%${#_line}));
if [[ $lineColor == "auto" ]]; then
fg $(($RANDOM%256)) $(($RANDOM%256)) $(($RANDOM%256))
else
fg ${LINE_COLOR[@]}
fi
char="${_line:${mod}:1}"
fi
echo -n "$char"
done; echo -e "`none`"
}
width()
{
if [[ -n "$COLS" ]]; then
echo $COLS
elif [[ -n "$COLUMNS" ]]; then
echo $COLUMNS
else
tput cols 2>/dev/null
if [[ $? -ne 0 ]]; then
echo 0
return 1
fi
fi
}
height()
{
if [[ -n "$LINES" ]]; then
echo $LINES
else
tput lines 2>/dev/null
if [[ $? -ne 0 ]]; then
echo 0
return 1
fi
fi
}
#
none()
{
echo -en "\e[0m"
}
#
fg()
{
echo -en "\e[38;2;$1;$2;$3m"
[[ -n "$4" ]] && echo -en "$4\e[39m"
}
bg()
{
echo -en "\e[48;2;$1;$2;$3m"
[[ -n "$4" ]] && echo -en "$4\e[49m"
}
info()
{
fg 173 234 68 "$*"
}
warn()
{
fg 232 226 37 "$*"
}
error()
{
fg 252 131 38 "$*"
}
debug()
{
fg 150 180 200 "$*"
}
bold()
{
echo -en "\e[1m"
[[ -n "$*" ]] && echo -en "$*\e[22m"
}
underline()
{
echo -en "\e[4m"
[[ -n "$*" ]] && echo -en "$*\e[24m"
}
italic()
{
echo -en "\e[3m"
[[ -n "$*" ]] && echo -en "$*\e[23m"
}
faint()
{
echo -en "\e[2m"
[[ -n "$*" ]] && echo -en "$*\e[22m"
}
slow()
{
echo -en "\e[5m"
[[ -n "$*" ]] && echo -en "$*\e[25m"
}
fast()
{
echo -en "\e[6m"
[[ -n "$*" ]] && echo -en "$*\e[26m"
}
inverse()
{
echo -en "\e[7m"
[[ -n "$*" ]] && echo -en "$*\e[27m"
}
hidden()
{
echo -en "\e[8m"
[[ -n "$*" ]] && echo -en "$*\e[28m"
}
strike()
{
echo -en "\e[9m"
[[ -n "$*" ]] && echo -en "$*\e[29m"
}
#
clearLine()
{
echo -en "\e[2K"
}
#
SOURCE()
{
local _str="Unable to \`source\`"
if [[ -n "$1" ]]; then
_str="${_str} the file `warn "$1"`"
else
_str="${_str} a required file"
fi
ERROR "$_str"
[[ -n "$2" ]] && exit $2
exit 123
}
ERROR()
{
echo -en " `faint`[`none``error``inverse`ERROR`none``faint`]`none`" >&2
[[ -n "$*" ]] && echo -e " `error`${*}`none`" >&2
}
WARN()
{
echo -en "`faint`[`none``warn``inverse`WARNING`none``faint`]`none`" >&2
[[ -n "$*" ]] && echo -e " `warn`${*}`none`" >&2
}
DEBUG()
{
echo -en " `faint`[`none``debug``inverse`JFYI`none``faint`]`none`" >&2
[[ -n "$*" ]] && echo -e " `debug`${*}`none`" >&2
}
INFO()
{
echo -en " `faint`[`none``info``inverse`INFO`none``faint`]`none`"
[[ -n "$*" ]] && echo -e " `info`${*}`none`"
}
#