OSDN Git Service

[update] : Added text output
authorhayao <shun819.mail@gmail.com>
Sat, 19 Dec 2020 03:04:27 +0000 (12:04 +0900)
committerhayao <shun819.mail@gmail.com>
Sat, 19 Dec 2020 03:04:27 +0000 (12:04 +0900)
wfa [changed mode: 0644->0755]

diff --git a/wfa b/wfa
old mode 100644 (file)
new mode 100755 (executable)
index 212c4ba..22f232b
--- a/wfa
+++ b/wfa
@@ -1 +1,339 @@
-#!/usr/bin/env bash
\ No newline at end of file
+#!/usr/bin/env bash
+
+set -eu
+
+msgdebug=false
+nocolor=false
+debug=true
+
+msg() {
+    local OPTIND OPTARG arg
+
+    local _appname="msg.sh"
+    local _bash_debug=false
+    local _nocolor=false
+    local _echo_opts=""
+    local _message=""
+    local _msg_type="info"
+    local _msg_label=""
+    local _label_space="7"
+    local _adjust_chr=" "
+    local _customized_label=false
+    local _customized_label_color=false
+    local _nolabel=false
+    local _noappname=false
+    local _noadjust=false
+    local _output="stdout"
+
+    _help() {
+        echo "usage ${0} [option] [type] [message]"
+        echo
+        echo "Display a message with a colored app name and message type label"
+        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 [name]                 Specify the app name"
+        echo "    -c [character]            Specify the character to adjust the label"
+        echo "    -l [label]                Specify the label."
+        echo "    -n | --nocolor            No output colored output"
+        echo "    -o [option]               Specify echo options"
+        echo "    -r [color]                Specify the color of label"
+        echo "    -s [number]               Specifies the label space."
+        echo "    -x | --bash-debug         Enables output bash debugging"
+        echo "    -h | --help               This help message"
+        echo
+        echo "         --nolabel            Do not output label"
+        echo "         --noappname          Do not output app name"
+        echo "         --noadjust           Do not adjust the width of the label"
+    }
+
+    while getopts "a:c:l:no:r:s:xh-:" arg; do
+        case ${arg} in
+                a) 
+                    _appname="${OPTARG}"
+                    ;;
+                c) 
+                    _adjust_chr="${OPTARG}"
+                    ;;
+                l) 
+                    _customized_label=true
+                    _msg_label="${OPTARG}"
+                    ;;
+                n)
+                    _nocolor=true
+                    ;;
+                o)
+                    _echo_opts="${OPTARG}"
+                    ;;
+                r)
+                    _customized_label_color=true
+                    case ${OPTARG} in
+                        "black")
+                            _labelcolor="30"
+                            ;;
+                        "red")
+                            _labelcolor="31"
+                            ;;
+                        "green")
+                            _labelcolor="32"
+                            ;;
+                        "yellow")
+                            _labelcolor="33"
+                            ;;
+                        "blue")
+                            _labelcolor="34"
+                            ;;
+                        "magenta")
+                            _labelcolor="35"
+                            ;;
+                        "cyan")
+                            _labelcolor="36"
+                            ;;
+                        "white")
+                            _labelcolor="37"
+                            ;;
+                        *)
+                            return 1
+                            ;;
+                    esac
+                    ;;
+                s)
+                    _label_space="${OPTARG}"
+                    ;;
+                x)
+                    _bash_debug=true
+                    set -xv
+                    ;;
+                h)
+                    _help
+                    shift 1
+                    exit 0
+                    ;;
+                -)
+                    case "${OPTARG}" in
+                        "nocolor")
+                            _nocolor=true
+                            ;;
+                        "bash-debug")
+                            _bash_debug=true
+                            set -xv
+                            ;;
+                        "help") 
+                            _help
+                            exit 0
+                            ;;
+                        "nolabel")
+                            _nolabel=true
+                            ;;
+                        "noappname")
+                            _noappname=true
+                            ;;
+                        "noadjust")
+                            _noadjust=true 
+                            ;;
+                        *)
+                            _help
+                            exit 1
+                            ;;
+                    esac
+        esac
+    done
+
+    shift $((OPTIND - 1))
+
+    # 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"
+            _output="stdout"
+            [[ "${_customized_label_color}" = false ]] && _labelcolor="32"
+            [[ "${_customized_label}"       = false ]] && _msg_label="Info"
+            shift 1
+            ;;
+        "warn")
+            _msg_type="warn"
+            _output="stdout"
+            [[ "${_customized_label_color}" = false ]] && _labelcolor="33"
+            [[ "${_customized_label}"       = false ]] && _msg_label="Warning"
+            shift 1
+            ;;
+        "debug")
+            _msg_type="debug"
+            _output="stdout"
+            [[ "${_customized_label_color}" = false ]] && _labelcolor="35"
+            [[ "${_customized_label}"       = false ]] && _msg_label="Debug"
+            shift 1
+            ;;
+        "error")
+            _msg_type="error"
+            _output="stderr"
+            [[ "${_customized_label_color}" = false ]] && _labelcolor="31"
+            [[ "${_customized_label}"       = false ]] && _msg_label="Error"
+            shift 1
+            ;;
+        "")
+            echo "Please specify the message type" >&2
+            exit 1
+            ;;
+        *)
+            echo "Unknown message type" >&2
+            exit 1
+            ;;
+    esac
+
+    _word_count="${#_msg_label}"
+    _message="${@}"
+
+    echo_type() {
+        local __time
+        if [[ "${_nolabel}" = false ]]; then
+            if [[ "${_noadjust}" = false ]]; then
+                for __time in $( seq 1 $(( ${_label_space} - ${_word_count})) ); do
+                    echo -ne "${_adjust_chr}"
+                done
+            fi
+            if [[ "${_nocolor}" = false ]]; then
+                echo -ne "\e[$([[ -v _backcolor ]] && echo -n "${_backcolor}"; [[ -v _labelcolor ]] && echo -n ";${_labelcolor}"; [[ -v _decotypes ]] && echo -n ";${_decotypes}")m${_msg_label}\e[m "
+            else
+                echo -ne "${_msg_label} "
+            fi
+        fi
+    }
+
+    echo_appname() {
+        if [[ "${_noappname}" = false ]]; then
+            if [[ "${_nocolor}" = false ]]; then
+                echo -ne "\e[36m[${_appname}]\e[m "
+            else
+                echo -ne "[${_appname}] "
+            fi
+        fi
+    }
+
+    for _count in $(seq "1" "$(echo -ne "${_message}\n" | wc -l)"); do
+        _echo_message=$(echo -ne "${_message}\n" |head -n "${_count}" | tail -n 1 )
+        _full_message="$(echo_appname)$(echo_type)${_echo_message}"
+        case "${_output}" in
+            "stdout")
+                echo ${_echo_opts} "${_full_message}" >&1
+                ;;
+            "stderr")
+                echo ${_echo_opts} "${_full_message}" >&2
+                ;;
+            *)
+                echo ${_echo_opts} "${_full_message}" > ${_output}
+                ;;
+        esac
+    done
+}
+
+# Show an INFO message
+# $1: message string
+msg_info() {
+    local _msg_opts="-a WFA"
+    if [[ "${1}" = "-n" ]]; then
+        _msg_opts="${_msg_opts} -o -n"
+        shift 1
+    fi
+    [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
+    [[ "${nocolor}"  = true ]] && _msg_opts="${_msg_opts} -n"
+    msg ${_msg_opts} info "${1}"
+}
+
+# Show an Warning message
+# $1: message string
+msg_warn() {
+    local _msg_opts="-a WFA"
+    if [[ "${1}" = "-n" ]]; then
+        _msg_opts="${_msg_opts} -o -n"
+        shift 1
+    fi
+    [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
+    [[ "${nocolor}"  = true ]] && _msg_opts="${_msg_opts} -n"
+    msg ${_msg_opts} warn "${1}"
+}
+
+# Show an debug message
+# $1: message string
+msg_debug() {
+    if [[ "${debug}" = true ]]; then
+        local _msg_opts="-a WFA"
+        if [[ "${1}" = "-n" ]]; then
+            _msg_opts="${_msg_opts} -o -n"
+            shift 1
+        fi
+        [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
+        [[ "${nocolor}"  = true ]] && _msg_opts="${_msg_opts} -n"
+        msg ${_msg_opts} debug "${1}"
+    fi
+}
+
+# Show an ERROR message then exit with status
+# $1: message string
+# $2: exit code number (with 0 does not exit)
+msg_error() {
+    local _msg_opts="-a WFA"
+    if [[ "${1}" = "-n" ]]; then
+        _msg_opts="${_msg_opts} -o -n"
+        shift 1
+    fi
+    [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
+    [[ "${nocolor}"  = true ]] && _msg_opts="${_msg_opts} -n"
+    msg ${_msg_opts} error "${1}"
+    if [[ -n "${2:-}" ]]; then
+        exit ${2}
+    fi
+}
+
+help (){
+    echo "Usage:"
+    echo "wfa"
+    echo "wfa <operation> [...]"
+    echo
+    echo "operations:"
+    echo "yay {-h --help}"
+    echo "yay {-V --version}"
+    #echo "yay {-D --database}    <options> <package(s)>"
+    #echo "yay {-F --files}       [options] [package(s)]"
+    echo "yay {-Q --query}       [options] [package(s)]"
+    echo "yay {-R --remove}      [options] <package(s)>"
+    echo "yay {-S --sync}        [options] [package(s)]"
+    #echo "yay {-T --deptest}     [options] [package(s)]"
+    #echo "yay {-U --upgrade}     [options] <file(s)>"
+}
+