/
format.go
162 lines (131 loc) 路 3.99 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package output
import (
"fmt"
"math"
"github.com/Rhymond/go-money"
"github.com/dustin/go-humanize"
"github.com/shopspring/decimal"
)
var roundCostsAbove = 1
func formatQuantity(q *decimal.Decimal) string {
if q == nil {
return "-"
}
f, _ := q.Float64()
return humanize.CommafWithDigits(f, 4)
}
func formatCost(currency string, d *decimal.Decimal) string {
if d == nil {
return "-"
}
if d.Abs().GreaterThanOrEqual(decimal.NewFromInt(int64(roundCostsAbove))) {
return formatWholeDecimalCurrency(currency, *d)
}
return formatRoundedDecimalCurrency(currency, *d)
}
func FormatCost2DP(currency string, d *decimal.Decimal) string {
if d == nil {
return "-"
}
return formatRoundedDecimalCurrency(currency, *d)
}
func formatPrice(currency string, d decimal.Decimal) string {
if d.LessThan(decimal.NewFromFloat(0.1)) {
return formatFullDecimalCurrency(currency, d)
}
return formatRoundedDecimalCurrency(currency, d)
}
func formatFullDecimalCurrency(currency string, d decimal.Decimal) string {
formatter := money.GetCurrency(currency).Formatter()
scaledInt := decimalToScaledInt(d, formatter.Fraction, 10)
formatter.Fraction = scaledInt.FractionLength
return formatter.Format(scaledInt.Number)
}
func formatRoundedDecimalCurrency(currency string, d decimal.Decimal) string {
formatter := money.GetCurrency(currency).Formatter()
scaledInt := decimalToScaledInt(d, formatter.Fraction, formatter.Fraction)
formatter.Fraction = scaledInt.FractionLength
return formatter.Format(scaledInt.Number)
}
func formatWholeDecimalCurrency(currency string, d decimal.Decimal) string {
formatter := money.GetCurrency(currency).Formatter()
scaledInt := decimalToScaledInt(d, 0, 0)
formatter.Fraction = scaledInt.FractionLength
return formatter.Format(scaledInt.Number)
}
type scaledInt64 struct {
Number int64
FractionLength int
}
// Convert a decimal to a "scaled int" that can be used with the money.Formatter.
// This is a bit funny since decimal.Decimal is implemented as a scaled int itself. We can't use it though
// because the scale (Exponent) can potentially be anything. This method normalizes the scale to the desired
// length of the fraction.
func decimalToScaledInt(d decimal.Decimal, minFracLen, maxFracLen int) *scaledInt64 {
// round excess fraction part
d = d.Round(int32(maxFracLen))
co := d.Coefficient().Int64()
ex := int(d.Exponent())
frac := 0
if d.Exponent() < 0 {
// calculate the size of the fractional part
frac = ex * -1
} else if ex > 0 {
// not sure if this can happen, but scale the coefficient to frac 0
co *= int64(math.Pow10(ex))
}
// remove excess trailing zeros
for co%10 == 0 && frac > minFracLen {
co /= 10
frac--
}
// add trailing zeros
if frac < minFracLen {
co *= int64(math.Pow10(minFracLen - frac))
frac = minFracLen
}
return &scaledInt64{co, frac}
}
func formatTitleWithCurrency(title, currency string) string {
if currency == "USD" {
return title
}
return fmt.Sprintf("%s (%s)", title, currency)
}
func truncateMiddle(s string, maxLen int, fill string) string {
r := []rune(s)
if len(r) <= maxLen {
return s
}
fillR := []rune(fill)
fillLen := len(fillR)
if fillLen > maxLen {
return fill[0:maxLen]
}
startLen := int64(math.Ceil(0.5 * float64(maxLen-fillLen)))
endLen := int64(math.Floor(0.5 * float64(maxLen-fillLen)))
truncated := make([]rune, 0, maxLen)
truncated = append(truncated, r[0:startLen]...)
truncated = append(truncated, fillR...)
truncated = append(truncated, r[int64(len(r))-endLen:]...)
return string(truncated)
}
func showProject(p Project, opts Options, showError bool) bool {
if p.Metadata.HasErrors() && showError {
return true
}
if opts.ShowOnlyChanges {
// only return true if the project has code changes so the table can also show
// project that have cost changes.
if p.Metadata.VCSCodeChanged != nil && *p.Metadata.VCSCodeChanged {
return true
}
}
if opts.ShowAllProjects {
return true
}
if p.Diff == nil || !p.Diff.HasResources() {
return false
}
return true // has diff
}