Skip to content

Commit

Permalink
ask: support editing the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 11, 2023
1 parent 0d76d23 commit 6357fac
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function ask_() (
}

# process
local item args=() option_question='' option_default='' option_password='no' option_required='no' option_confirm='no' option_timeout=''
local item args=() option_question='' option_default='' option_password='no' option_required='no' option_confirm_default='yes' option_confirm_input='no' option_timeout=''
while test "$#" -ne 0; do
item="$1"
shift
Expand All @@ -116,8 +116,11 @@ function ask_() (
'--no-required'* | '--required'*)
option_required="$(get-flag-value required --missing="$option_required" -- "$item" | echo-affirmative --stdin)"
;;
'--no-confirm'* | '--confirm'*)
option_confirm="$(get-flag-value confirm --missing="$option_confirm" -- "$item" | echo-affirmative --stdin)"
'--no-confirm-default'* | '--confirm-default'*)
option_confirm_default="$(get-flag-value confirm-default --missing="$option_confirm_default" -- "$item" | echo-affirmative --stdin)"
;;
'--no-confirm-input'* | '--confirm-input'*)
option_confirm_input="$(get-flag-value confirm-input --missing="$option_confirm_input" -- "$item" | echo-affirmative --stdin)"
;;
'--')
args+=("$@")
Expand All @@ -144,16 +147,16 @@ function ask_() (
# helpers
function on_timeout {
if is-value "$RESULT"; then
echo-style --notice="Ask timed out, using fallback value: " --code="$RESULT" >/dev/stderr
echo-style --notice="Ask timed out. Using the fallback value: " --code="$RESULT" >/dev/stderr
sleep 5
print_line "$RESULT"
return 0
elif test "$option_required" = 'no'; then
echo-style --notice='Ask timed out, as the field was optional will use no value.' >/dev/stderr
echo-style --notice='Ask timed out. Had no fallback value, this is fine as the field was optional.' >/dev/stderr
sleep 5
return 0
else
echo-style --warning='Ask timed out, with no fallback.' >/dev/stderr
echo-style --warning='Ask timed out. Had no fallback value... the field was required.' >/dev/stderr
sleep 5
return 60 # ETIMEDOUT 60 Operation timed out
fi
Expand All @@ -163,10 +166,11 @@ function ask_() (
tty_auto
ASKED='yes' # not local
if test -n "${1-}"; then
print_line "$1" >"$tty_target"
echo-style --bold="$1" >"$tty_target"
fi
while true; do
read_status=0 && read -r -t 300 -r -p '> ' RESULT || read_status=$?
# -i requires -e
read_status=0 && read -r -t 300 -ei "$RESULT" -p '> ' RESULT || read_status=$?
if test "$read_status" -eq 142; then
return 60 # ETIMEDOUT 60 Operation timed out
fi
Expand All @@ -182,19 +186,24 @@ function ask_() (
function do_validate {
local choose_status ask_status choice choices=()
if is-value "$RESULT"; then
# we have a value, so go for it
if test "$option_confirm" != 'yes'; then
print_line "$RESULT"
return 0
fi
# proceed with confirm
# we have an input
if test "$ASKED" = 'yes'; then
# do we want to confirm the input value
if test "$option_confirm_input" = 'no'; then
print_line "$RESULT"
return 0
fi
if test "$option_password" = 'yes'; then
choices+=('existing' 'use the entered password')
else
choices+=('existing' "use the entered value: [$RESULT]")
fi
else
# do we want to confirm the default value
if test "$option_confirm_default" = 'no'; then
print_line "$RESULT"
return 0
fi
if test "$option_password" = 'yes'; then
choices+=('existing' 'use the preconfigured password')
else
Expand Down

0 comments on commit 6357fac

Please sign in to comment.