OSDN Git Service

[add] : Added pen4 xfce
[alterlinux/alterlinux.git] / tools / msg.sh
1 #!/usr/bin/env bash
2
3 set -eu
4
5 msgsh="$( cd -P "$( dirname "$(readlink -f "$0")" )" && pwd )/$(basename "${0}")"
6
7 msg_type="info"
8 echo_opts=()
9 bash_debug=false
10 nocolor=false
11
12 # appname
13 appname="msg.sh"
14 noappname=false
15
16 # main text
17 message=""
18 textcolor="white"
19 customized_text_color=false
20 output="stdout"
21 customized_output=false
22
23 # label
24 msg_label=""
25 label_space="7"
26 nolabel=false
27 customized_label=false
28 customized_label_color=false
29 labelcolor=""
30 adjust_chr=" "
31 noadjust=false
32
33
34 _help() {
35     echo "usage ${0} [option] [type] [message]"
36     echo
37     echo "Display a message with a colored app name and message type label"
38     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
42     echo " General type:"
43     echo "    info                                  General message"
44     echo "    warn                                  Warning message"
45     echo "    error                                 Error message"
46     echo "    debug                                 Debug message"
47     echo
48     echo " General options:"
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"
62     echo
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"
66 }
67
68 # text [-b/-c color/-f/-l/]
69 # -b: 太字, -f: 点滅, -l: 下線
70 text() {
71     local OPTIND OPTARG _arg _textcolor _decotypes=""
72     while getopts "c:bfln" _arg; do
73         case "${_arg}" in
74             c)
75                 case "${OPTARG}" in
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        ;;
85                 esac
86                 ;;
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" ;;
92         esac
93     done
94     shift "$((OPTIND - 1))"
95     if [[ "${nocolor}" = true ]]; then
96         echo -ne "${*}"
97     else
98         echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n "${_decotypes}")m${*}\e[m"
99     fi
100 }
101
102 # Message functions
103 msg_error() {
104     bash "${msgsh}" -a "msg.sh" error "${1}"
105 }
106
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
129
130 while true; do
131     case "${1}" in
132         -a | --appname)
133             appname="${2}"
134             shift 2
135             ;;
136         -c | --chr)
137             adjust_chr="${2}"
138             shift 2
139             ;;
140         -l | --label)
141             customized_label=true
142             msg_label="${2}"
143             shift 2
144             ;;
145         -n | --nocolor)
146             nocolor=true
147             shift 1
148             ;;
149         -o | --echo-option)
150             #echo_opts+=(${2})
151             IFS=" " read -r -a echo_opts <<< "${2}"
152             shift 2
153             ;;
154         -p | --output)
155             output="${2}"
156             customized_output=true
157             shift 2
158             ;;
159         -r | --label-color)
160             customized_label_color=true
161             if check_color "${2}"; then
162                 labelcolor="${2}"
163             else
164                 msg_error "The wrong color."
165                 exit 1
166             fi
167             shift 2
168             ;;
169         -s | --label-space)
170             label_space="${2}"
171             shift 2
172             ;;
173         -t | --text-color)
174             customized_text_color=true
175             if check_color "${2}"; then
176                 textcolor="${2}"
177             else
178                 msg_error "The wrong color."
179                 exit 1
180             fi
181             shift 2
182             ;;
183         -x | --bash_debug)
184             bash_debug=true
185             set -xv
186             shift 1
187             ;;
188         -h | --help)
189             _help
190             shift 1
191             exit 0
192             ;;
193         --nolabel)
194             nolabel=true
195             shift 1
196             ;;
197         --noappname)
198             noappname=true
199             shift 1
200             ;;
201         --noadjust)
202             noadjust=true
203             shift 1
204             ;;
205         --)
206             shift 1
207             break
208             ;;
209         *)
210             _help
211             exit 1
212             ;;
213     esac
214 done
215
216
217 # Color echo
218 #
219 # Text Color
220 # 30 => Black
221 # 31 => Red
222 # 32 => Green
223 # 33 => Yellow
224 # 34 => Blue
225 # 35 => Magenta
226 # 36 => Cyan
227 # 37 => White
228 #
229 # Background color
230 # 40 => Black
231 # 41 => Red
232 # 42 => Green
233 # 43 => Yellow
234 # 44 => Blue
235 # 45 => Magenta
236 # 46 => Cyan
237 # 47 => White
238 #
239 # Text decoration
240 # You can specify multiple decorations with ;.
241 # 0 => All attributs off (ノーマル)
242 # 1 => Bold on (太字)
243 # 4 => Underscore (下線)
244 # 5 => Blink on (点滅)
245 # 7 => Reverse video on (色反転)
246 # 8 => Concealed on
247
248 case "${1-""}" in
249     "info")
250         msg_type="type"
251         [[ "${customized_output}"      = false ]] && output="stdout"
252         [[ "${customized_label_color}" = false ]] && labelcolor="green"
253         [[ "${customized_label}"       = false ]] && msg_label="Info"
254         shift 1
255         ;;
256     "warn")
257         msg_type="warn"
258         [[ "${customized_output}"      = false ]] && output="stdout"
259         [[ "${customized_label_color}" = false ]] && labelcolor="yellow"
260         [[ "${customized_label}"       = false ]] && msg_label="Warning"
261         shift 1
262         ;;
263     "debug")
264         msg_type="debug"
265         [[ "${customized_output}"      = false ]] && output="stdout"
266         [[ "${customized_label_color}" = false ]] && labelcolor="magenta"
267         [[ "${customized_label}"       = false ]] && msg_label="Debug"
268         shift 1
269         ;;
270     "error")
271         msg_type="error"
272         [[ "${customized_output}"      = false ]] && output="stderr"
273         [[ "${customized_label_color}" = false ]] && labelcolor="red"
274         [[ "${customized_label}"       = false ]] && msg_label="Error"
275         shift 1
276         ;;
277     "")
278         msg_error "Please specify the message type"
279         exit 1
280         ;;
281     *)
282         msg_error "Unknown message type"
283         exit 1
284         ;;
285 esac
286
287 word_count="${#msg_label}"
288 message="${*}"
289
290 echo_type() {
291     if [[ "${nolabel}" = false ]]; then
292         [[ "${noadjust}" = false ]] && yes "${adjust_chr}" 2> /dev/null  | head -n "$(( label_space - word_count))" | tr -d "\n"
293         text -c "${labelcolor}" "${msg_label}"
294     fi
295     return 0
296 }
297
298 echo_appname() {
299     [[ "${noappname}" = false ]] && text -c "cyan" "[${appname}]"
300     return 0
301 }
302
303 # echo_message <message>
304 echo_message() {
305     [[ "${customized_text_color}" = false ]] && text -n "${1}" || text -c "${textcolor}" "${1}"
306     return 0
307 }
308
309 for count in $(seq "1" "$(echo -ne "${message}\n" | wc -l)"); do
310     _message="$(echo -ne "${message}\n" | head -n "${count}" | tail -n 1 )"
311     full_message="$(echo_appname)$(echo_type) $(echo_message "${_message}")"
312     case "${output}" in
313         "stdout")
314             echo "${echo_opts[@]}" "${full_message}" >&1
315             ;;
316         "stderr")
317             echo "${echo_opts[@]}" "${full_message}" >&2
318             ;;
319         *)
320             echo "${echo_opts[@]}" "${full_message}" > "${output}"
321             ;;
322     esac
323     unset _message
324 done