all repos

clerk @ 8489d4c

missing tooling for ledger/hledger

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

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