5 msgsh="$( cd -P "$( dirname "$(readlink -f "$0")" )" && pwd )/$(basename "${0}")"
19 customized_text_color=false
21 customized_output=false
27 customized_label=false
28 customized_label_color=false
35 echo "usage ${0} [option] [type] [message]"
37 echo "Display a message with a colored app name and message type label"
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)"
43 echo " info General message"
44 echo " warn Warning message"
45 echo " error Error message"
46 echo " debug Debug message"
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"
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"
68 # text [-b/-c color/-f/-l/]
69 # -b: 太字, -f: 点滅, -l: 下線
71 local OPTIND OPTARG _arg _textcolor _decotypes=""
72 while getopts "c:bfln" _arg; do
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" ;;
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" ;;
94 shift "$((OPTIND - 1))"
95 if [[ "${nocolor}" = true ]]; then
98 echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n "${_decotypes}")m${*}\e[m"
104 bash "${msgsh}" -a "msg.sh" error "${1}"
108 # Usage check_color <str>
111 "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white")
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
127 eval set -- "${OPT[@]}"
128 unset OPT OPTS OPTL ARGUMENT
141 customized_label=true
151 IFS=" " read -r -a echo_opts <<< "${2}"
156 customized_output=true
160 customized_label_color=true
161 if check_color "${2}"; then
164 msg_error "The wrong color."
174 customized_text_color=true
175 if check_color "${2}"; then
178 msg_error "The wrong color."
240 # You can specify multiple decorations with ;.
241 # 0 => All attributs off (ノーマル)
243 # 4 => Underscore (下線)
245 # 7 => Reverse video on (色反転)
251 [[ "${customized_output}" = false ]] && output="stdout"
252 [[ "${customized_label_color}" = false ]] && labelcolor="green"
253 [[ "${customized_label}" = false ]] && msg_label="Info"
258 [[ "${customized_output}" = false ]] && output="stdout"
259 [[ "${customized_label_color}" = false ]] && labelcolor="yellow"
260 [[ "${customized_label}" = false ]] && msg_label="Warning"
265 [[ "${customized_output}" = false ]] && output="stdout"
266 [[ "${customized_label_color}" = false ]] && labelcolor="magenta"
267 [[ "${customized_label}" = false ]] && msg_label="Debug"
272 [[ "${customized_output}" = false ]] && output="stderr"
273 [[ "${customized_label_color}" = false ]] && labelcolor="red"
274 [[ "${customized_label}" = false ]] && msg_label="Error"
278 msg_error "Please specify the message type"
282 msg_error "Unknown message type"
287 word_count="${#msg_label}"
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}"
299 [[ "${noappname}" = false ]] && text -c "cyan" "[${appname}]"
303 # echo_message <message>
305 [[ "${customized_text_color}" = false ]] && text -n "${1}" || text -c "${textcolor}" "${1}"
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}")"
314 echo "${echo_opts[@]}" "${full_message}" >&1
317 echo "${echo_opts[@]}" "${full_message}" >&2
320 echo "${echo_opts[@]}" "${full_message}" > "${output}"