all repos

clerk @ 6fdb9097048e212574439fb0da84d0c94aa7e01b

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
formatter, 17 hours ago
1
package printer
2
3
import (
4
	"strings"
5
6
	"olexsmir.xyz/clerk/journal/ast"
7
)
8
9
func (p *printer) writeIncludeDirective(i *ast.IncludeDirective) {
10
	p.buf.WriteString("include ")
11
	p.buf.WriteString(quoteString(i.Path))
12
	p.writeInlineComment(i.Comment)
13
}
14
15
func (p *printer) writeAccountDirective(a *ast.AccountDirective) {
16
	p.buf.WriteString("account ")
17
	p.buf.WriteString(a.Account.Name)
18
	p.writeInlineComment(a.Comment)
19
}
20
21
func (p *printer) writeCommodityDirective(c *ast.CommodityDirective) {
22
	p.buf.WriteString("commodity ")
23
	p.buf.WriteString(c.Commodity)
24
	p.writeInlineComment(c.Comment)
25
}
26
27
func (p *printer) writeAliasDirective(a *ast.AliasDirective) {
28
	p.buf.WriteString("alias ")
29
	p.buf.WriteString(a.From)
30
	p.buf.WriteString(" = ")
31
	p.buf.WriteString(a.To)
32
	p.writeInlineComment(a.Comment)
33
}
34
35
func (p *printer) writePayeeDirective(pd *ast.PayeeDirective) {
36
	p.buf.WriteString("payee ")
37
	p.buf.WriteString(quoteString(pd.Name))
38
	p.writeInlineComment(pd.Comment)
39
}
40
41
func (p *printer) writeTagDirective(t *ast.TagDirective) {
42
	p.buf.WriteString("tag ")
43
	p.buf.WriteString(quoteString(t.Name))
44
	p.writeInlineComment(t.Comment)
45
}
46
47
func (p *printer) writeYearDirective(y *ast.YearDirective) {
48
	p.buf.WriteString("year ")
49
	p.writeInt(y.Year, 4)
50
	p.writeInlineComment(y.Comment)
51
}
52
53
func (p *printer) writeDecimalMarkDirective(d *ast.DecimalMarkDirective) {
54
	p.buf.WriteString("decimal ")
55
	p.buf.WriteByte(d.Mark)
56
	p.writeInlineComment(d.Comment)
57
}
58
59
func (p *printer) writeDefaultCommodityDirective(d *ast.DefaultCommodityDirective) {
60
	p.buf.WriteString("D ")
61
	p.writeAmount(&d.Amount, p.cfg.CommodityPos)
62
	p.writeInlineComment(d.Comment)
63
}
64
65
func (p *printer) writeConversionDirective(c *ast.ConversionDirective) {
66
	p.buf.WriteString("C ")
67
	p.writeAmount(&c.From, CommodityAfter) // TODO: support CommodityBefore
68
	p.buf.WriteString(" = ")
69
	p.writeAmount(&c.To, CommodityAfter) // TODO: support CommodityBefore
70
	p.writeInlineComment(c.Comment)
71
}
72
73
func (p *printer) writeMarketPriceDirective(m *ast.MarketPriceDirective) {
74
	p.buf.WriteString("P ")
75
	p.writeDate(m.DateTime.Date)
76
	if m.DateTime.Time != nil {
77
		p.buf.WriteByte(' ')
78
		p.writeInt(m.DateTime.Time.Hour, 2)
79
		p.buf.WriteByte(':')
80
		p.writeInt(m.DateTime.Time.Minute, 2)
81
		p.buf.WriteByte(':')
82
		p.writeInt(m.DateTime.Time.Second, 2)
83
	}
84
	p.buf.WriteByte(' ')
85
	p.buf.WriteString(m.Commodity)
86
	p.buf.WriteByte(' ')
87
	p.writeAmount(&m.Amount, p.cfg.CommodityPos)
88
	p.writeInlineComment(m.Comment)
89
}
90
91
func (p *printer) writeCommentBlockDirective(cb *ast.CommentBlockDirective) {
92
	p.buf.WriteString("comment")
93
	if cb.Header != "" {
94
		p.buf.WriteByte(' ')
95
		p.buf.WriteString(cb.Header)
96
	}
97
	p.buf.WriteByte('\n')
98
	if cb.Content != "" {
99
		p.buf.WriteString(cb.Content)
100
		if !strings.HasSuffix(cb.Content, "\n") {
101
			p.buf.WriteByte('\n')
102
		}
103
	}
104
	p.buf.WriteString("end")
105
	if cb.Header != "" {
106
		p.buf.WriteByte(' ')
107
		p.buf.WriteString(cb.Header)
108
	}
109
}
110
111
func (p *printer) writeApplyDirective(a *ast.ApplyDirective) {
112
	p.buf.WriteString("apply ")
113
	p.buf.WriteString(a.Expr)
114
	p.writeInlineComment(a.Comment)
115
}
116
117
func (p *printer) writeEndDirective(e *ast.EndDirective) {
118
	p.buf.WriteString("end")
119
	if e.Expr != "" {
120
		p.buf.WriteByte(' ')
121
		p.buf.WriteString(e.Expr)
122
	}
123
	p.writeInlineComment(e.Comment)
124
}