From 3cc8887885f24a3f0d607af24197bc364ab16b8d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 12 Aug 2021 10:56:36 +0300 Subject: add missing brush and also add color client configs plus jsonschema --- internal/color/brush/brush.go | 104 ++++++++++++++++++++++++++++++++++++++++++ internal/color/color.go | 2 +- internal/color/color_test.go | 1 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 internal/color/brush/brush.go (limited to 'internal/color') diff --git a/internal/color/brush/brush.go b/internal/color/brush/brush.go new file mode 100644 index 0000000..c5efff6 --- /dev/null +++ b/internal/color/brush/brush.go @@ -0,0 +1,104 @@ +package brush + +import ( + "fmt" + "strings" + + "github.com/mimecast/dtail/internal/color" + "github.com/mimecast/dtail/internal/config" +) + +// Add some color to log lines received from remote servers. +func paintRemote(line string) string { + splitted := strings.Split(line, "|") + if splitted[2] == "100" { + splitted[2] = color.Paint(splitted[2], + config.Client.TermColors.RemoteStatsOkFg, + config.Client.TermColors.RemoteStatsOkBg) + } else { + splitted[2] = color.Paint(splitted[2], + config.Client.TermColors.RemoteStatsWarnFg, + config.Client.TermColors.RemoteStatsWarnBg) + } + + info := strings.Join(splitted[0:5], "|") + log := strings.Join(splitted[5:], "|") + + switch { + case strings.HasPrefix(log, "WARN"): + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteWarnFg, + config.Client.TermColors.RemoteWarnBg, + config.Client.TermColors.RemoteWarnAttr) + + case strings.HasPrefix(log, "ERROR"): + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteErrorFg, + config.Client.TermColors.RemoteErrorBg, + config.Client.TermColors.RemoteErrorAttr) + + case strings.HasPrefix(log, "FATAL"): + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteFatalFg, + config.Client.TermColors.RemoteFatalBg, + config.Client.TermColors.RemoteFatalAttr) + + case strings.HasPrefix(log, "DEBUG"): + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteDebugFg, + config.Client.TermColors.RemoteDebugBg, + config.Client.TermColors.RemoteDebugAttr) + + case strings.HasPrefix(log, "TRACE"): + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteTraceFg, + config.Client.TermColors.RemoteTraceBg, + config.Client.TermColors.RemoteTraceAttr) + + default: + log = color.PaintWithAttr(log, + config.Client.TermColors.RemoteTextFg, + config.Client.TermColors.RemoteTextBg, + config.Client.TermColors.RemoteTextAttr) + } + + return fmt.Sprintf("%s|%s", info, log) +} + +// Add some color to stats generated by the client. +func paintClientStats(line string) string { + splitted := strings.Split(line, "|") + first := strings.Join(splitted[0:4], "|") + connected := color.PaintWithAttr(splitted[4], + config.Client.TermColors.ClientStatsFg, + config.Client.TermColors.ClientStatsBg, + config.Client.TermColors.ClientStatsAttr) + last := strings.Join(splitted[5:], "|") + + return fmt.Sprintf("%s|%s|%s", first, connected, last) +} + +// Colorfy a given line based on the line's content. +func Colorfy(line string) string { + switch { + case strings.HasPrefix(line, "REMOTE"): + return paintRemote(line) + + case strings.HasPrefix(line, "CLIENT") && strings.Contains(line, "|stats|"): + return paintClientStats(line) + + case strings.Contains(line, "ERROR"): + return color.PaintWithAttr(line, + config.Client.TermColors.ClientErrorFg, + config.Client.TermColors.ClientErrorBg, + config.Client.TermColors.ClientErrorAttr) + + case strings.Contains(line, "WARN"): + return color.PaintWithAttr(line, + config.Client.TermColors.ClientWarnFg, + config.Client.TermColors.ClientWarnBg, + config.Client.TermColors.ClientWarnAttr) + } + + return line +} diff --git a/internal/color/color.go b/internal/color/color.go index 692de51..5c25855 100644 --- a/internal/color/color.go +++ b/internal/color/color.go @@ -138,6 +138,6 @@ func ToAttribute(s string) (Attribute, error) { case "": return AttrNone, nil default: - return AttrReset, fmt.Errorf("unknown text attribute '" + s + "'") + return AttrNone, fmt.Errorf("unknown text attribute '" + s + "'") } } diff --git a/internal/color/color_test.go b/internal/color/color_test.go index 16b2f90..bfc7c54 100644 --- a/internal/color/color_test.go +++ b/internal/color/color_test.go @@ -36,6 +36,7 @@ func TestColors(t *testing.T) { t.Log(builder.String()) } + func TestAttributes(t *testing.T) { text := " Mimecast " builder := strings.Builder{} -- cgit v1.2.3