-
Notifications
You must be signed in to change notification settings - Fork 718
/
_hledger
286 lines (273 loc) · 14.9 KB
/
_hledger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#compdef hledger
# ------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for hledger 1.10 ( https://hledger.org/ )
# Last updated: 07.08.2018
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Valodim ( https://github.com/Valodim )
# * fdw ( https://github.com/fdw )
#
# ------------------------------------------------------------------------------
# Notes
# -----
#
# account completion depends on availability of a ~/.hledger.journal file
#
# ------------------------------------------------------------------------------
local curcontext="$curcontext" curstate state line expl grp cmd ret=1
typeset -a args
typeset -A opt_args
args=(
'(- *)'{-h,--help}'[print help information]'
'(-f --file)'{-f,--file}'=[use a different input file]:input file:_files'
'--rules-file=[CSV conversion rules file]:rules file:_files'
'--alias=[display accounts named OLD as NEW]:alias specification'
'--anon=[anonymize accounts and payees]'
'(-b --begin)'{-b,--begin}'=[include postings/txns on or after this date]:date'
'(-e --end)'{-e,--end}'=[include postings/txns before this date]:date'
'(-D --daily)'{-D,--daily}'[multiperiod/multicolumn report by day]'
'(-W --weekly)'{-W,--weekly}'[multiperiod/multicolumn report by week]'
'(-M --monthly)'{-M,--monthly}'[multiperiod/multicolumn report by month]'
'(-Q --quarterly)'{-Q,--quarterly}'[multiperiod/multicolumn report by quarter]'
'(-Y --yearly)'{-Y,--yearly}'[multiperiod/multicolumn report by year]'
'(-p --period)'{-p,--period}'=[set start date, end date, and/or reporting interval all at once]'
'(-C --cleared)'{-C,--cleared}'[include only cleared postings/txns]'
'(-U --uncleared)'{-U,--uncleared}'[include only uncleared postings/txns]'
'(-R --real)'{-R,--real}'[include only non-virtual postings]'
'(--depth)--depth=[hide accounts/postings deeper than N]:depth'
'(-E --empty)'{-E,--empty}'[show empty/zero things which are normally omitted]'
'(-B --cost)'{-B,--cost}'[show amounts in their cost price'\''s commodity]'
'(-V --value)'{-V,--value}'[converts reported amounts to the current market value]'
'(-I --ignore-assertions)'{-I,--ignore-assertions}'[ignore any failing balance assertions]'
'--forecast=[apply periodic transaction rules to generate future transactions]'
)
_arguments -C "$args[@]" -A "-*" \
'(- *)--version[print version information]' \
'--debug[show debug output]' \
'1: :->cmds' \
'*:: :->args' && ret=0
while (( $#state )); do
curstate=$state
shift state
case $curstate in
cmds)
typeset -a cmds
cmds=(
'accounts:show account names (a)'
'activity:show an ascii barchart of posting counts per interval'
'add:prompt for transactions and add them to the journal'
'balance:show accounts and balances (b, bal)'
'balancesheet:show a balance sheet (bs)'
'balancesheetequity:like balancesheet, but also reports equity'
'cashflow:show a cashflow statement (cf)'
'check-dates:check that transactions are sorted by increasing date'
'check-dupes:report account names having the same leaf but different prefixes'
'close:print closing/opening transactions that bring some or all account balances to zero and back'
'help:show any of the hledger manuals'
'import:read new transactions added to each file since last run, and add them to the main journal file'
'incomestatement:show an income statement (is)'
'prices:print market price directives from the journal'
'print:show transaction entries (p, txns)'
'print-unique:print transactions which do not reuse an already-seen description'
'register:show postings and running total (r, reg)'
'register-patch:print the one posting whose transaction description is closest to the description'
'rewrite:print all transactions, adding custom postings to the matched ones'
'stats:show some journal statistics'
'tags:list all the tag names used in the journal'
'test:run built-in unit tests'
)
_describe 'subcommands' cmds && ret=0
;;
args)
: $words
local cmd=$words[1]
(( $+cmd )) || return 1
# curcontext="${curcontext%:*:*}:$service-$cmd:"
case $cmd in
accounts)
args=(
'(--declared)--declared[show account names declared with account directives]'
'(--used)--used[show account names posted to by transactions]'
'(--tree)--tree[show accounts as a tree (default in simple reports)]'
'(--flat)--flat[show accounts as a list (default in multicolumn)]'
'(--drop)--drop=[flat mode, omit N leading account name parts]:drop n'
)
;;
activity)
;;
add)
args=(
'(--no-new-accounts)--no-new-accounts=[do not allow creating new accounts]'
)
;;
bal|balance)
args+=(
'(--change)--change[show balance change in each period (default)]'
'(--cumulative)--cumulative[show balance change accumulated across periods]'
'(-H --historical)'{-H,--historical}'[show historical ending balance in each period]'
'(--tree)--tree[show accounts as a tree (default in simple reports)]'
'(--flat)--flat[show accounts as a list (default in multicolumn)]'
'(-A --average)'{-A,--average}'[show a row average column (in multicolumn mode)]'
'(-T --row-total)'{-T,--row-total}'[show a row total column]'
'(-N --no-total)'{-N,--no-total}'[do not show the final total row]'
'(--drop)--drop=[in flat mode, omit N leading account name parts]:drop n'
'(--no-elide)--no-elide[tree mode, do not squash boring parent accounts]'
'(--format)--format=[in tree mode, use this custom line format]:custom line format'
'(-O --output-format)'{-O,--output-format}='[select the output format from txt, csv, html]:format'
'(-o --output-file)'{-o,--output-file}'=[write output to file]:file'
'(--pretty-tables)--pretty-tables[use unicode to display prettier tables]'
'(--sort-amount)--sort-amount[sort by amount instead of account code/name]'
'(--invert)--invert[display all amounts with reversed sign]'
'(--budget)--budget[show performance compared to budget goals]'
'(--show-unbudgeted)--show-unbudgeted[with --budget, show unbudgeted accounts also]'
)
;;
bl|balancesheet|balancesheetequity)
args+=(
'(--change)--change[show balance change in each period (default)]'
'(--cumulative)--cumulative[show balance change accumulated across periods]'
'(-H --historical)'{-H,--historical}'[show historical ending balance in each period]'
'(--tree)--tree[show accounts as a tree (default in simple reports)]'
'(--flat)--flat[show accounts as a list (default in multicolumn)]'
'(-A --average)'{-A,--average}'[show a row average column (in multicolumn mode)]'
'(-T --row-total)'{-T,--row-total}'[show a row total column]'
'(-N --no-total)'{-N,--no-total}'[do not show the final total row]'
'(--drop)--drop=[in flat mode, omit N leading account name parts]:drop n'
'(--no-elide)--no-elide[tree mode, do not squash boring parent accounts]'
'(--format)--format=[in tree mode, use this custom line format]:custom line format'
'(--sort-amount)--sort-amount[sort by amount instead of account code/name]'
)
;;
cashflow|cf|balancesheet|bs|incomestatement|is)
args+=(
'(--change)--change[show balance change in each period (default)]'
'(--cumulative)--cumulative[show balance change accumulated across periods]'
'(-H --historical)'{-H,--historical}'[show historical ending balance in each period]'
'(--tree)--tree[show accounts as a tree (default in simple reports)]'
'(--flat)--flat[show accounts as a list (default in multicolumn)]'
'(-A --average)'{-A,--average}'[show a row average column (in multicolumn mode)]'
'(-T --row-total)'{-T,--row-total}'[show a row total column]'
'(-N --no-total)'{-N,--no-total}'[do not show the final total row]'
'(--drop)--drop=[in flat mode, omit N leading account name parts]:drop n'
'(--no-elide)--no-elide[tree mode, do not squash boring parent accounts]'
'(--format)--format=[in tree mode, use this custom line format]:custom line format'
'(--sort-amount)--sort-amount[sort by amount instead of account code/name]'
)
;;
import)
args=(
'(--dry-run)--dry-run[just show the transactions to be imported]'
)
;;
is|incomestatement)
args+=(
'(--change)--change[show balance change in each period (default)]'
'(--cumulative)--cumulative[show balance change accumulated across periods]'
'(-H --historical)'{-H,--historical}'[show historical ending balance in each period]'
'(--tree)--tree[show accounts as a tree (default in simple reports)]'
'(--flat)--flat[show accounts as a list (default in multicolumn)]'
'(-A --average)'{-A,--average}'[show a row average column (in multicolumn mode)]'
'(-T --row-total)'{-T,--row-total}'[show a row total column]'
'(-N --no-total)'{-N,--no-total}'[do not show the final total row]'
'(--drop)--drop=[in flat mode, omit N leading account name parts]:drop n'
'(--no-elide)--no-elide[tree mode, do not squash boring parent accounts]'
'(--format)--format=[in tree mode, use this custom line format]:custom line format'
'(--sort-amount)--sort-amount[sort by amount instead of account code/name]'
)
;;
print)
args=(
'(-m --match)'{-m,--match}'[show the transaction whose description is most similar]:string'
'(--new)--new[show only newer-dated transactions added in each file since last run]'
'(-x --explicit)'{-x,--explicit}'[show all amounts explicitly]'
'(-O --output-format)'{-O,--output-format}='[select the output format from txt, csv, html]:format'
'(-o --output-file)'{-o,--output-file}'=[write output to file]:file'
)
;;
register|reg)
args+=(
'(--cumulative)--cumulative[show balance change accumulated across periods]'
'(-H --historical)'{-H,--historical}'[show historical ending balance in each period]'
'(-A --average)'{-A,--average}'[show a row average column (in multicolumn mode)]'
'(-r --related)'{-r,--related}'[show postings'\'' siblings instead]'
'(-w --width)'{-w,--width}'=[set output width to 120, or N]:width (default 80)'
'(-O --output-format)'{-O,--output-format}='[select the output format from txt, csv, html]:format'
'(-o --output-file)'{-o,--output-file}'=[write output to file]:file'
)
;;
stats)
args=(
'(-o --output-file)'{-o,--output-file}'=[write output to file]:file'
)
;;
# fallback to _default
*) _arguments -C -A "-*" "$args[@]" \
'*: :_default' && ret=0
continue
esac
_arguments -C -A "-*" "$args[@]" \
'*:query patterns:->query' && ret=0
;;
query)
local -a accs keywords
keywords=(
'acct\::match account names'
'code\::match by transaction code'
'desc\::match transaction descriptions'
'tag\::match by tag name'
'depth\::match at or above depth'
'status\::match cleared/uncleared transactions'
'real\::match real/virtual transactions'
'empty\::match if amount is/is not zero'
'amt\::match transaction amount'
'cur\::match by currency'
)
if compset -P 'amt:'; then
_message 'match amount (<, <=, >, >=, add sign for non-absolute match)' && ret=0
continue
fi
if compset -P '(#b)(code|desc|tag|depth|status|real|empty):'; then
_message "'$match[1]' parameter" && ret=0
continue
fi
accs=( ${(f)"$(_call_program hledger hledger accounts $PREFIX 2>/dev/null)"} )
if (( $? )); then
_message "error fetching accounts from hledger"
fi
# decided against partial matching here. these lines can
# be uncommented to complete subaccounts hierarchically
# (add -S '' -q to the compadd below, too)
# if compset -P '(#b)(*):'; then
# accs=( ${(M)accs:#$match[1]:*} )
# accs=( ${accs#$IPREFIX} )
# fi
# accs=( ${accs%%:*} )
_wanted accounts expl "accounts" compadd -a accs && ret=0
_describe "matcher keywords" keywords -S '' && ret=0
# not is special, it doesn't need the -S ''
keywords=(
'not:negate expression'
)
_describe "matcher keywords" keywords && ret=0
;;
esac
done
return ret