Postpone options display until after entering a dash

I was looking at how other programs that have an overwhelming
number of sub-commands and options deal with completion, namely
how git does it, and I liked the clean workflow not spitting every
available option until asked for. Hledger's main workflow is:

> hledger COMMAND QUERY

so I have tried to reproduce this with this change. Options are of
course still there, but not shown until you ask for them by entering
a dash on the command line. Also, the `acct:` filter proposes only top
level accounts until there is some input from the user because accounts
tend to be numerous as well.
This commit is contained in:
Vladimir Zhelezov 2020-12-12 06:43:06 +01:00
parent ddf55a86a4
commit e6d54f79d7
2 changed files with 38 additions and 18 deletions

View File

@ -48,9 +48,11 @@ _hledger_completion_function() {
return 0 return 0
fi fi
fi fi
# Replace dashes with underscores and use indirect expansion if [[ $cur == -* ]]; then
subcommandOptions=_hledger_complist_options_${subcommand//-/_} # Replace dashes with underscores and use indirect expansion
_hledger_compreply "$(_hledger_compgen "${!subcommandOptions}")" subcommandOptions=_hledger_complist_options_${subcommand//-/_}
_hledger_compreply "$(_hledger_compgen "${!subcommandOptions}")"
fi
break break
done done
@ -58,11 +60,12 @@ _hledger_completion_function() {
_hledger_compreply_optarg && return _hledger_compreply_optarg && return
if [[ -z $subcommand ]]; then if [[ -z $subcommand ]]; then
# Completion lists are already sorted at build-time compopt +o filenames
# This keeps commands and options grouped separately if [[ $cur == -* ]]; then
compopt -o nosort +o filenames _hledger_compreply "$(_hledger_compgen "$_hledger_complist_generic_options")"
_hledger_compreply "$(_hledger_compgen "$_hledger_complist_commands")" else
_hledger_compreply_append "$(_hledger_compgen "$_hledger_complist_generic_options")" _hledger_compreply "$(_hledger_compgen "$_hledger_complist_commands")"
fi
return 0 return 0
fi fi
@ -261,11 +264,18 @@ _hledger_compreply_optarg() {
_hledger_compreply_query() { _hledger_compreply_query() {
[[ $cur =~ .: ]] || return [[ $cur =~ .: ]] || return
local query=${cur%%:*}: local query=${cur%%:*}:
local match=${cur#*:}
grep -Fxqe "$query" <<< "$_hledger_complist_query_filters" || return grep -Fxqe "$query" <<< "$_hledger_complist_query_filters" || return
local hledgerArgs=() local hledgerArgs=()
case $query in case $query in
acct:) hledgerArgs=(accounts --flat) ;; acct:)
if (( ${#match} )); then
hledgerArgs=(accounts --flat)
else
hledgerArgs=(accounts --flat --depth 1)
fi
;;
code:) hledgerArgs=(codes) ;; code:) hledgerArgs=(codes) ;;
cur:) hledgerArgs=(commodities) ;; cur:) hledgerArgs=(commodities) ;;
desc:) hledgerArgs=(descriptions) ;; desc:) hledgerArgs=(descriptions) ;;

View File

@ -48,9 +48,11 @@ _hledger_completion_function() {
return 0 return 0
fi fi
fi fi
# Replace dashes with underscores and use indirect expansion if [[ $cur == -* ]]; then
subcommandOptions=_hledger_complist_options_${subcommand//-/_} # Replace dashes with underscores and use indirect expansion
_hledger_compreply "$(_hledger_compgen "${!subcommandOptions}")" subcommandOptions=_hledger_complist_options_${subcommand//-/_}
_hledger_compreply "$(_hledger_compgen "${!subcommandOptions}")"
fi
break break
done done
@ -58,11 +60,12 @@ _hledger_completion_function() {
_hledger_compreply_optarg && return _hledger_compreply_optarg && return
if [[ -z $subcommand ]]; then if [[ -z $subcommand ]]; then
# Completion lists are already sorted at build-time compopt +o filenames
# This keeps commands and options grouped separately if [[ $cur == -* ]]; then
compopt -o nosort +o filenames _hledger_compreply "$(_hledger_compgen "$_hledger_complist_generic_options")"
_hledger_compreply "$(_hledger_compgen "$_hledger_complist_commands")" else
_hledger_compreply_append "$(_hledger_compgen "$_hledger_complist_generic_options")" _hledger_compreply "$(_hledger_compgen "$_hledger_complist_commands")"
fi
return 0 return 0
fi fi
@ -261,11 +264,18 @@ _hledger_compreply_optarg() {
_hledger_compreply_query() { _hledger_compreply_query() {
[[ $cur =~ .: ]] || return [[ $cur =~ .: ]] || return
local query=${cur%%:*}: local query=${cur%%:*}:
local match=${cur#*:}
grep -Fxqe "$query" <<< "$_hledger_complist_query_filters" || return grep -Fxqe "$query" <<< "$_hledger_complist_query_filters" || return
local hledgerArgs=() local hledgerArgs=()
case $query in case $query in
acct:) hledgerArgs=(accounts --flat) ;; acct:)
if (( ${#match} )); then
hledgerArgs=(accounts --flat)
else
hledgerArgs=(accounts --flat --depth 1)
fi
;;
code:) hledgerArgs=(codes) ;; code:) hledgerArgs=(codes) ;;
cur:) hledgerArgs=(commodities) ;; cur:) hledgerArgs=(commodities) ;;
desc:) hledgerArgs=(descriptions) ;; desc:) hledgerArgs=(descriptions) ;;