2020-08-06 20:58:47 +03:00
|
|
|
package output
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
import (
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"golang.org/x/exp/slog"
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
// Printing describes color and pretty-printing options.
|
|
|
|
type Printing struct {
|
2020-08-06 20:58:47 +03:00
|
|
|
monochrome bool
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
// FlushThreshold is the size in bytes after which an output writer
|
|
|
|
// should flush any internal buffer.
|
|
|
|
FlushThreshold int
|
|
|
|
|
2022-12-17 06:46:37 +03:00
|
|
|
// 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
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Pretty indicates that output should be pretty-printed.
|
2023-04-22 06:36:32 +03:00
|
|
|
// Typically this means indentation, new lines, etc., but
|
2020-08-06 20:58:47 +03:00
|
|
|
// varies by output format.
|
|
|
|
Pretty bool
|
|
|
|
|
|
|
|
// Indent is the indent string to use when pretty-printing,
|
|
|
|
// typically two spaces.
|
|
|
|
Indent string
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
// Redact indicates that sensitive fields (such as passwords)
|
|
|
|
// should be redacted (hidden/masked).
|
|
|
|
//
|
|
|
|
// TODO: Redact is not being honored by the writers.
|
|
|
|
Redact bool
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// 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
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// Duration is the color for time duration values.
|
|
|
|
Duration *color.Color
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Error is the color for error elements such as an error message.
|
|
|
|
Error *color.Color
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
// Faint is the color for faint elements - the opposite of Hilite.
|
|
|
|
Faint *color.Color
|
|
|
|
|
|
|
|
// Handle is the color for source handles such as "@sakila"
|
2020-08-06 20:58:47 +03:00
|
|
|
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
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
// Location is the color for Source.Location values.
|
|
|
|
Location *color.Color
|
|
|
|
|
|
|
|
// Null is the color for null.
|
|
|
|
Null *color.Color
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Normal is the default color.
|
|
|
|
Normal *color.Color
|
|
|
|
|
2023-04-16 01:28:51 +03:00
|
|
|
// Number is the color for number values, including int, float, decimal etc.
|
|
|
|
Number *color.Color
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Punc is the color for punctuation such as colons, braces, etc.
|
|
|
|
Punc *color.Color
|
2023-04-16 01:28:51 +03:00
|
|
|
|
|
|
|
// String is the color for string values.
|
|
|
|
String *color.Color
|
|
|
|
|
|
|
|
// Success is the color for success elements.
|
|
|
|
Success *color.Color
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
// NewPrinting returns a Printing instance. Color and pretty-print
|
2020-08-06 20:58:47 +03:00
|
|
|
// are enabled. The default indent is two spaces.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewPrinting() *Printing {
|
|
|
|
pr := &Printing{
|
2023-05-03 15:36:10 +03:00
|
|
|
ShowHeader: true,
|
|
|
|
Verbose: false,
|
|
|
|
Pretty: true,
|
|
|
|
Redact: true,
|
|
|
|
FlushThreshold: 1000,
|
|
|
|
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),
|
|
|
|
Duration: 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),
|
|
|
|
// Header: color.New(color.FgBlue, color.Bold),
|
|
|
|
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),
|
|
|
|
Success: color.New(color.FgGreen, color.Bold),
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
pr.EnableColor(true)
|
|
|
|
return pr
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
// LogValue implements slog.LogValuer.
|
|
|
|
func (pr *Printing) LogValue() slog.Value {
|
|
|
|
if pr == nil {
|
|
|
|
return slog.Value{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return slog.GroupValue(
|
|
|
|
slog.Bool("verbose", pr.Verbose),
|
|
|
|
slog.Bool("header", pr.ShowHeader),
|
|
|
|
slog.Bool("monochrome", pr.monochrome),
|
|
|
|
slog.Bool("pretty", pr.Pretty),
|
|
|
|
slog.Bool("redact", pr.Redact),
|
|
|
|
slog.Int("flush-threshold", pr.FlushThreshold),
|
|
|
|
slog.String("indent", pr.Indent),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
func (pr *Printing) colors() []*color.Color {
|
|
|
|
return []*color.Color{
|
2023-04-26 18:16:42 +03:00
|
|
|
pr.Active, pr.Bold, pr.Bold, pr.Bytes, pr.Datetime, pr.Duration,
|
2023-04-22 06:36:32 +03:00
|
|
|
pr.Error, pr.Faint, pr.Handle, pr.Header, pr.Hilite,
|
2023-05-01 06:59:34 +03:00
|
|
|
pr.Key, pr.Location, pr.Normal, pr.Null, pr.Number,
|
2023-04-22 06:36:32 +03:00
|
|
|
pr.Punc, pr.String, pr.Success,
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsMonochrome returns true if in monochrome (no color) mode.
|
|
|
|
// Default is false (color enabled) for a new instance.
|
2023-04-22 06:36:32 +03:00
|
|
|
func (pr *Printing) IsMonochrome() bool {
|
|
|
|
return pr.monochrome
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnableColor enables or disables all colors.
|
2023-04-22 06:36:32 +03:00
|
|
|
func (pr *Printing) EnableColor(enable bool) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if enable {
|
2023-04-22 06:36:32 +03:00
|
|
|
pr.monochrome = false
|
|
|
|
|
|
|
|
for _, clr := range pr.colors() {
|
|
|
|
clr.EnableColor()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.monochrome = true
|
|
|
|
for _, clr := range pr.colors() {
|
|
|
|
clr.DisableColor()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|