

# U_DIM="\033[2m"
# U_INFO="\033[2m"
# U_WARN="\033[2m"

# U_END="\033[0m"

# echo "msg${_U_INFO}${msg}${_U_END}"

# printf "${_U_INFO}%s${_U_END}"


___x_cmd_ui_style_(){
    local IFS=" "
    while [ $# -gt 0 ]; do
        case "$1" in
            dim|debug)                                              ___X_CMD_UI_STYLE_="\033[2m"                                            ;;  # tput dim;;
            info)                                                   ___X_CMD_UI_STYLE_="\033[1;36m"                                         ;;
            error)                                                  ___X_CMD_UI_STYLE_="\033[1;31m"                                         ;;
            warn)                                                   ___X_CMD_UI_STYLE_="\033[1;33m"                                         ;;
            normal)                                                 ___X_CMD_UI_STYLE_="\033[0m"                                            ;;  # tput bold
            bold)                                                   ___X_CMD_UI_STYLE_="\033[1m"                                            ;;  # tput bold
            underline|smul)                                         ___X_CMD_UI_STYLE_="\033[4m"                                            ;;  # tput dim;;
            rmul)                                                   ___X_CMD_UI_STYLE_="\033[24m"                                           ;;  # tput dim;;
            black|red|green|yellow|blue|magenta|cyan|white)         local ___X_CMD_UI_COLORCODE_
                                                                    ___x_cmd_ui_colorcode_ "$1"
                                                                    ___X_CMD_UI_STYLE_="\033[3${___X_CMD_UI_COLORCODE_}m"
                                                                    ;;
            bfg-*)                                                  local ___X_CMD_UI_COLORCODE_;
                                                                    ___x_cmd_ui_colorcode_ "${1#bfg-}"
                                                                    ___X_CMD_UI_STYLE_="\033[1;3${___X_CMD_UI_COLORCODE_}m}"
                                                                    ;;
            hfg-*)                                                  local ___X_CMD_UI_COLORCODE_;
                                                                    ___x_cmd_ui_colorcode_ "${1#hfg-}"
                                                                    ___X_CMD_UI_STYLE_="\033[9${___X_CMD_UI_COLORCODE_}m"
                                                                    ;;
            bg-*)                                                   local ___X_CMD_UI_COLORCODE_;
                                                                    ___x_cmd_ui_colorcode_ "${1#bg-}"
                                                                    ___X_CMD_UI_STYLE_="\033[4${___X_CMD_UI_COLORCODE_}m"
                                                                    ;;
            hbg-*)                                                  local ___X_CMD_UI_COLORCODE_;
                                                                    ___x_cmd_ui_colorcode_ "${1#hbg-}"
                                                                    ___X_CMD_UI_STYLE_="\033[10${___X_CMD_UI_COLORCODE_}m"
                                                                    ;;
            rgbfg-*)                                                ___X_CMD_UI_STYLE_="\033[38;5;${1#rgbfg-}m"                             ;;
            rgbbg-*)                                                ___X_CMD_UI_STYLE_="\033[48;5;${1#rgbbg-}m"                             ;;
            rev)                                                    ___X_CMD_UI_STYLE_="\033[7m"                                            ;;
            end)                                                    ___X_CMD_UI_STYLE_="\033[0m"                                            ;;
            reset)                                                  tput reset                                                  ;;
            --)                                                     shift; ___X_CMD_UI_STYLE_="%s\033[0m" "$*"; return ;;
            *)                                                      ___X_CMD_UI_STYLE_="%s\033[0m" "$*"; return
        esac
        shift
    done
}


___x_cmd_ui_style(){
    local IFS=" "
    while [ $# -gt 0 ]; do
        case "$1" in
            dim|debug)                                              printf "\033[2m"                                            ;; # tput dim;;
            info)                                                   printf "\033[1;36m"                                         ;;
            error)                                                  printf "\033[1;31m"                                         ;;
            warn)                                                   printf "\033[1;33m"                                         ;;
            normal)                                                 printf "\033[0m"                                            ;;
            bold)                                                   printf "\033[1m"                                            ;; # tput bold
            underline|smul)                                         printf "\033[4m"                                            ;; # tput underline;;
            rmul)                                                   printf "\033[24m"                                           ;; # turn off underline;;
            black|red|green|yellow|blue|magenta|cyan|white)         printf "\033[3$(___x_cmd_ui_colorcode "$1")m"               ;;
            bfg-*)                                                  printf "\033[1;3$(___x_cmd_ui_colorcode "${1#bfg-}")m"      ;;
            hfg-*)                                                  printf "\033[9$(___x_cmd_ui_colorcode "${1#hfg-}")m"        ;;
            rgbfg-*)                                                printf "\033[38;5;${1#rgbfg-}m"                             ;;
            bg-*)                                                   printf "\033[4$(___x_cmd_ui_colorcode "${1#bg-}")m"         ;;
            hbg-*)                                                  printf "\033[10$(___x_cmd_ui_colorcode "${1#hbg-}")m"       ;;
            rgbbg-*)                                                printf "\033[48;5;${1#rgbbg-}m"                             ;;
            rev)                                                    printf "\033[7m"                                            ;;
            end)                                                    printf "\033[0m"                                            ;;
            reset)                                                  tput reset                                                  ;;
            --)                                                     shift; printf "%s\033[0m" "$*"; return ;;
            *)                                                      printf "%s\033[0m" "$*"; return
        esac
        shift
    done
}

# Section : tput and its alternative

if command -v tput 1>/dev/null 2>&1; then

# For some advancing usage. Refer to http://linuxcommand.org/lc3_adv_tput.php
# tput: https://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html

___x_cmd_ui_tput(){
    local op="$1"; shift
    case "$op" in
        cursor_read)                ___x_cmd_ui_cursor_read "$@";;
        screen)                     ___x_cmd_ui_screen "$@" ;;

        sc|cursor_save)             tput sc ;;
        rc|cursor_restore)          tput rc ;;

        cnorm|cursor_normal)        tput cnorm ;;
        cvvis|cursor_show)          tput cvvis ;;
        civis|cursor_hide)          tput civis ;;

        smcup|screen_save)          tput smcup ;;
        rmcup|screen_restore)       tput rmcup ;;


        *)                          tput "$op" "$@" ;;
    esac
}


else

___x_cmd_ui_tput(){
    local op="$1"; shift
    case "$op" in
        cursor_read)                ___x_cmd_ui_cursor_read "$@";;
        screen)                     ___x_cmd_ui_screen "$@" ;;

        sc|cursor_save)             printf "\0337" ;;
        rc|cursor_restore)          printf "\0338" ;;

        cnorm|cursor_normal)        printf "\033[34h\033[?25h" ;;
        cvvis|cursor_show)          printf "\033[34l" ;;
        civis|cursor_hide)          printf "\033[?25l" ;;

        smcup|screen_save)          printf "\033[?1049h" ;;
        rmcup|screen_restore)       printf "\033[?1049l" ;;

        el)                         printf "\033[K" ;;
        el1)                        printf "\033[1K" ;;
        cuu)                        printf "\033[%sA" "${2:?Provide number of line to move up}" ;;
    esac
}

fi


# EndSection

# Section : tput facility: cursor
# reference: https://unix.stackexchange.com/questions/88296/get-vertical-cursor-position/183121#183121
# TODO: Not work in alpine. Find out a way to work in alpine.
# Notice: I think I already put enough effort in it. Since we cannot get the cursor position, we have to do the control by ourself thus we always know where the cursor is.
___x_cmd_ui_cursor_read(){
    local cv
    local ch
    local IFS
    # tput u7                  # Inquire for cursor position
    # read -sdR CURPOS         # Read cursor position
    # IFS=\; read cv ch <<<"${CURPOS#$'\e['}" # split $CURPOS

    # TODO: Use something better
    # IFS=';' read -sdR -p $'\E[6n' cv ch
    IFS=';' read -sdR -p "$(printf "\033[6n")" cv ch
    cv="${cv#*[}"

    case $# in
        0)  printf "%s %s\n" "$cv" "$ch";;
        1)  eval "$1=$cv";;
        *)
            eval "$1=$cv";
            eval "$2=$ch";;
    esac
}

___x_cmd_ui_line_count(){
    local maxw="$COLUMNS"
    local n
    local IFS
    local linenum=0
    while read -r n; do
        linenum=$(( linenum + ( ${#n} + maxw - 1 ) / maxw ))
    done
    printf "%s" "$linenum"
}

___x_cmd_ui_colorcode(){
    case "$1" in
        black)      printf 0 ;;
        red)        printf 1 ;;
        green)      printf 2 ;;
        yellow)     printf 3 ;;
        blue)       printf 4 ;;
        magenta)    printf 5 ;;
        cyan)       printf 6 ;;
        white)      printf 7 ;;
    esac
}

___x_cmd_ui_colorcode_(){
    case "$1" in
        black)      ___X_CMD_UI_COLORCODE_=0 ;;
        red)        ___X_CMD_UI_COLORCODE_=1 ;;
        green)      ___X_CMD_UI_COLORCODE_=2 ;;
        yellow)     ___X_CMD_UI_COLORCODE_=3 ;;
        blue)       ___X_CMD_UI_COLORCODE_=4 ;;
        magenta)    ___X_CMD_UI_COLORCODE_=5 ;;
        cyan)       ___X_CMD_UI_COLORCODE_=6 ;;
        white)      ___X_CMD_UI_COLORCODE_=7 ;;
    esac
}

# EndSection
