fix: Full compability on macos (#2)

* fix: Make first macos select work

* fix: Inline emjoys
This commit is contained in:
Timo Reymann
2023-02-17 12:50:24 +01:00
committed by GitHub
parent 487d7469b7
commit 9fa16c8295
4 changed files with 39 additions and 28 deletions
+5 -5
View File
@@ -54,24 +54,24 @@ log() {
case "${level}" in case "${level}" in
"$LOG_INFO") "$LOG_INFO")
level="INFO" level="INFO"
color='\e[1;36m' color='\033[1;36m'
;; ;;
"$LOG_DEBUG") "$LOG_DEBUG")
level="DEBUG" level="DEBUG"
color='\e[1;34m' color='\033[1;34m'
;; ;;
"$LOG_WARN") "$LOG_WARN")
level="WARN" level="WARN"
color='\e[0;33m' color='\033[0;33m'
;; ;;
"$LOG_ERROR") "$LOG_ERROR")
level="ERROR" level="ERROR"
color='\e[0;31m' color='\033[0;31m'
;; ;;
esac 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}"
} }
+30 -19
View File
@@ -8,18 +8,29 @@ _get_cursor_row() {
read -sdR -p $'\E[6n' ROW COL; read -sdR -p $'\E[6n' ROW COL;
echo "${ROW#*[}"; echo "${ROW#*[}";
} }
_cursor_blink_on() { echo -en "\e[?25h" >&2; } _cursor_blink_on() { echo -en "\033[?25h" >&2; }
_cursor_blink_off() { echo -en "\e[?25l" >&2; } _cursor_blink_off() { echo -en "\033[?25l" >&2; }
_cursor_to() { echo -en "\e[$1;${2:-1}H" >&2; } _cursor_to() { echo -en "\033[$1;$2H" >&2; }
# key input helper # key input helper
_key_input() { _key_input() {
read -s -r -N1 key 2>/dev/null >&2 local ESC=$'\033'
case $key in local IFS=''
"A") echo "up"; ;;
"B") echo "down"; ;; read -rsn1 a
" ") echo "space"; ;; # is the first character ESC?
$'\n') echo "enter" ;; 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 esac
} }
@@ -36,7 +47,7 @@ _new_line_foreach_item() {
# display prompt text without linebreak # display prompt text without linebreak
_prompt_text() { _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 # decrement counter $1, considering out of range for $2
@@ -84,16 +95,16 @@ input() {
# if [ "$confirmed" = "0" ]; then echo "No?"; else echo "Yes!"; fi # if [ "$confirmed" = "0" ]; then echo "No?"; else echo "Yes!"; fi
confirm() { confirm() {
_prompt_text "$1 (y/N)" _prompt_text "$1 (y/N)"
echo -en "\e[36m\c " >&2 echo -en "\033[36m\c " >&2
local result="" local result=""
echo -n " " >&2 echo -n " " >&2
until [[ "$result" == "y" ]] || [[ "$result" == "N" ]] until [[ "$result" == "y" ]] || [[ "$result" == "N" ]]
do do
echo -e "\e[1D\c " >&2 echo -e "\033[1D\c " >&2
# shellcheck disable=SC2162 # shellcheck disable=SC2162
read -n1 result read -n1 result
done done
echo -en "\e[0m" >&2 echo -en "\033[0m" >&2
case $result in case $result in
y) echo -n 1; ;; y) echo -n 1; ;;
@@ -136,7 +147,7 @@ list() {
for opt in "${opts[@]}"; do for opt in "${opts[@]}"; do
_cursor_to $((startrow + idx)) _cursor_to $((startrow + idx))
if [ $idx -eq $selected ]; then 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 else
printf " %s" "$opt" >&2 printf " %s" "$opt" >&2
fi fi
@@ -157,7 +168,7 @@ list() {
_cursor_to "${lastrow}" _cursor_to "${lastrow}"
_cursor_blink_on _cursor_blink_on
echo -n "${selected}" echo -n "${selected}"
} }
# @description Render a text based list of options, where multiple can be selected by the # @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 if [ $idx -eq $selected ]; then
printf "%s \e[0m\e[36m\u276F\e[0m \e[36m%-50s\e[0m" "$icon" "$opt" >&2 printf "%s \e[0m\e[36m\u276F\e[0m \e[36m%-50s\e[0m" "$icon" "$opt" >&2
else else
printf "%s %-50s " "$icon" "$opt" >&2 printf "%s %-50s" "$icon" "$opt" >&2
fi fi
((idx++)) ((idx++))
done done
@@ -252,7 +263,7 @@ checkbox() {
# pass=$(password "Enter password to use") # pass=$(password "Enter password to use")
password() { password() {
_prompt_text "$1" _prompt_text "$1"
echo -en "\e[36m" >&2 echo -en "\033[36m" >&2
local password='' local password=''
local IFS= local IFS=
while read -r -s -n1 char; do while read -r -s -n1 char; do
@@ -287,10 +298,10 @@ editor() {
echo "" >&2 echo "" >&2
"${EDITOR:-vi}" "${tmpfile}" >/dev/tty "${EDITOR:-vi}" "${tmpfile}" >/dev/tty
echo -en "\e[36m" >&2 echo -en "\033[36m" >&2
# shellcheck disable=SC2002 # shellcheck disable=SC2002
cat "${tmpfile}" | sed -e 's/^/ /' >&2 cat "${tmpfile}" | sed -e 's/^/ /' >&2
echo -en "\e[0m" >&2 echo -en "\033[0m" >&2
cat "${tmpfile}" cat "${tmpfile}"
} }
+2 -2
View File
@@ -7,7 +7,7 @@
# @example # @example
# show_error "Oh snap, that went horribly wrong" # show_error "Oh snap, that went horribly wrong"
show_error() { 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 # @description Display success message in stderr, prefixed by cross emoji
@@ -15,5 +15,5 @@ show_error() {
# @example # @example
# show_success "There it is! World peace." # show_success "There it is! World peace."
show_success() { show_success() {
echo -e "\e[92;1m\u2714 $1\e[0m" >&2 echo -e "\033[92;1m $1\033[0m" >&2
} }
+2 -2
View File
@@ -41,13 +41,13 @@ validate_password() {
fi fi
} }
# Password prompt # Password prompt
pass=$(with_validate 'password "Enter random password"' validate_password) #pass=$(with_validate 'password "Enter random password"' validate_password)
# Checkbox # Checkbox
checked=$(checkbox "Select one or more items" "${options[@]}") checked=$(checkbox "Select one or more items" "${options[@]}")
# text input with validation # 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 # Select
option=$(list "Select one item" "${options[@]}") option=$(list "Select one item" "${options[@]}")