Skip to content

Commit ac8d0da

Browse files
committed
Merge branch 'master' into dev/devilbird
2 parents 13d7fad + dbce6f2 commit ac8d0da

File tree

3 files changed

+76
-35
lines changed

3 files changed

+76
-35
lines changed

commands/choose

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ function choose_test() (
3333
eval-tester --name='receive default --index responses with --skip-default' --stdout=$'1\n2' \
3434
-- env COLOR=no choose 'q' 'd' --multi --index --skip-default --default=b --default=c -- a b c
3535

36+
# custom match tests
37+
eval-tester --name='receive default first response with --skip-default and custom --match prep' --stdout=b-value \
38+
-- env COLOR=no choose 'q' 'd' --skip-default --default=b-value --label -- a-value a-label b-value b-label
39+
40+
eval-tester --name='receive default first response with --skip-default and custom --match label' --stdout=b-value \
41+
-- env COLOR=no choose 'q' 'd' --skip-default --default=b-label --match="\$LABEL" --label -- a-value a-label b-value b-label
42+
43+
eval-tester --name='receive default first response with --skip-default and custom --match index' --stdout=b-value \
44+
-- env COLOR=no choose 'q' 'd' --skip-default --default=1 --match="\$INDEX" --label -- a-value a-label b-value b-label
45+
46+
eval-tester --name='receive default first response with --skip-default and custom --match visual' --stdout=b-value \
47+
-- env COLOR=no choose 'q' 'd' --skip-default --default='1 b-value b-label' --visual="\$INDEX \$VALUE \$LABEL" --match="\$VISUAL" --label -- a-value a-label b-value b-label
48+
49+
eval-tester --name='receive default first response with --skip-default and custom --match return' --stdout='1 b-value b-label' \
50+
-- env COLOR=no choose 'q' 'd' --skip-default --default='1 b-value b-label' --return="\$INDEX \$VALUE \$LABEL" --match="\$RETURN" --label -- a-value a-label b-value b-label
51+
52+
eval-tester --name='receive default first response with --skip-default and custom --match index and label (part 1)' --stdout='b-value' \
53+
-- env COLOR=no choose 'q' 'd' --skip-default --default=1 --match="\$INDEX" --match="\$LABEL" --label -- a-value a-label b-value b-label
54+
55+
eval-tester --name='receive default first response with --skip-default and custom --match index and label (part 2)' --stdout='b-value' \
56+
-- env COLOR=no choose 'q' 'd' --skip-default --default='b-label' --match="\$INDEX" --match="\$LABEL" --label -- a-value a-label b-value b-label
57+
3658
# timeout optional
3759
eval-tester --name='receive no response by timeout with no input and optional' --stderr="q $timeout_optional" \
3860
-- env COLOR=no choose 'q' 'd' --timeout=5 -- a b c
@@ -629,9 +651,15 @@ function choose_() (
629651
If empty LABEL, then will equal VALUE.
630652
631653
--return='\$VALUE' -- ...[<value> <label>]
632-
Customise how the value is returned to the program. It is eval'd. E.g.
654+
Customise what is returned to the caller. It is eval'd. E.g.
633655
To return the visual, use: --return='\$VISUAL'
634-
Tto return the index, use: --return='\$INDEX' or --index
656+
To return the index, use: --return='\$INDEX' or --index
657+
658+
--match='\$VALUE' -- ...[<value> <label>]
659+
Customise what is matched to a default, can be specified multiple times. It is eval'd. E.g.
660+
To match the label, use: --match='\$LABEL'
661+
To match the index, use: --match='\$INDEX'
662+
To match either the value or the label, use: --match='\$VALUE' --match='\$LABEL'
635663
636664
--default-exact=<value>
637665
--defaults-exact=<newline separated values>
@@ -690,7 +718,7 @@ function choose_() (
690718
local args_string="$*"
691719
local item='' inputs=() tmp=()
692720
local option_question=()
693-
local option_label='no' option_visual='' option_return='$VALUE'
721+
local option_label='no' option_visual='' option_return='$VALUE' option_matches=()
694722
local defaults_exact=() defaults_fuzzy=() option_default_all='' option_confirm_solo='yes' option_confirm_default='yes' option_confirm_input='no' option_confirm_cancel='yes'
695723
local option_required='no' option_multi='no'
696724
local option_linger='no' option_timeout='' option_truncate_body='no'
@@ -707,6 +735,7 @@ function choose_() (
707735
'--visual='*) option_visual="${item#*=}" ;;
708736
'--return='*) option_return="${item#*=}" ;;
709737
'--index') option_return='$INDEX' ;;
738+
'--match='*) option_matches+=("${item#*=}") ;;
710739
'--default-exact='* | '--default='*) defaults_exact+=("${item#*=}") ;;
711740
'--defaults-exact='* | '--defaults='*)
712741
mapfile -t tmp <<<"${item#*=}"
@@ -771,6 +800,9 @@ function choose_() (
771800
if [[ ${#inputs[@]} -eq 0 ]]; then
772801
help "choose: no <item>s provided: $0 $args_string"
773802
fi
803+
if [[ ${#option_matches[@]} -eq 0 ]]; then
804+
option_matches=('$VALUE')
805+
fi
774806

775807
# question
776808
local question_title question_body
@@ -851,7 +883,7 @@ function choose_() (
851883

852884
# generate the items
853885
# @todo add support for option_defaults_indexes if a user requests it
854-
local index item INDEX=-1 VALUE LABEL VISUAL RETURN items=() returns=() defaults=() items_count can_skip_prompt='no' defaults_indexes=() defaults_count=0 defaults_last=-1 fallbacks_indexes=() fallbacks_count=0 fallbacks_last=-1
886+
local index item INDEX=-1 VALUE LABEL VISUAL RETURN MATCH items=() returns=() defaults=() items_count can_skip_prompt='no' defaults_indexes=() defaults_count=0 defaults_last=-1 fallbacks_indexes=() fallbacks_count=0 fallbacks_last=-1
855887
for ((index = 0; index < ${#inputs[@]}; index = index + inputs_step)); do
856888
# index considers inputs_step, INDEX is only each item (label/value combo)
857889
INDEX=$((INDEX + 1))
@@ -881,26 +913,6 @@ function choose_() (
881913
fi
882914
fi
883915

884-
# enable if default
885-
if [[ $option_default_all == 'yes' ]]; then
886-
defaults[INDEX]='yes'
887-
else
888-
if [[ ${#defaults_exact[@]} -ne 0 ]]; then # bash v3 compat
889-
for item in "${defaults_exact[@]}"; do
890-
if [[ $VALUE == "$item" ]]; then
891-
defaults[INDEX]='yes'
892-
fi
893-
done
894-
fi
895-
if [[ ${#defaults_fuzzy[@]} -ne 0 ]]; then # bash v3 compat
896-
for item in "${defaults_fuzzy[@]}"; do
897-
if __string_has_case_insensitive_substring "$VALUE" "$item"; then
898-
defaults[INDEX]='yes'
899-
fi
900-
done
901-
fi
902-
fi
903-
904916
# generate what is used
905917
if [[ -n $option_visual ]]; then
906918
eval "VISUAL=\"$option_visual\""
@@ -911,17 +923,45 @@ function choose_() (
911923
__print_lines "${style__error}Invalid visual=[$VISUAL] for label=[$LABEL] value=[$VALUE], all must be non-empty.${style__end__error}" >/dev/stderr
912924
return 22 # EINVAL 22 Invalid argument
913925
fi
926+
items+=("$VISUAL")
914927
if [[ -n $option_return ]]; then
915928
eval "RETURN=\"$option_return\""
916929
else
917930
RETURN="$VALUE"
918931
fi
919932
if [[ -z $RETURN ]]; then
920-
__print_lines "${style__error}Invalid return=[$RETURN] for label=[$LABEL] value=[$VALUE], all must be non-empty.${style__end__error}" >/dev/stderr
933+
__print_lines "${style__error}Invalid return=[$RETURN] for label=[$LABEL] value=[$VALUE] visual=[$VISUAL], all must be non-empty.${style__end__error}" >/dev/stderr
921934
return 22 # EINVAL 22 Invalid argument
922935
fi
923-
items+=("$VISUAL")
924936
returns+=("$RETURN")
937+
938+
# enable if default
939+
if [[ $option_default_all == 'yes' ]]; then
940+
defaults[INDEX]='yes'
941+
else
942+
for MATCH in "${option_matches[@]}"; do
943+
eval "MATCH=\"$MATCH\""
944+
if [[ -z $MATCH ]]; then
945+
__print_lines "${style__error}Invalid match=[$MATCH] for label=[$LABEL] value=[$VALUE] visual=[$VISUAL] return=[$RETURN], all must be non-empty.${style__end__error}" >/dev/stderr
946+
return 22 # EINVAL 22 Invalid argument
947+
fi
948+
949+
if [[ ${#defaults_exact[@]} -ne 0 ]]; then # bash v3 compat
950+
for item in "${defaults_exact[@]}"; do
951+
if [[ $MATCH == "$item" ]]; then
952+
defaults[INDEX]='yes'
953+
fi
954+
done
955+
fi
956+
if [[ ${#defaults_fuzzy[@]} -ne 0 ]]; then # bash v3 compat
957+
for item in "${defaults_fuzzy[@]}"; do
958+
if __string_has_case_insensitive_substring "$MATCH" "$item"; then
959+
defaults[INDEX]='yes'
960+
fi
961+
done
962+
fi
963+
done
964+
fi
925965
done
926966
items_count="${#items[@]}"
927967
if [[ $items_count -eq 1 && $option_required == 'yes' ]]; then

commands/secret

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ function secret_() (
198198
# database
199199
local sudo_reason='Secret is requesting your sudo/root/login password to securely store and access its secrets.'
200200
local database="$DOROTHY/user/config.local/secrets.json"
201+
local op_cli_config="$XDG_CONFIG_HOME/op"
201202
function correct_database_permission {
202-
fs-own --me --u --optional -- "$database" "$XDG_CONFIG_HOME/op" >"$terminal_device_file"
203+
fs-own --me --u --optional --recursive -- "$database" "$op_cli_config" >"$terminal_device_file"
203204
}
204205
function correct_key_permission {
205206
local state_key="$1"
@@ -413,9 +414,10 @@ function secret_() (
413414
fi
414415
if [[ $status -ne 0 ]]; then
415416
echo-error \
416-
"Failed to sign into [$OP_SUBDOMAIN] via the email [$email] and account key [$key]." \
417+
"Failed to sign into [$OP_SUBDOMAIN] via the email [$email] and account key [$key]." --newline \
418+
"STATUS = [$status]" --newline \
417419
"OP_SESSION = [$OP_SESSION]"
418-
return 1
420+
return "$status"
419421
fi
420422
if [[ -n $OP_SESSION ]]; then
421423
cache_write 'OP_SESSION' "$OP_SESSION"
@@ -537,7 +539,7 @@ function secret_() (
537539
# filter or ask
538540
choose \
539541
--question="Which vault did you want with [$vault]?" \
540-
--default="$vault" --label --visual="\$LABEL [\$VALUE]" \
542+
--default="$vault" --label --visual="\$LABEL [\$VALUE]" --match="\$VALUE" --match="\$LABEL" \
541543
-- "${tuples[@]}"
542544
}
543545

@@ -555,7 +557,7 @@ function secret_() (
555557
# filter or ask
556558
choose \
557559
--question="Which item did you want with [$item]?" \
558-
--default="$item" --label --visual="\$LABEL [\$VALUE]" \
560+
--default="$item" --label --visual="\$LABEL [\$VALUE]" --match="\$VALUE" --match="\$LABEL" \
559561
-- "${tuples[@]}"
560562
}
561563

@@ -579,7 +581,7 @@ function secret_() (
579581
# filter or ask
580582
choose \
581583
--question="Which field did you want with [$field]?" \
582-
--default="$field" --label --visual="\$LABEL [\$VALUE]" --return="$wants" \
584+
--default="$field" --label --visual="\$LABEL [\$VALUE]" --return="$wants" --match="\$LABEL" \
583585
-- "${tuples[@]}"
584586
}
585587

init.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ else
6262
DOROTHY_LOAD='no' # this must be outside the below if, to ensure DOROTHY_LOAD is reset, and DOROTHY_LOADED is respected, otherwise posix shells may double load due to cross-compat between dotfiles (.profile along with whatever they support)
6363
if [ -z "${DOROTHY_LOADED_SHARED_SCOPE-}" ]; then
6464
# `-dash` is macos login shell, `dash` is manual `dash -l` invocation (as $- doesn't include l in dash)
65-
if [ "$0" = '-bash' ] || [ "$0" = '-zsh' ] || [ "$0" = '-dash' ] || [ "$0" = 'dash' ]; then
65+
# NVIM here because it sets $0 as whichever shell invoked Neovim: https://github.com/bevry/dorothy/pull/279$0
66+
if [ "$0" = '-bash' ] || [ "$0" = '-zsh' ] || [ "$0" = '-dash' ] || [ "$0" = 'dash' ] || [ -n "${NVIM-}" ]; then
6667
DOROTHY_LOAD='yes'
6768
elif [ -n "${BASH_VERSION-}" ]; then
6869
# trunk-ignore(shellcheck/SC3044)
@@ -78,8 +79,6 @@ else
7879
fi
7980
elif [ -z "$-" ] && [ -z "$*" ] && [ "${CI-}" = 'true' ]; then
8081
DOROTHY_LOAD='yes' # dash on github ci, in which [$-] and [$*] are empty, and $0 = /home/runner....
81-
elif [ -n "${NVIM-}" ]; then
82-
DOROTHY_LOAD='yes' # neovim: $0 = init.sh, and BASH_SOURCE[*] is undefined
8382
else
8483
# bash v3 and dash do not set l in $-
8584
# zsh does, however zsh we have a definite option earlier

0 commit comments

Comments
 (0)