-
Notifications
You must be signed in to change notification settings - Fork 6
/
render.go
219 lines (188 loc) · 4.63 KB
/
render.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
package style
import (
"bytes"
"fmt"
"reflect"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/araddon/dateparse"
"github.com/dustin/go-humanize"
"github.com/mattn/go-runewidth"
"github.com/muesli/termenv"
"gopkg.in/yaml.v3"
"go.szostok.io/version/style/termenvx"
)
// GoTemplateRender provides a functionality to render input data based on a given style.
type GoTemplateRender struct {
config *Config
colorProfile termenv.Profile
width int
}
// NewGoTemplateRender returns new GoTemplateRender instance.
func NewGoTemplateRender(cfg *Config) *GoTemplateRender {
renderer := GoTemplateRender{
config: cfg,
colorProfile: termenv.Ascii,
width: 15,
}
return &renderer
}
// Render renders input data based on configured given style.
// If isSmartTerminal is set to 'true', colors and formatting are used.
func (r *GoTemplateRender) Render(in interface{}, isSmartTerminal bool) (string, error) {
if isSmartTerminal {
r.colorProfile = termenvx.ColorProfile()
}
tpl, err := template.New("tpl").
Funcs(sprig.TxtFuncMap()).
Funcs(r.styleFuncMap()).
Funcs(r.generalHelpersFuncMap()).
Funcs(r.termenvColorFuncMap()).
Funcs(r.termenvHelpersFuncMap()).
Parse(r.config.Layout.GoTemplate)
if err != nil {
return "", err
}
var buff bytes.Buffer
if err := tpl.Execute(&buff, in); err != nil {
return "", err
}
return buff.String(), nil
}
func (r *GoTemplateRender) adjustKeyWidth(in any) string {
if in == nil {
return ""
}
// could be any underlying type
val := reflect.ValueOf(in)
// if it's a pointer, resolve its value
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
// should double-check we now have a struct (could still be anything)
if val.Kind() != reflect.Struct {
return ""
}
// check for the longest name
structType := val.Type()
for i := 0; i < structType.NumField(); i++ {
var (
field = structType.Field(i)
tag = field.Tag
name = field.Name
)
if val, ok := tag.Lookup("pretty"); ok {
name = val
}
width := runewidth.StringWidth(name)
if r.width < width {
r.width = width
}
}
return ""
}
func (r *GoTemplateRender) header(in string) string {
c := NewTermenvStyle(r.colorProfile, r.config.Formatting.Header.FormatPrimitive)
return c.Styled(fmt.Sprintf("%s%s", r.config.Formatting.Header.Prefix, in))
}
func (r *GoTemplateRender) key(in string) string {
ff := r.config.Formatting.Key.FormatPrimitive
c := NewTermenvStyle(r.colorProfile, ff)
initial := runewidth.StringWidth(in)
newV := c.Styled(in)
varr := runewidth.StringWidth(newV) - initial + 2
return fmt.Sprintf("%-*s", r.width+varr, newV)
}
type KV struct {
Key string
Value any
}
func (r *GoTemplateRender) extra(in any) []KV {
var out []KV
if in == nil {
return out
}
// could be any underlying type
val := reflect.ValueOf(in)
// if its a pointer, resolve its value
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
// should double-check we now have a struct (could still be anything)
if val.Kind() != reflect.Struct {
return out
}
// now we grab our values
structType := val.Type()
for i := 0; i < structType.NumField(); i++ {
var (
field = structType.Field(i)
tag = field.Tag
name = field.Name
)
if val, ok := tag.Lookup("pretty"); ok {
name = val
}
out = append(out, KV{
Key: name,
Value: val.Field(i).Interface(),
})
}
return out
}
func (*GoTemplateRender) commit(in string) string {
return strings.TrimSpace(fmt.Sprintf("%.7s", in))
}
func (r *GoTemplateRender) val(in any) string {
c := NewTermenvStyle(r.colorProfile, r.config.Formatting.Val.FormatPrimitive)
switch str := in.(type) {
case string:
return c.Styled(str)
default:
out, err := yaml.Marshal(in)
if err != nil {
return err.Error()
}
return c.Styled(string(out))
}
}
func (*GoTemplateRender) fmtBool(in bool) string {
if in {
return "yes"
}
return "no"
}
func (r *GoTemplateRender) fmtDate(in interface{}) string {
var normalized time.Time
switch date := in.(type) {
case time.Time:
normalized = date
case string:
t, err := dateparse.ParseAny(date)
if err != nil {
return date
}
normalized = t
}
suffix := ""
if r.config.Formatting.Date.EnableHumanizedSuffix {
suffix = " (" + humanize.Time(normalized) + ")"
}
return fmt.Sprintf("%s%s", normalized.Local().Format(time.RFC822), suffix)
}
func (r *GoTemplateRender) fmtDateHumanized(in interface{}) string {
var normalized time.Time
switch date := in.(type) {
case time.Time:
normalized = date
case string:
t, err := dateparse.ParseAny(date)
if err != nil {
return ""
}
normalized = t
}
return humanize.Time(normalized)
}