diff --git a/src/logging.sh b/src/logging.sh index d05da97..d2f2ec8 100644 --- a/src/logging.sh +++ b/src/logging.sh @@ -54,24 +54,24 @@ log() { case "${level}" in "$LOG_INFO") level="INFO" - color='\e[1;36m' + color='\033[1;36m' ;; "$LOG_DEBUG") level="DEBUG" - color='\e[1;34m' + color='\033[1;34m' ;; "$LOG_WARN") level="WARN" - color='\e[0;33m' + color='\033[0;33m' ;; "$LOG_ERROR") level="ERROR" - color='\e[0;31m' + color='\033[0;31m' ;; esac - echo -e "[${color}$(printf '%-5s' "${level}")\e[0m] \e[1;35m$(date +'%Y-%m-%dT%H:%M:%S')\e[0m ${message}" + echo -e "[${color}$(printf '%-5s' "${level}")\033[0m] \033[1;35m$(date +'%Y-%m-%dT%H:%M:%S')\033[0m ${message}" } diff --git a/src/prompts.sh b/src/prompts.sh index 53d40e3..fda50b7 100644 --- a/src/prompts.sh +++ b/src/prompts.sh @@ -8,18 +8,29 @@ _get_cursor_row() { read -sdR -p $'\E[6n' ROW COL; echo "${ROW#*[}"; } -_cursor_blink_on() { echo -en "\e[?25h" >&2; } -_cursor_blink_off() { echo -en "\e[?25l" >&2; } -_cursor_to() { echo -en "\e[$1;${2:-1}H" >&2; } +_cursor_blink_on() { echo -en "\033[?25h" >&2; } +_cursor_blink_off() { echo -en "\033[?25l" >&2; } +_cursor_to() { echo -en "\033[$1;$2H" >&2; } # key input helper _key_input() { - read -s -r -N1 key 2>/dev/null >&2 - case $key in - "A") echo "up"; ;; - "B") echo "down"; ;; - " ") echo "space"; ;; - $'\n') echo "enter" ;; + local ESC=$'\033' + local IFS='' + + read -rsn1 a + # is the first character ESC? + if [[ $ESC == $a ]]; then + read -rsn2 b + fi + + local input="${a}${b}" + case $input in + $ESC[A) echo up ;; + $ESC[B) echo down ;; + $ESC[C) echo right ;; + $ESC[D) echo left ;; + '') echo enter ;; + ' ') echo space ;; esac } @@ -36,7 +47,7 @@ _new_line_foreach_item() { # display prompt text without linebreak _prompt_text() { - echo -en "\e[32m?\e[0m\e[1m ${1}\e[0m " >&2 + echo -en "\033[32m?\033[0m\033[1m ${1}\033[0m " >&2 } # decrement counter $1, considering out of range for $2 @@ -84,16 +95,16 @@ input() { # if [ "$confirmed" = "0" ]; then echo "No?"; else echo "Yes!"; fi confirm() { _prompt_text "$1 (y/N)" - echo -en "\e[36m\c " >&2 + echo -en "\033[36m\c " >&2 local result="" echo -n " " >&2 until [[ "$result" == "y" ]] || [[ "$result" == "N" ]] do - echo -e "\e[1D\c " >&2 + echo -e "\033[1D\c " >&2 # shellcheck disable=SC2162 read -n1 result done - echo -en "\e[0m" >&2 + echo -en "\033[0m" >&2 case $result in y) echo -n 1; ;; @@ -136,7 +147,7 @@ list() { for opt in "${opts[@]}"; do _cursor_to $((startrow + idx)) if [ $idx -eq $selected ]; then - printf "\e[0m\e[36m\u276F\e[0m \e[36m%s\e[0m" "$opt" >&2 + printf "\033[0m\033[36m❯\033[0m \033[36m%s\033[0m" "$opt" >&2 else printf " %s" "$opt" >&2 fi @@ -157,7 +168,7 @@ list() { _cursor_to "${lastrow}" _cursor_blink_on - echo -n "${selected}" + echo -n "${selected}" } # @description Render a text based list of options, where multiple can be selected by the @@ -204,7 +215,7 @@ checkbox() { if [ $idx -eq $selected ]; then printf "%s \e[0m\e[36m\u276F\e[0m \e[36m%-50s\e[0m" "$icon" "$opt" >&2 else - printf "%s %-50s " "$icon" "$opt" >&2 + printf "%s %-50s" "$icon" "$opt" >&2 fi ((idx++)) done @@ -252,7 +263,7 @@ checkbox() { # pass=$(password "Enter password to use") password() { _prompt_text "$1" - echo -en "\e[36m" >&2 + echo -en "\033[36m" >&2 local password='' local IFS= while read -r -s -n1 char; do @@ -287,10 +298,10 @@ editor() { echo "" >&2 "${EDITOR:-vi}" "${tmpfile}" >/dev/tty - echo -en "\e[36m" >&2 + echo -en "\033[36m" >&2 # shellcheck disable=SC2002 cat "${tmpfile}" | sed -e 's/^/ /' >&2 - echo -en "\e[0m" >&2 + echo -en "\033[0m" >&2 cat "${tmpfile}" } diff --git a/src/user_feedback.sh b/src/user_feedback.sh index baa7266..8bab57d 100644 --- a/src/user_feedback.sh +++ b/src/user_feedback.sh @@ -7,7 +7,7 @@ # @example # show_error "Oh snap, that went horribly wrong" show_error() { - echo -e "\e[91;1m\u2718 $1\e[0m" >&2 + echo -e "\033[91;1m✘ $1\033[0m" >&2 } # @description Display success message in stderr, prefixed by cross emoji @@ -15,5 +15,5 @@ show_error() { # @example # show_success "There it is! World peace." show_success() { - echo -e "\e[92;1m\u2714 $1\e[0m" >&2 + echo -e "\033[92;1m✔ $1\033[0m" >&2 } diff --git a/test.sh b/test.sh index dd4bdf0..47a8ed1 100755 --- a/test.sh +++ b/test.sh @@ -41,13 +41,13 @@ validate_password() { fi } # Password prompt -pass=$(with_validate 'password "Enter random password"' validate_password) +#pass=$(with_validate 'password "Enter random password"' validate_password) # Checkbox checked=$(checkbox "Select one or more items" "${options[@]}") # text input with validation -text=$(with_validate 'input "Please enter something and confirm with enter"' validate_present) +#text=$(with_validate 'input "Please enter something and confirm with enter"' validate_present) # Select option=$(list "Select one item" "${options[@]}")