all repos

clerk @ dc54e906021a5d4e2caeca03c52769c41bd80b28

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
formatter..., 1 month ago
1
package printer
2
3
import (
4
	"olexsmir.xyz/clerk/internal/decimal"
5
	"olexsmir.xyz/clerk/journal/ast"
6
)
7
8
func (p *printer) writeAmount(a *ast.Amount, pos CommodityPos) {
9
	if a == nil {
10
		return
11
	}
12
13
	if a.IsExpr {
14
		p.buf.WriteString(a.Expr)
15
		return
16
	}
17
18
	comm := a.Commodity
19
	if comm == "" {
20
		p.writeDecimal(a.Quantity, a.QuantityFmt, 2)
21
		return
22
	}
23
24
	switch pos {
25
	case CommodityBefore:
26
		p.buf.WriteString(comm)
27
		if a.HasSpace {
28
			p.buf.WriteByte(' ')
29
		}
30
		p.writeDecimal(a.Quantity, a.QuantityFmt, 2)
31
	case CommodityAfter:
32
		p.writeDecimal(a.Quantity, a.QuantityFmt, 2)
33
		if a.HasSpace {
34
			p.buf.WriteByte(' ')
35
		}
36
		p.buf.WriteString(comm)
37
	default:
38
		panic("impossible CommodityPos value")
39
	}
40
}
41
42
func (p *printer) writeCost(c *ast.Cost, pos CommodityPos) {
43
	if c == nil {
44
		return
45
	}
46
	if c.IsTotal {
47
		p.buf.WriteString(" @@ ")
48
	} else {
49
		p.buf.WriteString(" @ ")
50
	}
51
	p.writeAmount(&c.Amount, pos)
52
}
53
54
func (p *printer) writeBalanceAssertion(ba *ast.BalanceAssertion, pos CommodityPos) {
55
	if ba == nil {
56
		return
57
	}
58
	switch {
59
	case ba.IsInclusive:
60
		p.buf.WriteString("=== ")
61
	case ba.IsStrict:
62
		p.buf.WriteString("== ")
63
	default:
64
		p.buf.WriteString("= ")
65
	}
66
	p.writeAmount(&ba.Amount, pos)
67
}
68
69
func (p *printer) writeDecimal(d decimal.Decimal, fmt ast.QuantityFormat, forcePrec int) {
70
	d.WriteFixed(&p.buf, forcePrec, fmt.Decimal, fmt.Thousands)
71
}