ledger-completion.bash 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ### Assumption
  2. #
  3. # bash-completion package is installed and enabled
  4. #
  5. ### Just want to try it?
  6. #
  7. # $ source ledger-completion.bash
  8. #
  9. ### How to install?
  10. #
  11. #### For local user
  12. #
  13. # $ cat <<EOF >>~/.bash_completion
  14. # . ~/.bash_completion.d/ledger
  15. # EOF
  16. #
  17. # $ cp ledger-completion.bash ~/.bash_completion.d/ledger
  18. #
  19. #### For all users
  20. #
  21. # $ sudo cp ledger-completion.bash /etc/bash_completion.d/ledger
  22. #
  23. _ledger()
  24. {
  25. local cur prev command options
  26. COMPREPLY=()
  27. cur="${COMP_WORDS[COMP_CWORD]}"
  28. prev="${COMP_WORDS[COMP_CWORD-1]}"
  29. # COMMANDS
  30. #
  31. # Commands are found in source code:
  32. # report.cc::lookup "case symbol_t::COMMAND"
  33. # report.cc::lookupcase "case symbol_t::PRECOMMAND" : these are debug commands and they have been filtered out here
  34. #
  35. commands="accounts balance budget cleared commodities convert csv draft echo emacs entry equity lisp org payees pricemap prices pricesdb print register reload select source stats tags xact xml"
  36. # OPTIONS
  37. #
  38. # Options are found in source code:
  39. # global.cc::lookup_option
  40. # report.cc::lookup_option
  41. # session.cc::lookup_option
  42. #
  43. options="--abbrev-len= --account-width= --account= --actual --actual-dates --add-budget --amount-data --amount-width= --amount= --anon --ansi --args-only --auto-match --aux-date --average --balance-format= --base --basis --begin= --bold-if= --budget --budget-format= --by-payee --cache= --change --check-payees --cleared --cleared-format= --collapse --collapse-if-zero --color --columns= --cost --count --csv-format= --current --daily --date-format= --date-width= --date= --datetime-format= --day-break --days-of-week --dc --debug= --decimal-comma --depth= --detail --deviation --display-amount= --display-total= --display= --dow --download --effective --empty --end= --equity --exact --exchange= --explicit --file= --first= --flat --force-color --force-pager --forecast-while= --forecast-years= --forecast= --format= --full-help --gain --generated --group-by= --group-title-format= --head= --help --help-calc --help-comm --help-disp --historical --immediate --init-file= --inject= --input-date-format= --invert --last= --leeway= --limit= --lot-dates --lot-notes --lot-prices --lot-tags --lots --lots-actual --market --master-account= --meta-width= --meta= --monthly --no-aliases --no-color --no-pager --no-rounding --no-titles --no-total --now= --only= --options --output= --pager= --payee-width= --payee= --pedantic --pending --percent --period-sort= --period= --permissive --pivot= --plot-amount-format= --plot-total-format= --prepend-format= --prepend-width= --price --price-db= --price-exp= --pricedb-format= --prices-format= --primary-date --quantity --quarterly --raw --real --recursive-aliases --register-format= --related --related-all --revalued --revalued-only --revalued-total= --rich-data --script= --seed= --sort-all= --sort-xacts= --sort= --start-of-week= --strict --subtotal --tail= --time-colon --time-report --total-data --total-width= --total= --trace= --truncate= --unbudgeted --uncleared --unrealized --unrealized-gains= --unrealized-losses= --unround --value --value-expr= --values --verbose --verify --verify-memory --version --weekly --wide --yearly"
  44. # Bash FAQ E13 http://tiswww.case.edu/php/chet/bash/FAQ
  45. #
  46. COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
  47. # ACCOUNTS
  48. #
  49. # Accounts are generated with bash command:
  50. # $ ledger accounts>/tmp/accounts; for i in {1..5}; do cut -d : -f $i- /tmp/accounts;cut -d : -f -$i /tmp/accounts; done|sort -u|xargs
  51. #
  52. # Warning: this is working badly if there are spaces in account names
  53. #
  54. accounts="Assets Liabilities Equity Revenue Expenses"
  55. case $prev in
  56. --@(cache|file|init-file|output|pager|price-db|script))
  57. _filedir
  58. return 0
  59. ;;
  60. @(balance|equity|print|register))
  61. COMPREPLY=( $(compgen -W "${accounts}" -- ${cur}) )
  62. return 0
  63. ;;
  64. esac
  65. if [[ ${cur} == -* ]] ; then
  66. COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
  67. # elif [[ ${cur} == [A-Z]* ]] ; then
  68. # COMPREPLY=( $(compgen -W "${accounts}" -- ${cur}) )
  69. else
  70. COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
  71. fi
  72. return 0
  73. }
  74. complete -F _ledger ledger
  75. # Local variables:
  76. # mode: shell-script
  77. # sh-basic-offset: 4
  78. # sh-indent-comment: t
  79. # indent-tabs-mode: nil
  80. # End:
  81. # ex: ts=4 sw=4 et filetype=sh