export F_BOLD="\e[1m" export F_NONE="\e[0m" export C_NONE="\e[0m" export C_RED="\e[38;5;9m" export C_BLUE="\e[38;5;12m" export C_GREEN="\e[38;5;2m" export C_ORANGE="\e[38;5;208m" export C_YELLOW="\e[38;5;220m" export C_PURPLE="\e[38;5;5m" export S_DEBUG="${C_PURPLE}\u2691 [debug]${F_NONE} " export S_CRIT="${F_BOLD}${C_RED}\u2691 [critical]${F_NONE} " export S_ERROR="${C_RED}\u2691 [error]${F_NONE} " export S_WARN="${C_ORANGE}\u2691 [warning]${F_NONE} " export S_INFO="${F_BOLD}${C_BLUE}\u2691 [info]${F_NONE} " export S_NOTICE="${C_BLUE}\u2691 [notice]${F_NONE} " export S_SUCCESS="${F_BOLD}${C_GREEN}\u2691 [success]${F_NONE} " export U_ITEM="\u21e2" export U_TICK="\u2713" export U_FAIL="\u2715" export COLS="$(( $(tput cols) - 1 ))" export EL=$(tput el) : "cli.clear" function cli.clear { clear } # Get the cursor current position and returns CURSOR_X, CURSOR_Y : "cli.get_cursor" cli.get_cursor() { local _xy echo -ne $"\E[6n" read -rsdR _xy _xy="${_xy#*[}" export CURSOR_X=${_xy/*;} export CURSOR_Y=${_xy/;*} } # Puts the cursor at x:y (defaults to 0:0) : "cli.set_cursor " cli.set_cursor() { printf "\E[%sH" "${2:0};${1:0}" } # Just prints N blank lines : "cli.jump " cli.jump() { for i in $(seq 1 ${1:-1}); do printf "%s${EL}\n" done } # Just like printf : "cli.print [] [ ...]" cli.print() { printf $"${1}" ${@:2} } # Folds text at 'size' column : "cli.fold " cli.fold() { printf "%s" "${@:2}" | fold -sw ${1} } : "cli.status [] " cli.status() { local fmt=${2//\\n} cli.jump case "${1:-info}" in debug) printf "\e[0F${S_DEBUG}${fmt:-%s}${@:3}${EL}\n" ;; crit*) printf "\e[0F${S_CRIT}${fmt:-%s}${@:3}${EL}\n" ;; error) printf "\e[0F${S_ERROR}${fmt:-%s}${@:3}${EL}\n" ;; warn*) printf "\e[0F${S_WARN}${fmt:-%s}${@:3}${EL}\n" ;; info*) printf "\e[0F${S_INFO}${fmt:-%s}${@:3}${EL}\n" ;; noti*) printf "\e[0F${S_NOTICE}${fmt:-%s}${@:3}${EL}\n" ;; succ*) printf "\e[0F${S_SUCCESS}${fmt:-%s}${@:3}${EL}\n" ;; *) printf "\e[0F${S_INFO}${fmt:-%s}" "${@:3}" ;; esac } : "cli.color [] [ ...]" cli.color() { local fmt="${2:-}" fmt="${fmt//\\n}" case "${1:-}" in red) printf "${C_RED}${fmt:-%s}${C_NONE}" ${@:3} ;; blue) printf "${C_BLUE}${fmt:-%s}${C_NONE}" ${@:3} ;; green) printf "${C_GREEN}${fmt:-%s}${C_NONE}" ${@:3} ;; orange) printf "${C_ORANGE}${fmt:-%s}${C_NONE}" ${@:3} ;; yellow) printf "${C_YELLOW}${fmt:-%s}${C_NONE}" ${@:3} ;; purple) printf "${C_PURPLE}${fmt:-%s}${C_NONE}" ${@:3} ;; *) printf "${C_NONE}${fmt:-%s}${C_NONE}" ${@:3} ;; esac } : "cli.emphasis [] [ ...]" cli.emphasis() { local fmt="${2:-}" fmt="${fmt//\\n}" case "${1:-bold}" in red) printf "${C_RED}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; blue) printf "${C_BLUE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; green) printf "${C_GREEN}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; orange) printf "${C_ORANGE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; yellow) printf "${C_YELLOW}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; purple) printf "${C_PURPLE}${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; bold|*) printf "${F_BOLD}${fmt:-%s}${F_NONE}" "${@:3}" ;; esac } : "cli.bold " cli.bold() { cli.emphasis bold "%s" "${@}" } : "cli.tab " cli.tab() { s=${1:-2} ! [[ -z "${s//[0-9]}" ]] && s=2 printf "%${s:-2}s" " " } : "cli.items [] [ ...]" cli.items() { printf "${U_ITEM} ${1:-%s}" "${@:2}" } : "cli.items " cli.item() { cli.items "%s\n" "${@}" | head -1 } : "cli.title " cli.title() { cli.items "%s" "$(cli.emphasis blue "%s" "${@^^}")" cli.line } : "cli.subtitle " cli.subtitle() { printf "%0.s\u2508%s" cli.bold " ${@^^} " cli.get_cursor for i in $(seq 0 $((( ${COLS} - ${CURSOR_X} )))); do printf "%0.s\u2508" done cli.jump } : "cli.prompt []" cli.prompt() { read -p "$(cli.print ${1:-})" k echo ${k} } # Draws a horizontal line : "cli.line [nobreak]" cli.line() { [[ -z "${1:-}" ]] && cli.jump for i in $(seq 1 ${COLS}); do printf "%0.s\u2504" done [[ -z "${1:-}" ]] && cli.jump } # Draws a thin horizontal divisor line : "cli.boldline" cli.boldline() { for i in $(seq 1 ${COLS}); do printf "%0.s\u2581" done } : "cli.section <subtitle> <content>" cli.section() { __section.header() { __section.title() { printf $"${@:1:1}" \ $(echo -ne ${F_BOLD}${C_BLUE}${@:2:1}${F_NONE}) \ $(echo -ne ${C_ORANGE}${@:3}${F_NONE}) } __section.title.underline() { cli.boldline } __section.title "${@}" __section.title.underline } __section.content() { cli.jump 2 cli.fold ${COLS} "${@}" } __section.footer() { __section.footer.line() { cli.line } __section.footer.line } __section.header "${@:1:3}" __section.content "${@:4}" __section.footer } cli.self_compress() { cat cli.bash | \ gzip -c9 - | \ base64 -w77 | \ xargs printf ' %s \\\n' }