Skip to content

Commit

Permalink
ask, choose, confirm: --confirm*, --linger, --question
Browse files Browse the repository at this point in the history
changes:

- bash.bash:
    - native __ prefix on methods, b/c alias (rather than non-performant inverse)
    - removed unused __print_lines_no_trail
- ask, choose, confirm:
    - fix question title and body implementation, title is uniformly bold
    - linger answer will be dim
    - fix outdated linger option implementation
    - implement confirm on choose
    - implement linger on confirm
    - new options implemented in caller where appropriate
- dorothy: remove require_globstar as already provided by bash.bash
- get-flag-value:
    - remove unused complex --*-fallback options, fix inversions
    - update tests
- macos-settings: remove fodder usage, was unnecessary

todos:

- need to update tests for ask, confirm, choose
  • Loading branch information
balupton committed May 1, 2024
1 parent 9dd6c88 commit d332953
Show file tree
Hide file tree
Showing 153 changed files with 1,233 additions and 995 deletions.
2 changes: 1 addition & 1 deletion commands.beta/echo-exit-status
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function echo_exit_status() (

local cmd_status
eval_capture --statusvar=cmd_status -- "${cmd[@]}"
print_line "$cmd_status"
__print_line "$cmd_status"
)

# fire if invoked standalone
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-if-directory
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function echo_if_directory() (

function on_input {
if test -n "$1" -a -d "$1"; then
print_line "$1"
__print_line "$1"
fi
}

Expand Down
4 changes: 2 additions & 2 deletions commands.beta/echo-if-empty
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function echo_if_empty() (
}
function on_finish {
if is-empty-string -- "$inputs"; then
print_string "$fallback"
__print_string "$fallback"
else
print_string "$inputs"
__print_string "$inputs"
fi
}
stdinargs "${option_args[@]}"
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-if-executable
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function echo_if_executable() (

function on_input {
if test -n "$1" -a -x "$1"; then
print_line "$1"
__print_line "$1"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-if-path
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function echo_if_path() (

function on_input {
if is-present -- "$1"; then
print_line "$1"
__print_line "$1"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-nonflags
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function echo_nonflags() (

function on_input {
if [[ $1 != -* ]]; then
print_line "$1"
__print_line "$1"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-numeric
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function echo_numeric() (
local had_an_invalid_input=no
function on_input {
if is-number -- "$1"; then
print_line "$1"
__print_line "$1"
else
echo-style --error="[$1] is not a number" >/dev/stderr
had_an_invalid_input=yes
Expand Down
2 changes: 1 addition & 1 deletion commands.beta/echo-values
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function echo_values() (

function on_input {
if is-value -- "$1"; then
print_line "$1"
__print_line "$1"
fi
}

Expand Down
6 changes: 3 additions & 3 deletions commands.beta/edit-dns
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ function edit_dns() (
# edit /etc/resolv.conf

if get-app --quiet -- 'ProtonVPN'; then
print_line 'ProtonVPN enforces its own DNS'
__print_line 'ProtonVPN enforces its own DNS'
elif command-exists dnscrypt-proxy; then
print_line 'Editing DNS configuration of dnscrypt-proxy'
__print_line 'Editing DNS configuration of dnscrypt-proxy'
edit --wait -- "${HOMEBREW_PREFIX:-"/usr/local"}/etc/dnscrypt-proxy.toml"
else
print_line 'DNS editing only supported for dnscrypt-proxy'
__print_line 'DNS editing only supported for dnscrypt-proxy'
fi
)

Expand Down
8 changes: 4 additions & 4 deletions commands.beta/ensure-trailing-slash
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ function ensure_trailing_slash() (
for path in "${option_paths[@]}"; do
if test -d "$path"; then
if [[ $path != */ ]]; then
print_line "$path/"
__print_line "$path/"
else
print_line "$path"
__print_line "$path"
fi
elif test -f "$path"; then
if [[ $path == */ ]]; then
length="${#path}"
print_line "${path:0:length-1}"
__print_line "${path:0:length-1}"
else
print_line "$path"
__print_line "$path"
fi
else
echo-style --error='Not a file nor directory:' ' ' --code="$path" >/dev/stderr
Expand Down
8 changes: 4 additions & 4 deletions commands.beta/get-git-default-branch
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function get_git_default_branch() (
default_global="$(git config --global init.defaultBranch 2>/dev/null || :)"

if test -n "$default_local" && __branch_exists "$default_local"; then
print_line "$default_local"
__print_line "$default_local"
elif test -n "$default_global" && __branch_exists "$default_global"; then
print_line "$default_global"
__print_line "$default_global"
elif __branch_exists 'main'; then
print_line 'main'
__print_line 'main'
elif __branch_exists 'master'; then
print_line 'master'
__print_line 'master'
else
echo-error 'Unable to determine the default branch.'
return 1
Expand Down
6 changes: 3 additions & 3 deletions commands.beta/icloud-helper
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ function icloud_helper() (
# .screenflow does not play niceley with iCloud Drive, as it is a secret directory
if test ! -d "$path" && [[ $path != *".DS_Store" && $path != *".icloud" ]]; then
if [[ $path == *".screenflow"* || $path == *".sketch"* ]]; then
print_line "skipped $path, as it is a directory in disguise as a file, and can only evict files"
__print_line "skipped $path, as it is a directory in disguise as a file, and can only evict files"
else
brctl evict "$path"
fi
else
print_line "skipped $path, as it is a directory or excluded path"
__print_line "skipped $path, as it is a directory or excluded path"
fi
done
}
Expand All @@ -103,7 +103,7 @@ function icloud_helper() (
if test "$(type -t "$action")" = 'function'; then
"$action" "${option_args[@]}"
else
print_line "Action [$action] not yet implemented." >/dev/stderr
__print_line "Action [$action] not yet implemented." >/dev/stderr
return 78 # ENOSYS 78 Function not implemented
fi
)
Expand Down
6 changes: 3 additions & 3 deletions commands.beta/itunes-owners
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function itunes_owners() (
# check
if test ! -f "$song"; then
echo-style "$song" ' ' --notice='is missing'
print_line "$song" >>"$missing_list"
__print_line "$song" >>"$missing_list"
continue
fi

Expand All @@ -90,11 +90,11 @@ function itunes_owners() (
# extract
local owners owner_count owned_count
owners="$(cut -f1 -s <"$owner_list" | sort | uniq)"
owner_count="$(print_line "$owners" | wc -l | xargs)"
owner_count="$(__print_line "$owners" | wc -l | xargs)"
owned_count="$(wc -l <"$owner_list" | xargs)"

echo-style --h2="$owner_count UNIQUE OWNERS:"
print_line "$owners"
__print_line "$owners"
echo

echo-style --h2="$owned_count OWNED MEDIA:"
Expand Down
15 changes: 6 additions & 9 deletions commands.beta/macos-settings
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

function macos_settings() (
source "$DOROTHY/sources/bash.bash"
require_array 'mapfile'
__require_array 'mapfile'

# =====================================
# Arguments
Expand Down Expand Up @@ -53,9 +53,9 @@ function macos_settings() (
# Get Preferences

# ask
local fodder choices
fodder="$(
choose --multi \
local choices=()
mapfile -t choices < <(
choose --linger --confirm --multi \
--question="Which settings would you like to enable" \
--label -- \
dockside "Select which side of the screen the dock is on" \
Expand All @@ -80,13 +80,10 @@ function macos_settings() (
disableautocorrect "Disable autocorrect" \
updatedaily "Check for software updates daily" \
devcrashes "Show debug information when an application crashes"
)"
if test -z "$fodder"; then
print_line 'No choices selected.'
)
if test "${#choices[@]}" -eq 0; then
return 0
fi
mapfile -t choices <<<"$fodder" # <( destroys stdin
print_line "Proceeding with these choices: ${choices[*]}"

# -------------------------------------
# Dock
Expand Down
21 changes: 10 additions & 11 deletions commands.beta/macos-state
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ function macos_state() (

# ensure
action="$(
choose \
--question='What do you want to do?' \
choose --linger --question='What do you want to do?' \
--label --default="$action" -- \
backup 'Make a backup?' \
restore 'Restore a backup?'
)"

# What is the local volume?
local_root="$(
choose --required \
choose --linger --required \
--question='What is your local volume?' \
--default="$local_root" -- /Volumes/*
)"
Expand All @@ -84,17 +83,17 @@ function macos_state() (
if test "$action" = 'restore' -a -z "$backup_root"; then
echo
if
confirm --bool --ppid=$$ -- 'Do you want to restore from Time Machine?' \
confirm --linger --bool --ppid=$$ -- 'Do you want to restore from Time Machine?' \
"Ensure either:" \
"- Time Machine is configured in system preferences" \
"- The Time Machine Backup Volume is already mounted"
then
backup_root="$(tmutil latestbackup || :)"
if test ! -d "$backup_root"; then
echo
print_line 'Unable to find the Time Machine backup automatically, attempting manual resolution...'
__print_line 'Unable to find the Time Machine backup automatically, attempting manual resolution...'
time_backup_volume="$(
choose --required \
choose --linger --required \
--question='Which volume contains the time machine backups?' \
-- /Volumes/*
)"
Expand All @@ -106,10 +105,10 @@ function macos_state() (
backup_root="$time_backup_machine/Latest"
fi
if test -d "$backup_root"; then
print_line "Time Machine Backup: $backup_root"
__print_line "Time Machine Backup: $backup_root"
backup_type='tm'
else
print_line 'Time Machine backup could not be determined' >/dev/stderr
__print_line 'Time Machine backup could not be determined' >/dev/stderr
backup_root=''
fi
fi
Expand Down Expand Up @@ -173,7 +172,7 @@ function macos_state() (
else
# ask
backup_root="$(
choose --required \
choose --linger --required \
--question='Which backup volume to use?' \
-- "$(fs-join -- "$backup_root" 'Volumes')/"*
)"
Expand Down Expand Up @@ -201,7 +200,7 @@ function macos_state() (
function do_remove {
local path="$1"
if is-present -- "$path"; then
if confirm --positive --ppid=$$ -- "^ will be replaced, continue?"; then
if confirm --linger --positive --ppid=$$ -- '^ will be replaced, continue?'; then
fs-rm --quiet --no-confirm --sudo -- "$path"
fi
fi
Expand Down Expand Up @@ -273,7 +272,7 @@ function macos_state() (
function process {
local title="$1" paths=("${@:1}") path=''
echo-segment --h1 "$title"
if confirm --bool --ppid=$$ -- "Do you want to $action $title"; then
if confirm --linger --bool --ppid=$$ -- "Do you want to $action $title"; then
for path in "${paths[@]}"; do
if test "$action" = 'backup'; then
do_backup "$path"
Expand Down
24 changes: 12 additions & 12 deletions commands.beta/mail-sync
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function mail_sync() (

# origin IMAP server
type1="$(
choose --required \
choose --linger --required \
--question='Who is the host of the origin IMAP server?' \
--default="$type1" -- "${types[@]}"
)"
Expand All @@ -95,36 +95,36 @@ function mail_sync() (
else
args+=(
--host1 "$(
ask --required \
ask --linger --required \
--question="What is the origin IMAP server hostname?"
)"
)
if confirm --positive --ppid=$$ -- "Does the origin server use SSL?"; then
if confirm --linger --positive --ppid=$$ -- "Does the origin server use SSL?"; then
args+=(--ssl1)
fi
args+=(
--port1 "$(
ask --required \
ask --linger --required \
--question="What is the origin IMAP server's port?"
)"
)
fi
args+=(
--user1 "$(
ask --required \
ask --linger --required \
--question="What is the origin IMAP server's username?" \
--default="$user1"
)"
--password1 "$(
ask --required --password \
ask --linger --required --password \
--question="What is the origin IMAP server's password?" \
--default="$password1"
)"
)

# target IMAP server
type2="$(
choose --required \
choose --linger --required \
--question='Who is the host of the target IMAP server?' \
--default="$type2" -- "${types[@]}"
)"
Expand All @@ -148,24 +148,24 @@ function mail_sync() (
--question="What is the target IMAP server's hostname?"
)"
)
if confirm --positive --ppid=$$ -- "Does the target server use SSL?"; then
if confirm --linger --positive --ppid=$$ -- "Does the target server use SSL?"; then
args+=(--ssl2)
fi
args+=(
--port2 "$(
ask --required \
ask --linger --required \
--question="What is the target IMAP server's port?"
)"
)
fi
args+=(
--user2 "$(
ask --required \
ask --linger --required \
--question="What is the target IMAP server's username?" \
--default="$user2"
)"
--password2 "$(
ask --required --password \
ask --linger --required --password \
--question="What is the target IMAP server's password?" \
--default="$password2"
)"
Expand All @@ -182,7 +182,7 @@ function mail_sync() (
fi

# delete?
if confirm --negative --ppid=$$ -- "Delete messages from the origin IMAP server?" "Do this only once you have already completed a successful synchronisation, otherwise only the first label/folder will be applied."; then
if confirm --linger --negative --ppid=$$ -- "Delete messages from the origin IMAP server?" "Do this only once you have already completed a successful synchronisation, otherwise only the first label/folder will be applied."; then
args+=(--delete1 --delete1emptyfolders --expunge1) # --noexpungeaftereach
fi

Expand Down

0 comments on commit d332953

Please sign in to comment.