Skip to content

Commit 74292d8

Browse files
committed
Merge branch 'msg.sh' into dev
2 parents c9673ef + e75360f commit 74292d8

File tree

1 file changed

+139
-145
lines changed

1 file changed

+139
-145
lines changed

tools/msg.sh

Lines changed: 139 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/usr/bin/env bash
22

3-
set -e
3+
set -eu
44

5-
script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
5+
msgsh="$( cd -P "$( dirname "$(readlink -f "$0")" )" && pwd )/$(basename "${0}")"
66

77
msg_type="info"
8-
echo_opts=""
8+
echo_opts=()
99
bash_debug=false
1010
nocolor=false
1111

12-
1312
# appname
1413
appname="msg.sh"
1514
noappname=false
@@ -37,188 +36,182 @@ _help() {
3736
echo
3837
echo "Display a message with a colored app name and message type label"
3938
echo
39+
echo " Example: ${0} -a 'Script' -s 10 warn It is example message"
40+
echo " Output : $(bash "${msgsh}" -a "Script" -s 10 warn It is example message)"
41+
echo
4042
echo " General type:"
41-
echo " info General message"
42-
echo " warn Warning message"
43-
echo " error Error message"
44-
echo " debug Debug message"
43+
echo " info General message"
44+
echo " warn Warning message"
45+
echo " error Error message"
46+
echo " debug Debug message"
4547
echo
4648
echo " General options:"
47-
echo " -a [name] Specify the app name"
48-
echo " -c [character] Specify the character to adjust the label"
49-
echo " -l [label] Specify the label"
50-
echo " -n | --nocolor No output colored output"
51-
echo " -o [option] Specify echo options"
52-
echo " -p [output] Specify the output destination"
53-
echo " standard output: stdout"
54-
echo " error output : stderr"
55-
echo " -r [color] Specify the color of label"
56-
echo " -s [number] Specifies the label space"
57-
echo " -t [color] Specify the color of text"
58-
echo " -x | --bash-debug Enables output bash debugging"
59-
echo " -h | --help This help message"
49+
echo " -a | --appname [name] Specify the app name"
50+
echo " -c | --chr [character] Specify the character to adjust the label"
51+
echo " -l | --label [label] Specify the label"
52+
echo " -n | --nocolor No output colored output"
53+
echo " -o | --echo-option [option] Specify echo options"
54+
echo " -p | --output [output] Specify the output destination"
55+
echo " standard output: stdout"
56+
echo " error output : stderr"
57+
echo " -r | --label-color [color] Specify the color of label"
58+
echo " -s | --label-space [number] Specifies the label space"
59+
echo " -t | --text-color [color] Specify the color of text"
60+
echo " -x | --bash-debug Enables output bash debugging"
61+
echo " -h | --help This help message"
6062
echo
61-
echo " --nolabel Do not output label"
62-
echo " --noappname Do not output app name"
63-
echo " --noadjust Do not adjust the width of the label"
63+
echo " --nolabel Do not output label"
64+
echo " --noappname Do not output app name"
65+
echo " --noadjust Do not adjust the width of the label"
6466
}
6567

6668
# text [-b/-c color/-f/-l/]
6769
# -b: 太字, -f: 点滅, -l: 下線
6870
text() {
69-
local OPTIND OPTARG _arg _textcolor _decotypes="" _message
71+
local OPTIND OPTARG _arg _textcolor _decotypes=""
7072
while getopts "c:bfln" _arg; do
7173
case "${_arg}" in
7274
c)
7375
case "${OPTARG}" in
74-
"black")
75-
_textcolor="30"
76-
;;
77-
"red")
78-
_textcolor="31"
79-
;;
80-
"green")
81-
_textcolor="32"
82-
;;
83-
"yellow")
84-
_textcolor="33"
85-
;;
86-
"blue")
87-
_textcolor="34"
88-
;;
89-
"magenta")
90-
_textcolor="35"
91-
;;
92-
"cyan")
93-
_textcolor="36"
94-
;;
95-
"white")
96-
_textcolor="37"
97-
;;
98-
*)
99-
return 1
100-
;;
76+
"black" ) _textcolor="30" ;;
77+
"red" ) _textcolor="31" ;;
78+
"green" ) _textcolor="32" ;;
79+
"yellow" ) _textcolor="33" ;;
80+
"blue" ) _textcolor="34" ;;
81+
"magenta") _textcolor="35" ;;
82+
"cyan" ) _textcolor="36" ;;
83+
"white" ) _textcolor="37" ;;
84+
* ) return 1 ;;
10185
esac
10286
;;
103-
b)
104-
_decotypes="${_decotypes};1"
105-
;;
106-
f)
107-
_decotypes="${_decotypes};5"
108-
;;
109-
l)
110-
_decotypes="${_decotypes};4"
111-
;;
112-
n)
113-
_decotypes="${_decotypes};0"
114-
;;
87+
b) _decotypes="${_decotypes};1" ;;
88+
f) _decotypes="${_decotypes};5" ;;
89+
l) _decotypes="${_decotypes};4" ;;
90+
n) _decotypes="${_decotypes};0" ;;
91+
*) msg_error "Wrong use of text function" ;;
11592
esac
11693
done
11794
shift "$((OPTIND - 1))"
118-
119-
_message="${@}"
12095
if [[ "${nocolor}" = true ]]; then
121-
echo -ne "${@}"
96+
echo -ne "${*}"
12297
else
123-
echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n "${_decotypes}")m${_message}\e[m"
98+
echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n "${_decotypes}")m${*}\e[m"
12499
fi
125100
}
126101

127102
# Message functions
128103
msg_error() {
129-
bash "${script_path}/tools/msg.sh" -a "msg.sh" error "${1}"
104+
bash "${msgsh}" -a "msg.sh" error "${1}"
130105
}
131106

107+
# Check color
108+
# Usage check_color <str>
109+
check_color(){
110+
case "${1}" in
111+
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
112+
return 0
113+
;;
114+
*)
115+
return 1
116+
;;
117+
esac
118+
}
119+
120+
ARGUMENT=("${@}")
121+
OPTS="a:c:l:no:p:r:s:t:xh"
122+
OPTL="appname:,chr:,label:,nocolor,echo-option:,output:,label-color:,label-space:,text-color:,bash-debug,help,nolabel,noappname,noadjust"
123+
if ! OPT=($(getopt -o ${OPTS} -l ${OPTL} -- "${ARGUMENT[@]}")); then
124+
exit 1
125+
fi
126+
127+
eval set -- "${OPT[@]}"
128+
unset OPT OPTS OPTL ARGUMENT
132129

133-
while getopts "a:c:l:no:p:r:s:t:xh-:" arg; do
134-
case "${arg}" in
135-
a)
136-
appname="${OPTARG}"
130+
while true; do
131+
case "${1}" in
132+
-a | --appname)
133+
appname="${2}"
134+
shift 2
137135
;;
138-
c)
139-
adjust_chr="${OPTARG}"
136+
-c | --chr)
137+
adjust_chr="${2}"
138+
shift 2
140139
;;
141-
l)
140+
-l | --label)
142141
customized_label=true
143-
msg_label="${OPTARG}"
142+
msg_label="${2}"
143+
shift 2
144144
;;
145-
n)
145+
-n | --nocolor)
146146
nocolor=true
147+
shift 1
147148
;;
148-
o)
149-
echo_opts="${OPTARG}"
149+
-o | --echo-option)
150+
echo_opts+=(${2})
151+
shift 2
150152
;;
151-
p)
152-
output="${OPTARG}"
153+
-p | --output)
154+
output="${2}"
153155
customized_output=true
156+
shift 2
154157
;;
155-
r)
158+
-r | --label-color)
156159
customized_label_color=true
157-
case "${OPTARG}" in
158-
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
159-
labelcolor="${OPTARG}"
160-
;;
161-
*)
162-
msg_error "The wrong color."
163-
exit 1
164-
;;
165-
esac
160+
if check_color "${2}"; then
161+
labelcolor="${2}"
162+
else
163+
msg_error "The wrong color."
164+
exit 1
165+
fi
166+
shift 2
166167
;;
167-
s)
168-
label_space="${OPTARG}"
168+
-s | --label-space)
169+
label_space="${2}"
170+
shift 2
169171
;;
170-
t)
172+
-t | --text-color)
171173
customized_text_color=true
172-
case "${OPTARG}" in
173-
"black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
174-
textcolor="${OPTARG}"
175-
;;
176-
*)
177-
msg_error "The wrong color."
178-
exit 1
179-
;;
180-
esac
174+
if check_color "${2}"; then
175+
textcolor="${2}"
176+
else
177+
msg_error "The wrong color."
178+
exit 1
179+
fi
180+
shift 2
181181
;;
182-
x)
182+
-x | --bash_debug)
183183
bash_debug=true
184184
set -xv
185+
shift 1
185186
;;
186-
h)
187+
-h | --help)
187188
_help
188189
shift 1
189190
exit 0
190191
;;
191-
-)
192-
case "${OPTARG}" in
193-
"nocolor")
194-
nocolor=true
195-
;;
196-
"bash-debug")
197-
bash_debug=true
198-
set -xv
199-
;;
200-
"help")
201-
_help
202-
exit 0
203-
;;
204-
"nolabel")
205-
nolabel=true
206-
;;
207-
"noappname")
208-
noappname=true
209-
;;
210-
"noadjust")
211-
noadjust=true
212-
;;
213-
*)
214-
_help
215-
exit 1
216-
;;
217-
esac
218-
esac
192+
--nolabel)
193+
nolabel=true
194+
shift 1
195+
;;
196+
--noappname)
197+
noappname=true
198+
shift 1
199+
;;
200+
--noadjust)
201+
noadjust=true
202+
shift 1
203+
;;
204+
--)
205+
shift 1
206+
break
207+
;;
208+
*)
209+
_help
210+
exit 1
211+
;;
212+
esac
219213
done
220214

221-
shift "$((OPTIND - 1))"
222215

223216
# Color echo
224217
#
@@ -251,7 +244,7 @@ shift "$((OPTIND - 1))"
251244
# 7 => Reverse video on (色反転)
252245
# 8 => Concealed on
253246

254-
case "${1}" in
247+
case "${1-""}" in
255248
"info")
256249
msg_type="type"
257250
[[ "${customized_output}" = false ]] && output="stdout"
@@ -291,15 +284,16 @@ case "${1}" in
291284
esac
292285

293286
word_count="${#msg_label}"
294-
message="${@}"
287+
message="${*}"
295288

296289
echo_type() {
297-
local i
298290
if [[ "${nolabel}" = false ]]; then
299291
if [[ "${noadjust}" = false ]]; then
300-
for i in $( seq 1 "$(( label_space - word_count))" ); do
301-
echo -ne "${adjust_chr}"
302-
done
292+
yes "${adjust_chr}" 2> /dev/null | head -n "$(( label_space - word_count))" | tr -d "\n"
293+
#local i
294+
#for i in $( seq 1 "$(( label_space - word_count))" ); do
295+
# echo -ne "${adjust_chr}"
296+
#done
303297
fi
304298
text -c "${labelcolor}" "${msg_label}"
305299
fi
@@ -313,7 +307,7 @@ echo_appname() {
313307

314308
# echo_message <message>
315309
echo_message() {
316-
if [[ "${textcolor}" = "white" ]]; then
310+
if [[ "${customized_text_color}" = false ]]; then
317311
text -n "${1}"
318312
else
319313
text -c "${textcolor}" "${1}"
@@ -325,13 +319,13 @@ for count in $(seq "1" "$(echo -ne "${message}\n" | wc -l)"); do
325319
full_message="$(echo_appname)$(echo_type) $(echo_message "${_message}")"
326320
case "${output}" in
327321
"stdout")
328-
echo ${echo_opts} "${full_message}" >&1
322+
echo "${echo_opts[@]}" "${full_message}" >&1
329323
;;
330324
"stderr")
331-
echo ${echo_opts} "${full_message}" >&2
325+
echo "${echo_opts[@]}" "${full_message}" >&2
332326
;;
333327
*)
334-
echo ${echo_opts} "${full_message}" > ${output}
328+
echo "${echo_opts[@]}" "${full_message}" > "${output}"
335329
;;
336330
esac
337331
unset _message

0 commit comments

Comments
 (0)