As a ledger newbie, I have difficulties to remember all the commands
and options of ledger (and i'm lazy to open, man ledger, ledger.pdf,
etc), and I'm lazy too to type the long option (while long option is
easier to remember than the short ones maybe preferable for long time
hacker), so I decided to investigate around bash completion, and its
use for ledger.
While writing it, I made some design choices:
- it completes command (balance, register, etc.)
- it completes options, but only the long ones (it will complete --
basis, but not -B (who needs completion for one letter?))
- when option needs an argument, an '=' is added (to remember me that
a value is expected)
- when option needs an argument that is a file, it tries to complete
your file
I have tested against bash 4.1.5 and ledger 3.0.0-20100623, and it
worked for me.
Thierry
$ cat /home/thierry/.bash_completion.d/ledger
# Assumption : bash-completion package is installed and enabled
# Try it : just "source ledger"
# Here are three possible ways to install this file
# 1. As ~/.bash_completion file
# 2. As ~/.bash_completion.d/ledger file, with additional file
~/.bash_completion which contents ". ~/.bash_completion.d/ledger"
# 3. As /etc/bash_completion.d/ledger
_ledger()
{
local cur prev command options
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# 26 commands
# command explicitely filtered out: push pop reload draft
commands="accounts balance budget cleared commodities convert csv
echo emacs entry equity payees pricedb pricemap prices print register
server source stats xact xml"
# 151 options
options="--abbrev-len= --account= --account-width= --actual --
actual-dates --add-budget --amount= --amount-data --amount-width= --
anon --ansi --args-only --average --balance-format= --base --basis --
begin= --bold-if= --budget --budget-format= --by-payee --cache= --
cleared --cleared-format= --collapse --collapse-if-zero --color --
columns= --cost --count --csv-format= --current --daily --date= --date-
format= --datetime-format= --date-width= --days-of-week --debug= --
decimal-comma --depth= --deviation --display= --display-amount= --
display-total= --dow --download --effective --empty --end= --equity --
exact --exchange= --file= --first= --flat --force-color --force-pager
--forecast= --forecast-while= --forecast-years= --format= --full-help
--gain --generated --group-by= --group-title-format= --head= --help --
help-calc --help-comm --help-disp --import= --init-file= --inject= --
input-date-format= --invert --last= --leeway= --limit= --lot-dates --
lot-prices --lot-tags --lots --lots-actual --market --master-account=
--meta= --meta-width= --monthly --no-color --no-rounding --no-titles --
no-total --now= --only= --options --output= --pager= --payee= --payee-
width= --pending --percent --period= --period-sort --pivot= --plot-
amount-format= --plot-total-format= --prepend-format= --prepend-width=
--price --price-db= --price-exp= --prices-format= --pricedb-format= --
quantity --quarterly --raw --real --register-format= --related --
related-all --revalued --revalued-only --revalued-total= --seed= --
script= --sort= --sort-all= --sort-xacts= --start-of-week= --strict --
subtotal --tail= --total= --total-data --total-width= --trace= --
truncate= --unbudgeted --uncleared --unrealized --unrealized-gains= --
unrealized-losses= --unround --verbose --verify --version --weekly --
wide --yearly"
case $prev in
--@(cache|file|init-file|output|price-db))
_filedir
return 0
;;
esac
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
fi
return 0
}
complete -F _ledger ledger
# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh