on master help sign in
utils/lib.sh: colorize messages

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commit56a93ee77006499b869b2fa067b72b0171710c61 authorMarkus Heiser <markus.heiser@darmarit.de> dateFri 31 Jan 2020, 18:24 (7 years ago) parent1e3d2f6
3 files changed+66−28
utils/filtron.sh+4−2 utils/lib.sh+56−22 utils/searx.sh+6−4
utils/filtron.sh+4−2
@@ -287,7 +287,7 @@ remove_user() {
287287}
288288
289289interactive_shell(){
290− echo "// exit with CTRL-D"
290+ echo "// exit with ${_BCyan}CTRL-D${_creset}"
291291 sudo -H -u ${SERVICE_USER} -i
292292}
293293
@@ -397,7 +397,9 @@ EOF
397397 echo
398398 systemctl --no-pager -l status filtron.service
399399 echo
400− read -r -s -n1 -t 2 -p "// use CTRL-C to stop monitoring the log"
400+ # shellcheck disable=SC2059
401+ printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
402+ read -r -s -n1 -t 2
401403 echo
402404 while true; do
403405 trap break 2
utils/lib.sh+56−22
@@ -75,13 +75,50 @@ required_commands() {
7575 return $exit_val
7676}
7777
78+# colors
79+# ------
80+
81+# shellcheck disable=SC2034
82+set_terminal_colors() {
83+ _colors=8
84+ _creset='\e[0m' # reset all attributes
85+
86+ _Black='\e[0;30m'
87+ _White='\e[1;37m'
88+ _Red='\e[0;31m'
89+ _Green='\e[0;32m'
90+ _Yellow='\e[0;33m'
91+ _Blue='\e[0;34m'
92+ _Violet='\e[0;35m'
93+ _Cyan='\e[0;36m'
94+
95+ _BBlack='\e[1;30m'
96+ _BWhite='\e[1;37m'
97+ _BRed='\e[1;31m'
98+ _BGreen='\e[1;32m'
99+ _BYellow='\e[1;33m'
100+ _BBlue='\e[1;34m'
101+ _BPurple='\e[1;35m'
102+ _BCyan='\e[1;36m'
103+}
104+set_terminal_colors
105+
106+# reST
107+# ----
108+
109+if command -v fmt >/dev/null; then
110+ export FMT="fmt -u"
111+else
112+ export FMT="cat"
113+fi
114+
78115rst_title() {
79116 # usage: rst_title <header-text> [part|chapter|section]
80117
81118 case ${2-chapter} in
82− part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
83− chapter) printf "\n${1}\n${1//?/=}\n";;
84− section) printf "\n${1}\n${1//?/-}\n";;
119+ part) printf "\n${_BGreen}${1//?/=}\n$1\n${1//?/=}${_creset}\n";;
120+ chapter) printf "\n${_BGreen}${1}\n${1//?/=}${_creset}\n";;
121+ section) printf "\n${_BGreen}${1}\n${1//?/-}${_creset}\n";;
85122 *)
86123 err_msg "invalid argument '${2}' in line $(caller)"
87124 return 42
@@ -89,12 +126,6 @@ rst_title() {
89126 esac
90127}
91128
92−if command -v fmt >/dev/null; then
93− export FMT="fmt -u"
94−else
95− export FMT="cat"
96−fi
97−
98129rst_para() {
99130 # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
100131 local prefix=''
@@ -106,9 +137,9 @@ rst_para() {
106137 fi
107138}
108139
109−err_msg() { echo -e "ERROR: $*" >&2; }
110−warn_msg() { echo -e "WARN: $*" >&2; }
111−info_msg() { echo -e "INFO: $*"; }
140+err_msg() { echo -e "${_BRed}ERROR:${_creset} $*" >&2; }
141+warn_msg() { echo -e "${_BBlue}WARN:${_creset} $*" >&2; }
142+info_msg() { echo -e "${_BYellow}INFO:${_creset} $*" >&2; }
112143
113144clean_stdin() {
114145 if [[ $(uname -s) != 'Darwin' ]]; then
@@ -121,11 +152,14 @@ wait_key(){
121152
122153 clean_stdin
123154 local _t=$1
124− local msg="${MSG:-** press any [KEY] to continue **}"
155+ local msg="${MSG}"
156+ [[ -z "$msg" ]] && msg="${_Green}** press any [${_BCyan}KEY${_Green}] to continue **${_creset}"
157+
125158 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
126159 [[ ! -z $_t ]] && _t="-t $_t"
160+ printf "$msg"
127161 # shellcheck disable=SC2086
128− read -r -s -n1 $_t -p "$msg"
162+ read -r -s -n1 $_t
129163 echo
130164 clean_stdin
131165}
@@ -142,12 +176,12 @@ ask_yn() {
142176 case "${2}" in
143177 Yn)
144178 local exit_val=${EXIT_YES}
145− local choice="[YES/no]"
179+ local choice="[${_BGreen}YES${_creset}/no]"
146180 local default="Yes"
147181 ;;
148182 *)
149183 local exit_val=${EXIT_NO}
150− local choice="[NO/yes]"
184+ local choice="[${_BGreen}NO${_creset}/yes]"
151185 local default="No"
152186 ;;
153187 esac
@@ -201,7 +235,7 @@ prefix_stdout () {
201235
202236 local prefix=" | "
203237
204− if [[ ! -z $1 ]] ; then prefix="$1"; fi
238+ if [[ ! -z $1 ]] ; then prefix="${_BYellow}$1${_creset}"; fi
205239
206240 # shellcheck disable=SC2162
207241 (while IFS= read line; do
@@ -270,17 +304,17 @@ choose_one() {
270304 [[ ! -z $_t ]] && _t="-t $_t"
271305
272306 list=("$@")
273− echo -e "Menu::"
307+ echo -e "${_BGreen}Menu::${_creset}"
274308 for ((i=1; i<= $((max -1)); i++)); do
275309 if [[ "$i" == "$default" ]]; then
276− echo -e " $i.) ${list[$i]} [default]"
310+ echo -e " ${_BGreen}$i.${_creset}) ${list[$i]} [default]"
277311 else
278312 echo -e " $i.) ${list[$i]}"
279313 fi
280314 done
281315 while true; do
282316 clean_stdin
283− printf "$1 [$default] "
317+ printf "$1 [${_BGreen}$default${_creset}] "
284318
285319 if (( 10 > max )); then
286320 # shellcheck disable=SC2086
@@ -403,11 +437,11 @@ install_template() {
403437 ;;
404438 "interactiv shell")
405439 echo "// edit ${dst} to your needs"
406− echo "// exit with CTRL-D"
440+ echo "// exit with ${_BCyan}CTRL-D${_creset}"
407441 sudo -H -u "${owner}" -i
408442 $DIFF_CMD "${dst}" "${template_file}"
409443 echo
410− echo "did you edit file ..."
444+ echo "${_BBlack}did you edit file ...${_creset}"
411445 printf " ${template_file}"
412446 if ask_yn "... to your needs?"; then
413447 break
utils/searx.sh+6−4
@@ -445,9 +445,9 @@ deactivate_service() {
445445 uWSGI_restart
446446}
447447
448−interactive_shell() {
449− echo "// exit with CTRL-D"
450− sudo -H -u "${SERVICE_USER}" -i
448+interactive_shell(){
449+ echo "// exit with ${_BCyan}CTRL-D${_creset}"
450+ sudo -H -u ${SERVICE_USER} -i
451451}
452452
453453git_diff() {
@@ -534,7 +534,9 @@ EOF
534534 echo
535535 systemctl --no-pager -l status uwsgi.service
536536 echo
537− read -r -s -n1 -t 2 -p "// use CTRL-C to stop monitoring the log"
537+ # shellcheck disable=SC2059
538+ printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
539+ read -r -s -n1 -t 2
538540 echo
539541 while true; do
540542 trap break 2