From: hayao Date: Sat, 3 Oct 2020 13:15:01 +0000 (+0900) Subject: [update] : Moved message display processing to msg.sh X-Git-Tag: rc3-alpha1~166 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=af9183ff34defabcb028198b02667cd0d83f7d33;p=alterlinux%2Falterlinux.git [update] : Moved message display processing to msg.sh --- diff --git a/allarch.sh b/allarch.sh index 4bc74b45..04e79509 100755 --- a/allarch.sh +++ b/allarch.sh @@ -38,157 +38,46 @@ fi umask 0022 -# Color echo -# usage: echo_color -b -t -d [Text] -# -# Text Color -# 30 => Black -# 31 => Red -# 32 => Green -# 33 => Yellow -# 34 => Blue -# 35 => Magenta -# 36 => Cyan -# 37 => White -# -# Background color -# 40 => Black -# 41 => Red -# 42 => Green -# 43 => Yellow -# 44 => Blue -# 45 => Magenta -# 46 => Cyan -# 47 => White -# -# Text decoration -# You can specify multiple decorations with ;. -# 0 => All attributs off (ノーマル) -# 1 => Bold on (太字) -# 4 => Underscore (下線) -# 5 => Blink on (点滅) -# 7 => Reverse video on (色反転) -# 8 => Concealed on - -echo_color() { - local backcolor textcolor decotypes echo_opts arg OPTIND OPT - echo_opts="-e" - while getopts 'b:t:d:n' arg; do - case "${arg}" in - b) backcolor="${OPTARG}" ;; - t) textcolor="${OPTARG}" ;; - d) decotypes="${OPTARG}" ;; - n) echo_opts="-n -e" ;; - esac - done - shift $((OPTIND - 1)) - if [[ "${nocolor}" = false ]]; then - echo ${echo_opts} "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${*}\e[m" - else - echo ${echo_opts} "${@}" - fi -} - - # Show an INFO message # $1: message string msg_info() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift $((OPTIND - 1)) - echo ${echo_opts} "$( echo_color -t '36' '[allarch.sh]') $( echo_color -t '32' 'Info') ${*}" - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} info "${@}" } - # Show an Warning message # $1: message string msg_warn() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift $((OPTIND - 1)) - echo ${echo_opts} "$( echo_color -t '36' '[allarch.sh]') $( echo_color -t '33' 'Warning') ${*}" >&2 - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} warn "${@}" } - # Show an debug message # $1: message string msg_debug() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift $((OPTIND - 1)) - if [[ ${debug} = true ]]; then - echo ${echo_opts} "$( echo_color -t '36' '[allarch.sh]') $( echo_color -t '35' 'Debug') ${*}" - fi - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv + if [[ "${debug}" = true ]]; then + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} info "${@}" fi } - # Show an ERROR message then exit with status # $1: message string # $2: exit code number (with 0 does not exit) msg_error() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift $((OPTIND - 1)) - echo ${echo_opts} "$( echo_color -t '36' '[allarch.sh]') $( echo_color -t '31' 'Error') ${1}" >&2 + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} error "${1}" if [[ -n "${2:-}" ]]; then exit ${2} fi - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi } diff --git a/build.sh b/build.sh index c030b38d..03385696 100755 --- a/build.sh +++ b/build.sh @@ -38,157 +38,46 @@ fi umask 0022 -# Color echo -# usage: echo_color -b -t -d [Text] -# -# Text Color -# 30 => Black -# 31 => Red -# 32 => Green -# 33 => Yellow -# 34 => Blue -# 35 => Magenta -# 36 => Cyan -# 37 => White -# -# Background color -# 40 => Black -# 41 => Red -# 42 => Green -# 43 => Yellow -# 44 => Blue -# 45 => Magenta -# 46 => Cyan -# 47 => White -# -# Text decoration -# You can specify multiple decorations with ;. -# 0 => All attributs off (ノーマル) -# 1 => Bold on (太字) -# 4 => Underscore (下線) -# 5 => Blink on (点滅) -# 7 => Reverse video on (色反転) -# 8 => Concealed on - -echo_color() { - local backcolor textcolor decotypes echo_opts arg OPTIND OPT - echo_opts="-e" - while getopts 'b:t:d:n' arg; do - case "${arg}" in - b) backcolor="${OPTARG}" ;; - t) textcolor="${OPTARG}" ;; - d) decotypes="${OPTARG}" ;; - n) echo_opts="-n -e" ;; - esac - done - shift "$((OPTIND - 1))" - if [[ "${nocolor}" = false ]]; then - echo ${echo_opts} "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${*}\e[m" - else - echo ${echo_opts} "${@}" - fi -} - - # Show an INFO message # $1: message string msg_info() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift "$((OPTIND - 1))" - echo ${echo_opts} "$( echo_color -t '36' '[build.sh]') $( echo_color -t '32' 'Info') ${*}" - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} info "${@}" } - # Show an Warning message # $1: message string msg_warn() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift "$((OPTIND - 1))" - echo ${echo_opts} "$( echo_color -t '36' '[build.sh]') $( echo_color -t '33' 'Warning') ${*}" >&2 - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} warn "${@}" } - # Show an debug message # $1: message string msg_debug() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift "$((OPTIND - 1))" - if [[ ${debug} = true ]]; then - echo ${echo_opts} "$( echo_color -t '36' '[build.sh]') $( echo_color -t '35' 'Debug') ${*}" - fi - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv + if [[ "${debug}" = true ]]; then + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} info "${@}" fi } - # Show an ERROR message then exit with status # $1: message string # $2: exit code number (with 0 does not exit) msg_error() { - if [[ "${msgdebug}" = false ]]; then - set +xv - else - set -xv - fi - local echo_opts="-e" arg OPTIND OPT - while getopts 'n' arg; do - case "${arg}" in - n) echo_opts="${echo_opts} -n" ;; - esac - done - shift "$((OPTIND - 1))" - echo ${echo_opts} "$( echo_color -t '36' '[build.sh]') $( echo_color -t '31' 'Error') ${1}" >&2 + local _msg_opts="-a build.sh" + [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x" + [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n" + "${script_path}/tools/msg.sh" ${_msg_opts} error "${1}" if [[ -n "${2:-}" ]]; then exit ${2} fi - if [[ "${bash_debug}" = true ]]; then - set -xv - else - set +xv - fi } diff --git a/tools/msg.sh b/tools/msg.sh new file mode 100755 index 00000000..2c079b64 --- /dev/null +++ b/tools/msg.sh @@ -0,0 +1,162 @@ +#!/usr/bin/env bash + +set -e + +script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )" + +appname="msg.sh" +bash_debug=false +nocolor=false +echo_opts="" +message="" +msg_type="info" +msg_label="" +label_width="7" + +_help() { + echo "usage ${0} [option] [type] [message]" + echo + echo "Outputs colored messages" + echo + echo " General type:" + echo " info General message" + echo " warn Warning message" + echo " error Error message" + echo " debug Debug message" + echo + echo " General options:" + echo " -a | --appname [name] Specify the app name" + echo " -n | --nocolor No output colored output" + echo " -o | --echo-opts [opts] Specify echo options" + echo " -x | --bash-debug Enables output bash debugging" + echo " -h | --help This help message" +} + +# Parse options +ARGUMENT="${@}" +_opt_short="a:no:xh" +_opt_long="appname:,nocolor,echo-opts:,bash-debug,help" +OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT}) +[[ ${?} != 0 ]] && exit 1 + +eval set -- "${OPT}" +unset OPT _opt_short _opt_long + +while true; do + case ${1} in + -a | --appname) + appname="${2}" + shift 2 + ;; + -n | --nocolor) + nocolor=true + shift 1 + ;; + -o | --echo-opts) + echo_opts="${2}" + shift 2 + ;; + -x | --bash-debug) + bash_debug=true + set -xv + shift 1 + ;; + -h | --help) + _help + exit 0 + ;; + --) + shift 1 + break + ;; + + esac +done + +# Color echo +# +# Text Color +# 30 => Black +# 31 => Red +# 32 => Green +# 33 => Yellow +# 34 => Blue +# 35 => Magenta +# 36 => Cyan +# 37 => White +# +# Background color +# 40 => Black +# 41 => Red +# 42 => Green +# 43 => Yellow +# 44 => Blue +# 45 => Magenta +# 46 => Cyan +# 47 => White +# +# Text decoration +# You can specify multiple decorations with ;. +# 0 => All attributs off (ノーマル) +# 1 => Bold on (太字) +# 4 => Underscore (下線) +# 5 => Blink on (点滅) +# 7 => Reverse video on (色反転) +# 8 => Concealed on + +case ${1} in + "info") + msg_type="type" + textcolor="32" + msg_label="Info" + shift 1 + ;; + "warn") + msg_type="warn" + textcolor="33" + msg_label="Warning" + shift 1 + ;; + "debug") + msg_type="debug" + textcolor="35" + msg_label="Debug" + shift 1 + ;; + "error") + msg_type="error" + textcolor="31" + msg_label="Error" + shift 1 + ;; + "") + "${script_path}/tools/msg.sh" -a "msg.sh" error "Please specify the message type" + ;; + *) + "${script_path}/tools/msg.sh" -a "msg.sh" error "Unknown message type" + ;; +esac + +word_count="${#msg_label}" +message="${@}" + +echo_type() { + for i in $( seq 1 $(( ${label_width} - ${word_count} )) ); do + echo -ne " " + done + if [[ "${nocolor}" = false ]]; then + echo -ne "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v textcolor ]] && echo -n ";${textcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${msg_label}\e[m" + else + echo -ne "${msg_label}" + fi +} + +echo_appname() { + if [[ "${nocolor}" = false ]]; then + echo -ne "\e[36m[${appname}]\e[m" + else + echo -ne "[${appname}]" + fi +} + +echo ${echo_opts} "$(echo_appname) $(echo_type) ${message}"