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

View File

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