Skip to content

Instantly share code, notes, and snippets.

@tgray
Created May 23, 2013 01:54
Show Gist options
  • Save tgray/5632281 to your computer and use it in GitHub Desktop.
Save tgray/5632281 to your computer and use it in GitHub Desktop.
# ledger v3.0 fish completions
# Tim Gray - 2013-05-22
# vim: set ft=fish norl ts=2 sw=2 sts=2 :
# check to see if we have an empty 'ledger' command.
function __fish_ledger_bare
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'ledger' ]
return 0
end
return 1
end
# check to see what ledger command we are using
function __fish_ledger_using_command
if test -n $argv[1]
return 0
end
set cmd (commandline -opc)
if [ (count $cmd) -gt 1 ]
for token in $argv
if [ (echo $cmd | grep -w $token) ]
return 0
end
end
end
return 1
end
function __fish_ledger_complete
complete -c ledger -n __fish_ledger_bare -f -u $argv
end
function __fish_ledger_opts_complete
complete -c ledger -n "not __fish_ledger_bare" -f -u $argv
end
function __fish_ledger_accounts
ledger accounts
end
function __fish_ledger_tags
ledger tags
end
function __fish_ledger_payees
ledger payees
end
function __fish_ledger_codes
ledger reg code . --register-format "%(code)\n"
end
function __fish_ledger_notes
ledger reg note . --register-format "%(note)\n" | sed 's/^[ ]*//'
end
function __fish_ledger_query
__fish_ledger_accounts
echo '@'
echo payee
echo note
echo tag
echo code
echo or
echo 'not'
echo 'and'
end
function __fish_ledger_word
set cmd (commandline -opc)[-1]
set token (commandline -ct)
set firstchar (echo $token | cut -c 1)
set firsttwochar (echo $token | cut -c 1,2)
if [ $firstchar = '@' ]
__fish_ledger_payees | sed 's/^/\@/'
else if [ $firstchar = '=' ]
__fish_ledger_notes | sed 's/^/=/'
end
if test -n $argv
set switchword $cmd
else
set switchword $token
end
switch $switchword
case tag '\%'
__fish_ledger_tags
case payee '@'
__fish_ledger_payees
case note '='
__fish_ledger_notes
case code
__fish_ledger_codes
case '#'
case '*'
__fish_ledger_query
end
end
function __fish_ledger_body
set token (commandline -ct)
if test -n $token
__fish_ledger_word $token
else
__fish_ledger_word
end
end
function __fish_ledger_cmds -d "returns ledger base commands and descriptions"
for c in $ledgercmds
echo $c | tr ':' '\t'
end
end
# base commands are here. Processed by __fish_ledger_cmds. Format is
# 'command:description'.
set ledgercmds \
'accounts:Lists all accounts' \
'balance:Display a balance report' \
'bal:Display a balance report' \
'b:Display a balance report' \
'budget:Display a budget report' \
'cleared:Display a cleared report' \
'commodities:Lists all commodities' \
'csv:Outputs data in CSV format' \
'entry:Generate a new entry' \
'xact:Generate a new entry' \
'emacs:Display postings in emacs format' \
'equity:Produce equity transaction' \
'eval:Evaluates expression' \
'org:Outputs data in org mode format' \
'payees:Lists all payees' \
'prices:Reports prices for all commodities' \
'pricedb:Reports commodity prices in pricedb format' \
'pricemap:Produces graphviz pricemap file' \
'print:Prints out full transaction' \
'r:List all postings' \
'reg:List all postings' \
'register:List all postings' \
'stats:Displays summary information' \
'xml:Outputs data in XML format' \
'args:Displays arugment analysis [debug]' \
'format:Format string analysis [debug]' \
'parse:Value expression analysis [debug]' \
'template:Draft template analysis [debug]' \
'source:Parses a journal file [debug]'
# Deprecated?
# __fish_ledger_complete -a select -d 'SQL query'
# __fish_ledger_complete -a generate -d 'Generate 50 random transactions'
# Options are below. They are divided into groups, based on which commands they work with. The format is 'shortoption:longoption:description'.
# for balance command
set balanceopts \
'E:empty:Also show empty accounts' \
':flat:Flatten account names' \
':no-total:Suppress the summary total' \
':percent:Show subtotals as percentages'
# for register command
set registeropts \
'j:amount-data:Display only date and amount' \
'A:average:Show the running average' \
'P:by-payee:Group postings by payee' \
'D:daily:Group postings by day' \
':dow:Group postings by the day of the week' \
':equity:Equity register report' \
':invert:Invert the value of amounts shown' \
'M:monthly:Group postings by month' \
':quarterly:Group postings by fiscal quarter' \
':start-of-week:Set the start of the week' \
's:subtotal:Report register as a single subtotal' \
'J:total-data:Display only date and total' \
'W:weekly:Group postings by week' \
'Y:yearly:Group postings by year'
# for register command - takes arguments (-r)
set registeropts_r \
'X:exchange:Valuate in given commodity' \
':head:Only show the top number postings' \
':meta-width:Set the width of the meta' \
':meta:Prepend report with TAG' \
':payee-width:Set the width of the payee' \
':prepend-format:Prepend report with STR' \
':prepend-width:Reserve N spaces on each line' \
'S:sort:Sort postings' \
':tail:Only show the last number postings'
# for print command
set printopts \
':raw:Print raw entry'
# for any command
# some of these might need to go elsewhere, but...
set otheropts \
':add-budget:Show un-budgeted postings' \
':anon:Anonymize entries' \
':args-only:Ignore init and env variables' \
':auto-match:' \
':aux-date:Use aux date' \
':base:' \
'B:basis:Report in terms of cost basis' \
':budget:Compare postings to budget' \
':check-payees:Strict and pedantic payee check' \
'C:cleared:Only show uncleared postings' \
'n:collapse:Only show top-most accounts' \
':collapse-if-zero:Collapse account if zero balance' \
':color:Use color' \
':cost:See --basis' \
'c:current:Don\'t show postings today' \
':day-break:' \
':dc:Display in debit/credit format' \
':decimal-comma:European comma standard' \
'D:deviation:Reports deviation from average value' \
'Q:download:Download quotes with getquote' \
'e:end:End reports on DATE' \
':exact:' \
':explicit:' \
':force-color:Force TTY color' \
':force-pager:Force pagination' \
':forecast:Project balances into future' \
':full-help:Shows man file' \
'G:gain:Show any gains (or losses)' \
':generated:Include auto-generated postings' \
':help-calc:Shows help' \
':help-comm:Shows man file' \
':help-disp:Shows man file' \
':help:Prints summary of all options' \
'H:historical:Show historical prices' \
':immediate:Evaluate immediately, not lazily' \
':lot-dates:Report commodity purchase date' \
':lot-notes:Report commodity purchase notes?' \
':lot-prices:Report commodity purchase price' \
':lots-actual:' \
':lots:Report purchase date and price' \
'V:market:Show current market prices' \
':no-color:Suppress color TTY output' \
':no-rounding:Don\'t round (see docs)' \
':no-titles:Suppress group titles' \
':options:Display options in effect' \
':payee:Sets value EXPR for payee format' \
':pedantic:Undeclared data causes errors' \
':pending:Only show pending postings' \
':period-sort:Sort postings in period' \
':permissive:' \
'I:price:Use purchase price' \
':primary-date:' \
'O:quantity:Report commodity totals' \
'R:real:Display only real postings' \
'r:related:Show related postings' \
':related-all:Also show related postings' \
':revalued-only:' \
':revalued:' \
':rich-data:Import additional CSV data as tags' \
':script:Execute a ledger script' \
':sort-all:' \
':strict:Undeclared data causes warnings' \
':unbudgeted:Show un-budgeted postings' \
'U:uncleared:Only show uncleared postings' \
':unrealized-gains:' \
':unrealized-losses:' \
':unrealized:' \
':unround:Display full precision' \
':verbose:Print detailed information' \
':verify-memory:' \
':verify:Enable additional assertions [debug]' \
':version:Display version' \
'w:wide:Output for 132 columns'
# for any command - takes arguments (-r)
set otheropts_r \
':abbrev-len:Min account length when shortened' \
':account-width:Set the width of the account' \
'a:account:Specify account' \
':actual-dates:Display actual dates' \
'L:actual:Display only actual postings' \
':amount-width:Set the width of the amount' \
't:amount:Calculation for displayed amount' \
':balance-format:Set balance format' \
'b:begin:Begin reports on DATE' \
':bold-if:Bold line if EXPR true' \
':budget-format:Set budget format' \
':cache:' \
':cleared-format:Set cleared format' \
':columns:Width of register report' \
':csv-format:Set csv format' \
'y:date-format:Set date format' \
':date-width:Width of date' \
':date:Transforms date by EXPR' \
':datetime-format:' \
':debug:Use debug options' \
':depth:Limit depth of account tree' \
':display-amount:Apply transform to displayed amount' \
':display-total:Apply transform to displayed total' \
'd:display:Limit display of postings' \
'f:file:read FILE as a ledger file' \
':first:See --head' \
':forecast-while:Also --forecast' \
':forecast-years:Forecast N years into future' \
'F:format:Set reporting format' \
':group-by:Group postings by EXPR' \
':group-title-format:Format for section headers' \
':import:' \
'i:init-file:Specify options file' \
':inject:Use expected amounts in calculations' \
':input-date-format:Input date format for entries' \
':last:See --tail' \
'Z:leeway:Expected price freshness in MINS' \
'l:limit:Limits postings in calculations' \
':master-account:Prepends all accounts wih STR' \
':now:Define current date for calculations' \
':only:Postings predicate ???' \
'o:output:Redirects output to FILE' \
':pager:Direct output to PAGER' \
'p:period:Show postings for given period' \
':pivot:Produce pivot table on TAG' \
':plot-amount-format:Format of amount for plot output' \
':plot-total-format:Format of total for plot ouput' \
':price-db:Commodities value FILE' \
':price-exp:See --leeway' \
':pricedb-format:Format of the price-db file' \
':prices-format:Set prices format' \
':register-format:Set register format' \
':revalued-total:' \
':sort-xacts:Sort within entry using EXPR' \
'T:total:Calculation for displayed total' \
':truncate:Truncation method'
# it's a shame I need the following command twice, one for the -r option and
# one for without. There's probably a better way to do it...
function __fish_ledger_opt_complete
# for options that don't need arguments
set cmds $argv[1]
set options $argv[2..-1]
for opt in $options
set short (echo $opt | cut -d ':' -f 1)
set long (echo $opt | cut -d ':' -f 2)
set desc (echo $opt | cut -d ':' -f 3)
if test -n $long; and test -n $short
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -l $long -s $short -d $desc
else if test -n $short
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -s $short -d $desc
else if test -n $long
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -l $long -d $desc
end
end
end
function __fish_ledger_opt_complete_r
# for options that need arguments (-r switch on complete)
set cmds $argv[1]
set options $argv[2..-1]
for opt in $options
set short (echo $opt | cut -d ':' -f 1)
set long (echo $opt | cut -d ':' -f 2)
set desc (echo $opt | cut -d ':' -f 3)
if test -n $long; and test -n $short
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -r -l $long -s $short -d $desc
else if test -n $short
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -r -s $short -d $desc
else if test -n $long
complete -c ledger -f -n "__fish_ledger_using_command $cmds" -r -l $long -d $desc
end
end
end
# generate completions
# first up is the basic commands
__fish_ledger_complete -f -a '(__fish_ledger_cmds)'
# then, check to see if we are in the middle of typing something - if so, check
# what it is and return something sensible
__fish_ledger_opts_complete -f -a "(__fish_ledger_body)"
# once we have our commands, complete with options that match the commands
__fish_ledger_opt_complete 'balance bal b' $balanceopts
__fish_ledger_opt_complete 'register reg r' $registeropts
__fish_ledger_opt_complete_r 'register reg r' $registeropts_r
__fish_ledger_opt_complete 'print' $printopts
# now options for any command
# first blank argument is needed to tell opt_complete not to search for command
__fish_ledger_opt_complete '' $otheropts
__fish_ledger_opt_complete_r '' $otheropts_r
# easier to one off this one
complete -c ledger -f -n '__fish_ledger_using_command commodities tags accounts payees' -l 'count' -d 'Count items'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment