-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.go
241 lines (220 loc) · 4.51 KB
/
fmt.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package tplfunc
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"zgo.at/zstd/ztime"
)
// JSON prints any object as JSON.
func JSON(v any) string {
j, err := json.Marshal(v)
if err != nil {
panic(fmt.Errorf("json: %w", err))
}
return string(j)
}
// JSONPretty prints any object as indented JSON.
func JSONPretty(v any) string {
j, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(fmt.Errorf("json_pretty: %w", err))
}
return string(j)
}
// Number formats a number with thousand separators using the separator sep.
//
// For floats it will always use '.' as the digit separator, unless sep is set
// to '.' in which case it will use ','.
func Number(n any, sep ...rune) string {
if len(sep) == 0 {
sep = []rune{','}
}
s := strconv.FormatFloat(toFloat(n), 'f', -1, 64)
if len(s) < 4 {
return s
}
s, d, _ := strings.Cut(s, ".")
b := []byte(s)
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
var out []rune
for i := range b {
if i > 0 && i%3 == 0 && sep[0] > 1 {
out = append(out, sep[0])
}
out = append(out, rune(b[i]))
}
for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
out[i], out[j] = out[j], out[i]
}
s = string(out)
if d != "" {
if sep[0] == '.' {
s += "," + d
} else {
s += "." + d
}
}
return s
}
// LargeNumber formats a number, adding the suffix "M" for values larger than a
// million or "k" for values larger than 100,000.
//
// This loses some accuracy.
func LargeNumber(n any, sep ...rune) string {
if len(sep) == 0 {
sep = []rune{','}
}
num := toFloat(n)
switch {
case num > 100_000_000:
return Number(int64(num/1_000_000), sep...) + "M"
case num > 1_000_000:
s := fmt.Sprintf("%.1fM", num/1_000_000)
if sep[0] == '.' {
s = strings.ReplaceAll(s, ".", ",")
}
return s
case num > 10_000:
return Number(int64(num/1000), sep...) + "k"
default:
return Number(num, sep...)
}
}
// Time formats a time as the given format string.
//
// Return empty string if the time is nil or the zero value.
func Time(a any, format string) string {
var t time.Time
switch tt := a.(type) {
case time.Time:
t = tt
case *time.Time:
if tt == nil {
return ""
}
t = *tt
case string:
if tt == "now" {
t = time.Now()
} else {
t = ztime.FromString(tt)
}
default:
panic(fmt.Sprintf("time: unsupported type %T", tt))
}
if t.IsZero() {
return ""
}
switch format {
case "":
format = "2006-01-02"
case "rfc3339":
format = time.RFC3339
case "rfc3339nano":
format = time.RFC3339Nano
case "ansic":
format = time.ANSIC
}
return t.Format(format)
}
func Duration(a any, format string) string {
var d time.Duration
switch dd := a.(type) {
case time.Duration:
d = dd
case *time.Duration:
if dd == nil {
return ""
}
d = *dd
default:
panic(fmt.Sprintf("time: unsupported type %T", dd))
}
// TODO
return d.String()
}
func fact(unit byte) uint64 {
switch unit {
case 'b':
return 1
case 'k':
return 1024
case 'm':
return 1024 * 1024
case 'g':
return 1024 * 1024 * 1024
case 't':
return 1024 * 1024 * 1024 * 1024
case 'p':
return 1024 * 1024 * 1024 * 1024 * 1024
default:
panic(fmt.Sprintf("size:unknown unit value: %q", unit))
}
}
// Size formats a file size.
//
// The optional parameter max gives the highest unit to format it as. Values for
// this can be 'b', 'k', 'm', 'g, 't', 'p'.
//
// The format string controls some formatting aspects, as key/value pairs:
//
// min=n
// max=n
// from=n
func Size(n any, format ...string) string {
var (
min = byte('b')
max = byte('t')
from = byte('b')
)
for _, opt := range format {
k, v, ok := strings.Cut(opt, "=")
if !ok || len(v) != 1 {
panic(fmt.Sprintf("size: invalid option %q", opt))
}
switch k {
case "min":
min = v[0]
case "max":
max = v[0]
case "from":
from = v[0]
}
}
bytes := toFloat(n) * float64(fact(from))
_, _ = min, max
units := []string{"", "K", "M", "G", "T", "P"}
i := 0
for ; i < len(units); i++ {
if bytes < 1024 {
return fmt.Sprintf("%.1f%s", bytes, units[i])
}
bytes /= 1024
}
return fmt.Sprintf("%.1f%s", bytes*1024, units[i-1])
}
func Slug(s string) string {
var n strings.Builder
n.Grow(len(s))
didDash := false
for _, c := range s {
// All ASCII punctuation and control characters
if c <= '/' || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= 0x7f) {
if !didDash {
didDash = true
n.WriteByte('-')
}
} else {
didDash = false
n.WriteRune(c)
}
}
if didDash {
return n.String()[:n.Len()-1]
}
return n.String()
}