@@ -40,7 +40,7 @@ func (*cmdRegister) newCommand(names ...string) command {
4040
4141Lists or aggregate postings from the specified account.` )
4242 cmd .BoolVar (& cmd .verbose , "v" , false , "log debug info to stderr" )
43- cmd .BoolVar (& cmd .recurse , "r" , false , "include subaccount postings in parent accounts" )
43+ cmd .BoolVar (& cmd .recurse , "r" , false , "include sub-account postings in parent accounts" )
4444 // filtering options
4545 cmd .Var (& cmd .begin , "b" , "begin register from this date" )
4646 cmd .Var (& cmd .end , "e" , "end register on this date" )
@@ -51,7 +51,7 @@ Lists or aggregate postings from the specified account.`)
5151 cmd .BoolVar (& cmd .monthly , "m" , false , "aggregate postings by month" )
5252 cmd .BoolVar (& cmd .quarterly , "q" , false , "aggregate postings by quarter" )
5353 cmd .BoolVar (& cmd .yearly , "y" , false , "aggregate postings by year" )
54- cmd .IntVar (& cmd .top , "g" , 5 , "include this many largest subaccounts in aggregate results" )
54+ cmd .IntVar (& cmd .top , "g" , 5 , "include this many largest sub-accounts in aggregate results" )
5555 cmd .BoolVar (& cmd .cumulative , "c" , false , "aggregate cumulatively across time" )
5656 // output options
5757 cmd .IntVar (& cmd .maxLabelWidth , "l" , 12 , "maximum width of a column label" )
@@ -73,92 +73,59 @@ func (cmd *cmdRegister) execute(f io.Writer) {
7373 fmt .Fprintln (f , acc .FullName , acc .Commodity .Id )
7474 }
7575 if by := cmd .period (); by != nil {
76- if cmd .recurse {
77- cmd .recursiveAggregatedRegister (f , acc , by )
78- } else {
79- cmd .flatAggregatedRegister (f , acc , by )
80- }
76+ cmd .aggregatedRegister (f , acc , by )
8177 } else {
82- var opts = options {
83- prefix : acc .FullName ,
84- maxAcct : cmd .maxLabelWidth ,
85- location : cmd .location ,
86- commodity : acc .Commodity ,
87- showNotes : cmd .showNotes ,
88- }
89- if cmd .recurse {
90- var ps postings
91- acc .WithChildrenDo (func (a * coin.Account ) {
92- ps = append (ps , cmd .trim (a .Postings )... )
93- })
94- sort .SliceStable (ps , func (i , j int ) bool {
95- return ps [i ].Transaction .Posted .Before (ps [j ].Transaction .Posted )
96- })
97- ps .printLong (f , & opts )
98- } else {
99- cmd .trim (acc .Postings ).print (f , & opts )
100- }
78+ cmd .fullRegister (f , acc )
10179 }
10280}
10381
104- func (cmd * cmdRegister ) flatAggregatedRegister (f io.Writer , acc * coin.Account , by * reducer ) {
105- totals := accountTotals {}
106- acc .WithChildrenDo (func (a * coin.Account ) {
107- ts := totals .newTotals (a , by )
108- for _ , p := range cmd .trim (a .Postings ) {
109- ts .add (p .Transaction .Posted , p .Quantity )
110- }
111- })
112- var accounts []* coin.Account
113- totals , accounts = totals .top (cmd .top )
114- top := totals [accounts [0 ]]
115- for _ , ts := range totals {
116- top .mergeTime (ts )
117- }
118- totals .mergeTime (top )
119- if cmd .cumulative {
120- totals .makeCumulative ()
82+ func (cmd * cmdRegister ) fullRegister (f io.Writer , acc * coin.Account ) {
83+ var opts = options {
84+ prefix : acc .FullName ,
85+ maxAcct : cmd .maxLabelWidth ,
86+ location : cmd .location ,
87+ commodity : acc .Commodity ,
88+ showNotes : cmd .showNotes ,
12189 }
122- label := func (a * coin.Account ) string {
123- switch a {
124- case nil :
125- return "Other"
126- case acc :
127- return acc .Name
128- default :
129- n := strings .TrimPrefix (a .FullName , acc .FullName )
130- return coin .ShortenAccountName (n , cmd .maxLabelWidth )
131- }
90+ if cmd .recurse {
91+ var ps postings
92+ acc .WithChildrenDo (func (a * coin.Account ) {
93+ ps = append (ps , cmd .trim (a .Postings )... )
94+ })
95+ sort .SliceStable (ps , func (i , j int ) bool {
96+ return ps [i ].Transaction .Posted .Before (ps [j ].Transaction .Posted )
97+ })
98+ ps .printLong (f , & opts )
99+ } else {
100+ cmd .trim (acc .Postings ).print (f , & opts )
132101 }
133- totals .output (f , accounts , label , cmd .output )
134102}
135103
136- func (cmd * cmdRegister ) recursiveAggregatedRegister (f io.Writer , acc * coin.Account , by * reducer ) {
104+ func (cmd * cmdRegister ) aggregatedRegister (f io.Writer , acc * coin.Account , by * timeReducer ) {
137105 totals := accountTotals {}
138106 acc .WithChildrenDo (func (a * coin.Account ) {
139107 ts := totals .newTotals (a , by )
140108 for _ , p := range cmd .trim (a .Postings ) {
141- ts .add (p . Transaction . Posted , p . Quantity )
109+ ts .add (p )
142110 }
143111 })
144- if cmd .recurse {
145- acc .FirstWithChildrenDo (func (a * coin.Account ) {
146- child := totals [a ]
147- parent := totals [a .Parent ]
148- if parent != nil {
112+ // Propagate timelines and possibly amounts up top
113+ acc .FirstWithChildrenDo (func (a * coin.Account ) {
114+ child := totals [a ]
115+ parent := totals [a .Parent ]
116+ if parent != nil {
117+ if cmd .recurse {
149118 parent .merge (child )
119+ } else {
120+ parent .mergeTime (child )
150121 }
151- })
152- }
122+ }
123+ })
153124 totals .sanitize ()
154- accTotals := totals [acc ]
155- check .If (accTotals != nil , "root account totals shouldn't be empty\n " )
156- delete (totals , acc )
157125 var accounts []* coin.Account
158126 totals , accounts = totals .top (cmd .top )
159- totals .mergeTime (accTotals )
160- totals [acc ] = accTotals
161- accounts = append (accounts , acc )
127+ totals .mergeTime (totals [acc ])
128+
162129 if cmd .cumulative {
163130 totals .makeCumulative ()
164131 }
@@ -167,7 +134,7 @@ func (cmd *cmdRegister) recursiveAggregatedRegister(f io.Writer, acc *coin.Accou
167134 case nil :
168135 return "Other"
169136 case acc :
170- return "Totals"
137+ return acc . Name
171138 default :
172139 n := strings .TrimPrefix (a .FullName , acc .FullName )
173140 return coin .ShortenAccountName (n , cmd .maxLabelWidth )
@@ -176,7 +143,7 @@ func (cmd *cmdRegister) recursiveAggregatedRegister(f io.Writer, acc *coin.Accou
176143 totals .output (f , accounts , label , cmd .output )
177144}
178145
179- func (cmd * cmdRegister ) period () * reducer {
146+ func (cmd * cmdRegister ) period () * timeReducer {
180147 switch {
181148 case cmd .weekly :
182149 return & week
0 commit comments