-
Notifications
You must be signed in to change notification settings - Fork 13
/
printing.go
339 lines (281 loc) · 9.82 KB
/
printing.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package output
import (
"log/slog"
"time"
"github.com/fatih/color"
"github.com/samber/lo"
"github.com/neilotoole/sq/libsq/core/diffdoc"
"github.com/neilotoole/sq/libsq/core/ioz"
"github.com/neilotoole/sq/libsq/core/timez"
)
// Printing describes color and pretty-printing options.
type Printing struct {
// FormatDatetime formats a timestamp e.g. 2020-11-12T13:14:15Z.
// Defaults to timez.DefaultDatetime.
FormatDatetime func(time time.Time) string
// FormatTime formats a time of day, e.g. 13:14:15.
// Defaults to timez.DefaultTime.
FormatTime func(time time.Time) string
// FormatDate formats a date, e.g. 2020-11-12.
// Defaults to timez.DefaultDate.
FormatDate func(time time.Time) string
// Active is the color for an active handle (or group, etc).
Active *color.Color
// Bold is the color for bold elements. Frequently Punc will just be color.Bold.
Bold *color.Color
// Bool is the color for boolean values.
Bool *color.Color
// Bytes is the color for byte / binary values.
Bytes *color.Color
// Datetime is the color for time-related values.
Datetime *color.Color
// Diff contains colors for diff output.
Diff *diffdoc.Colors
// Disabled is the color for disabled elements.
Disabled *color.Color
// Duration is the color for time duration values.
Duration *color.Color
// Enabled is the color for enabled elements.
Enabled *color.Color
// Error is the color for error elements such as an error message.
Error *color.Color
// Faint is the color for faint elements - the opposite of Hilite.
Faint *color.Color
// Handle is the color for source handles such as "@sakila"
Handle *color.Color
// Header is the color for header elements in a table.
Header *color.Color
// Hilite is the color for highlighted elements.
Hilite *color.Color
// Key is the color for keys such as a JSON field name.
Key *color.Color
// Location is the color for Source.Location values.
Location *color.Color
// Normal is the default color.
Normal *color.Color
// Null is the color for null.
Null *color.Color
// Number is the color for number values, including int, float, decimal etc.
Number *color.Color
// Punc is the color for punctuation such as colons, braces, etc.
Punc *color.Color
// Stack is the color for stack traces.
Stack *color.Color
// StackError is the color for errors attached to a stack trace.
StackError *color.Color
// StackErrorType is the color for the error types attached to a stack trace.
StackErrorType *color.Color
// String is the color for string values.
String *color.Color
// Success is the color for success elements.
Success *color.Color
// Warning is the color for warning elements.
Warning *color.Color
// NewBufferFn returns a new [ioz.Buffer] instance; it should be preferred
// over [bytes.Buffer] when dealing large/unbounded data.
NewBufferFn func() ioz.Buffer
// Indent is the indent string to use when pretty-printing,
// typically two spaces.
Indent string
// ExcelDatetimeFormat is the format string for datetime values.
// See excelw.OptDatetimeFormat.
ExcelDatetimeFormat string
// ExcelDateFormat is the format string for date values.
// See excelw.OptDateFormat.
ExcelDateFormat string
// ExcelTimeFormat is the format string for time values.
// See excelw.OptTimeFormat.
ExcelTimeFormat string
// FlushThreshold is the size in bytes after which an output writer
// should flush any internal buffer.
FlushThreshold int
// monochrome is controlled by EnableColor.
monochrome bool
// ShowHeader indicates that a header (e.g. a header row) should
// be printed where applicable.
ShowHeader bool
// Verbose indicates that verbose output should be printed where
// applicable.
Verbose bool
// Compact indicates that output should not be pretty-printed.
// Typically this means indentation, new lines, etc., but
// varies by output format.
Compact bool
// Redact indicates that sensitive fields (such as passwords)
// should be redacted (hidden/masked).
Redact bool
// FormatDatetimeAsNumber indicates that datetime values should be
// rendered as naked numbers (instead of as a string) if possible.
// See cli.OptDatetimeFormatAsNumber.
FormatDatetimeAsNumber bool
// FormatTimeAsNumber indicates that time values should be
// rendered as naked numbers (instead of as a string) if possible.
// See cli.OptTimeFormatAsNumber.
FormatTimeAsNumber bool
// FormatDateAsNumber indicates that date values should be
// rendered as naked numbers (instead of as a string) if possible.
// See cli.OptDateFormatAsNumber.
FormatDateAsNumber bool
}
// NewPrinting returns a Printing instance. Color and pretty-print
// are enabled. The default indent is two spaces.
func NewPrinting() *Printing {
pr := &Printing{
NewBufferFn: ioz.NewDefaultBuffer,
ShowHeader: true,
Verbose: false,
Compact: false,
Redact: true,
FlushThreshold: 1000,
FormatDatetime: timez.FormatFunc(timez.DefaultDatetime),
FormatDatetimeAsNumber: false,
FormatTime: timez.FormatFunc(timez.DefaultTime),
FormatTimeAsNumber: false,
FormatDate: timez.FormatFunc(timez.DefaultDate),
FormatDateAsNumber: false,
monochrome: false,
Indent: " ",
Active: color.New(color.FgGreen, color.Bold),
Bold: color.New(color.Bold),
Bool: color.New(color.FgYellow),
Bytes: color.New(color.Faint),
Datetime: color.New(color.FgGreen, color.Faint),
Diff: diffdoc.NewColors(),
Disabled: color.New(color.FgYellow, color.Faint),
Duration: color.New(color.FgGreen, color.Faint),
Enabled: color.New(color.FgGreen, color.Faint),
Error: color.New(color.FgRed, color.Bold),
Faint: color.New(color.Faint),
Handle: color.New(color.FgBlue),
Header: color.New(color.FgBlue),
Hilite: color.New(color.FgHiBlue),
Key: color.New(color.FgBlue, color.Bold),
Location: color.New(color.FgGreen),
Normal: color.New(),
Null: color.New(color.Faint),
Number: color.New(color.FgCyan),
Punc: color.New(color.Bold),
String: color.New(color.FgGreen),
Stack: color.New(color.Faint),
StackError: color.New(color.FgYellow, color.Faint),
StackErrorType: color.New(color.FgGreen, color.Faint),
Success: color.New(color.FgGreen, color.Bold),
Warning: color.New(color.FgYellow),
}
pr.EnableColor(true)
return pr
}
// Clone returns a clone of pr.
func (pr *Printing) Clone() *Printing {
pr2 := &Printing{
NewBufferFn: pr.NewBufferFn,
monochrome: pr.monochrome,
FlushThreshold: pr.FlushThreshold,
ShowHeader: pr.ShowHeader,
Verbose: pr.Verbose,
Compact: pr.Compact,
Indent: pr.Indent,
Redact: pr.Redact,
FormatDatetime: pr.FormatDatetime,
FormatDatetimeAsNumber: pr.FormatDatetimeAsNumber,
FormatTime: pr.FormatTime,
FormatTimeAsNumber: pr.FormatTimeAsNumber,
FormatDate: pr.FormatDate,
FormatDateAsNumber: pr.FormatDateAsNumber,
Diff: pr.Diff.Clone(),
}
pr2.Active = lo.ToPtr(*pr.Active)
pr2.Bold = lo.ToPtr(*pr.Bold)
pr2.Bool = lo.ToPtr(*pr.Bool)
pr2.Bytes = lo.ToPtr(*pr.Bytes)
pr2.Datetime = lo.ToPtr(*pr.Datetime)
pr2.Disabled = lo.ToPtr(*pr.Disabled)
pr2.Duration = lo.ToPtr(*pr.Duration)
pr2.Enabled = lo.ToPtr(*pr.Enabled)
pr2.Error = lo.ToPtr(*pr.Error)
pr2.Faint = lo.ToPtr(*pr.Faint)
pr2.Handle = lo.ToPtr(*pr.Handle)
pr2.Header = lo.ToPtr(*pr.Header)
pr2.Hilite = lo.ToPtr(*pr.Hilite)
pr2.Key = lo.ToPtr(*pr.Key)
pr2.Location = lo.ToPtr(*pr.Location)
pr2.Null = lo.ToPtr(*pr.Null)
pr2.Normal = lo.ToPtr(*pr.Normal)
pr2.Number = lo.ToPtr(*pr.Number)
pr2.Punc = lo.ToPtr(*pr.Punc)
pr2.String = lo.ToPtr(*pr.String)
pr2.Success = lo.ToPtr(*pr.Success)
pr2.Stack = lo.ToPtr(*pr.Stack)
pr2.StackError = lo.ToPtr(*pr.StackError)
pr2.StackErrorType = lo.ToPtr(*pr.StackErrorType)
pr2.Warning = lo.ToPtr(*pr.Warning)
return pr2
}
// LogValue implements slog.LogValuer.
func (pr *Printing) LogValue() slog.Value {
if pr == nil {
return slog.Value{}
}
// REVISIT: Should we output all Printing values here?
return slog.GroupValue(
slog.Bool("verbose", pr.Verbose),
slog.Bool("header", pr.ShowHeader),
slog.Bool("monochrome", pr.monochrome),
slog.Bool("compact", pr.Compact),
slog.Bool("redact", pr.Redact),
slog.Int("flush-threshold", pr.FlushThreshold),
slog.String("indent", pr.Indent),
slog.Bool("format.datetime.number", pr.FormatDatetimeAsNumber),
slog.Bool("format.date.number", pr.FormatDateAsNumber),
slog.Bool("format.time.number", pr.FormatTimeAsNumber),
)
}
func (pr *Printing) colors() []*color.Color {
return []*color.Color{
pr.Active,
pr.Bold,
pr.Bool,
pr.Bytes,
pr.Datetime,
pr.Disabled,
pr.Duration,
pr.Enabled,
pr.Error,
pr.Faint,
pr.Handle,
pr.Header,
pr.Hilite,
pr.Key,
pr.Location,
pr.Normal,
pr.Null,
pr.Number,
pr.Punc,
pr.Stack,
pr.StackError,
pr.StackErrorType,
pr.String,
pr.Success,
pr.Warning,
}
}
// IsMonochrome returns true if in monochrome (no color) mode.
// Default is false (color enabled) for a new instance.
func (pr *Printing) IsMonochrome() bool {
return pr.monochrome
}
// EnableColor enables or disables all colors.
func (pr *Printing) EnableColor(enable bool) {
pr.Diff.EnableColor(enable)
if enable {
pr.monochrome = false
for _, clr := range pr.colors() {
clr.EnableColor()
}
return
}
pr.monochrome = true
for _, clr := range pr.colors() {
clr.DisableColor()
}
}