#
# user function for CLI
#


# {{{ function setAnsi (void)
# https://unix.stackexchange.com/questions/9957/how-to-check-if-bash-can-print-colors
function setAnsi () {
	[[ ! -t 1 ]] && [[ -n ${SSH_CLIENT} ]] && export TERM="xterm"

	# check if stdout is a terminal...
    if [[ -t 1 || ${TERM} == "xterm"* ]]; then
        # see if it supports colors...
        ncolors=$( tput colors )

        if [[ -n ${ncolors} ]] && (( ncolors >= 8 )); then
            bold="$(tput bold)"
            underline="$( tput smul )"
            revcolor="$(  tput smso )"
            # xterm mode 에서 sgr0 에 (B 문자가 포함이 되는데 (ISO 2022)
            # watch 등의 일부 tool 에서 ISO 2022 를 처리하지 못하는 버그가 있음
            # (B 문자열을 포함하지 않는 ansi mode 로 sgr0 을 이용하도록 변경
            normal="$(    TERM=ansi tput sgr0 )"
            black="$(     tput setaf 0 )"
            red="$(       tput setaf 1 )"
            green="$(     tput setaf 2 )"
            yellow="$(    tput setaf 3 )"
            blue="$(      tput setaf 4 )"
            magenta="$(   tput setaf 5 )"
            cyan="$(      tput setaf 6 )"
            white="$(     tput setaf 7 )"
            bgblack="$(   tput setab 0 )"
            bgred="$(     tput setab 1 )"
            bggreen="$(   tput setab 2 )"
            bgyellow="$(  tput setab 3 )"
            bgblue="$(    tput setab 4 )"
            bgmagenta="$( tput setab 5 )"
            bgcyan="$(    tput setab 6 )"
            bgwhite="$(   tput setab 7 )"
            bblack="${bold}${black}"
            bred="${bold}${red}"
            bgreen="${bold}${green}"
            byellow="${bold}${yellow}"
            bblue="${bold}${blue}"
            bmagenta="${bold}${magenta}"
            bcyan="${bold}${cyan}"
            bwhite="${bold}${white}"
        fi
    fi
}
setAnsi 2> /dev/null
# }}}

# {{{ function unsetAnsi (void)
function unsetAnsi () {
	unset bold underline revcolor normal black red green yellow blue magenta \
		cyan white bgblack bgred bggreen bgyellow bgblue bgmagenta bgcyan    \
		bgwhite bblack bred bgreen byellow bblue bmagenta bcyan bwhite
}
# }}}

# set env
export OK_V=" ${bgreen}[  OK  ]${normal}"
export ERR_V="${bred}[ FAIL ]${normal}"
export INFO_V="${byellow}[ INFO ]${normal}"
export WARN_V="${bred}[ WARN ]${normal}"

#
# vim: set filetype=sh noet sw=4 ts=4 fdm=marker:
# vim<600: noet sw=4 ts=4:
#
