all repos

clerk @ 6fdb9097048e212574439fb0da84d0c94aa7e01b

missing tooling for ledger/hledger

clerk/journal/printer/transaction.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
formatter, 17 hours ago
1
package printer
2
3
import (
4
	"olexsmir.xyz/clerk/journal/ast"
5
)
6
7
func (p *printer) writeTransaction(t *ast.Transaction) {
8
	// date
9
	p.writeDate(t.Date)
10
11
	// second date
12
	if t.SecondDate != nil {
13
		p.buf.WriteByte('=')
14
		p.writeDate(*t.SecondDate)
15
	}
16
17
	// status
18
	if t.Status != nil && t.Status.Value != ast.StatusNone {
19
		p.buf.WriteByte(' ')
20
		p.buf.WriteString(t.Status.Value.String())
21
	}
22
23
	// code
24
	if t.Code != nil && *t.Code != "" {
25
		p.buf.WriteString(" (")
26
		p.buf.WriteString(*t.Code)
27
		p.buf.WriteByte(')')
28
	}
29
30
	// payee
31
	if t.Payee != nil && t.Payee.Name != "" {
32
		p.buf.WriteByte(' ')
33
		p.buf.WriteString(t.Payee.Name)
34
	}
35
36
	// note
37
	if t.Note != nil && *t.Note != "" {
38
		p.buf.WriteString(" | ")
39
		p.buf.WriteString(*t.Note)
40
	}
41
42
	p.writeComment(t.Comment)
43
	p.buf.WriteByte('\n')
44
45
	// header comments (between transaction line and first posting)
46
	for _, c := range t.HeaderComments {
47
		p.buf.WriteString(p.indent)
48
		p.writeComment(c)
49
		p.buf.WriteByte('\n')
50
	}
51
52
	// postings
53
	p.writePostings(t.Postings)
54
}
55
56
func (p *printer) writePeriodicTransaction(t *ast.PeriodicTransaction) {
57
	p.buf.WriteByte('~')
58
59
	// period
60
	if t.Period.Raw != "" {
61
		p.buf.WriteByte(' ')
62
		p.buf.WriteString(t.Period.Raw)
63
	}
64
65
	// status
66
	if t.Status != nil && t.Status.Value != ast.StatusNone {
67
		p.buf.WriteByte(' ')
68
		p.buf.WriteString(t.Status.Value.String())
69
	}
70
71
	// code
72
	if t.Code != nil && *t.Code != "" {
73
		p.buf.WriteString(" (")
74
		p.buf.WriteString(*t.Code)
75
		p.buf.WriteByte(')')
76
	}
77
78
	// description
79
	if t.Description != nil && *t.Description != "" {
80
		p.buf.WriteByte(' ')
81
		p.buf.WriteString(*t.Description)
82
	}
83
84
	// comment
85
	p.writeInlineComment(t.Comment)
86
	p.buf.WriteByte('\n')
87
88
	// header comments (between periodic line and first posting)
89
	for _, c := range t.HeaderComments {
90
		p.buf.WriteString(p.indent)
91
		p.writeComment(c)
92
		p.buf.WriteByte('\n')
93
	}
94
95
	// postings
96
	p.writePostings(t.Postings)
97
}
98
99
func (p *printer) writeAutomatedTransaction(t *ast.AutomatedTransaction) {
100
	p.buf.WriteByte('=')
101
102
	// expression
103
	if t.Expr != "" {
104
		p.buf.WriteByte(' ')
105
		p.buf.WriteString(t.Expr)
106
	}
107
108
	// comment
109
	if t.Comment != nil && t.Comment.Text != "" {
110
		p.buf.WriteString(" ; ")
111
		p.buf.WriteString(t.Comment.Text)
112
	}
113
114
	p.buf.WriteByte('\n')
115
116
	// header comments (between auto line and first posting)
117
	for _, c := range t.HeaderComments {
118
		p.buf.WriteString(p.indent)
119
		p.writeComment(c)
120
		p.buf.WriteByte('\n')
121
	}
122
123
	// postings
124
	p.writePostings(t.Postings)
125
}
126
127
func (p *printer) writeDate(d ast.Date) {
128
	p.writeInt(d.Year, 4)
129
	p.buf.WriteByte('-')
130
	p.writeInt(int(d.Month), 2)
131
	p.buf.WriteByte('-')
132
	p.writeInt(int(d.Day), 2)
133
}
134
135
// writeInt writes an integer left-padded to the given number of digits.
136
func (p *printer) writeInt(n int, digits int) {
137
	var b [10]byte
138
	pos := len(b)
139
	for range digits {
140
		pos--
141
		b[pos] = '0' + byte(n%10)
142
		n /= 10
143
	}
144
	p.buf.Write(b[pos:])
145
}