diff options
| author | Paul Buetow <paul@buetow.org> | 2021-09-05 15:51:26 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-09-05 15:51:26 +0300 |
| commit | bfcb0f159a4835cc6a0326ff46de7934e0363aed (patch) | |
| tree | 92a3f9363df0e70bf54a2a3c99928aa6a4ef861c /internal/color | |
| parent | 23982f331c2154a66b86d596226c24454fd06be5 (diff) | |
finalize new default color schema
Diffstat (limited to 'internal/color')
| -rw-r--r-- | internal/color/brush/brush.go | 110 | ||||
| -rw-r--r-- | internal/color/paint.go | 59 | ||||
| -rw-r--r-- | internal/color/table.go | 2 |
3 files changed, 121 insertions, 50 deletions
diff --git a/internal/color/brush/brush.go b/internal/color/brush/brush.go index 87c02d0..415028a 100644 --- a/internal/color/brush/brush.go +++ b/internal/color/brush/brush.go @@ -1,7 +1,6 @@ package brush import ( - "fmt" "strings" "github.com/mimecast/dtail/internal/color" @@ -10,96 +9,133 @@ import ( ) // Add some color to log lines received from remote servers. -func paintRemote(line string) string { - splitted := strings.Split(line, protocol.FieldDelimiter) +func paintRemote(sb *strings.Builder, line string) { + splitted := strings.SplitN(line, protocol.FieldDelimiter, 6) + + color.PaintWithAttr(sb, splitted[0], + config.Client.TermColors.RemoteStrFg, + config.Client.TermColors.RemoteStrBg, + config.Client.TermColors.RemoteStrAttr) + + color.PaintWithAttr(sb, protocol.FieldDelimiter, + config.Client.TermColors.DelimiterFg, + config.Client.TermColors.DelimiterBg, + config.Client.TermColors.DelimiterAttr) + + color.PaintWithAttr(sb, splitted[1], + config.Client.TermColors.RemoteServerFg, + config.Client.TermColors.RemoteServerBg, + config.Client.TermColors.RemoteServerAttr) + + color.PaintWithAttr(sb, protocol.FieldDelimiter, + config.Client.TermColors.DelimiterFg, + config.Client.TermColors.DelimiterBg, + config.Client.TermColors.DelimiterAttr) + if splitted[2] == "100" { - splitted[2] = color.Paint(splitted[2], + color.PaintWithAttr(sb, splitted[2], config.Client.TermColors.RemoteStatsOkFg, - config.Client.TermColors.RemoteStatsOkBg) + config.Client.TermColors.RemoteStatsOkBg, + config.Client.TermColors.RemoteStatsOkAttr) } else { - splitted[2] = color.Paint(splitted[2], + color.PaintWithAttr(sb, splitted[2], config.Client.TermColors.RemoteStatsWarnFg, - config.Client.TermColors.RemoteStatsWarnBg) + config.Client.TermColors.RemoteStatsWarnBg, + config.Client.TermColors.RemoteStatsWarnAttr) } - info := strings.Join(splitted[0:5], protocol.FieldDelimiter) - log := strings.Join(splitted[5:], protocol.FieldDelimiter) + color.PaintWithAttr(sb, protocol.FieldDelimiter, + config.Client.TermColors.DelimiterFg, + config.Client.TermColors.DelimiterBg, + config.Client.TermColors.DelimiterAttr) + + color.PaintWithAttr(sb, splitted[3], + config.Client.TermColors.RemoteCountFg, + config.Client.TermColors.RemoteCountBg, + config.Client.TermColors.RemoteCountAttr) + + color.PaintWithAttr(sb, protocol.FieldDelimiter, + config.Client.TermColors.DelimiterFg, + config.Client.TermColors.DelimiterBg, + config.Client.TermColors.DelimiterAttr) + + color.PaintWithAttr(sb, splitted[4], + config.Client.TermColors.RemoteIdFg, + config.Client.TermColors.RemoteIdBg, + config.Client.TermColors.RemoteIdAttr) + color.PaintWithAttr(sb, protocol.FieldDelimiter, + config.Client.TermColors.DelimiterFg, + config.Client.TermColors.DelimiterBg, + config.Client.TermColors.DelimiterAttr) + + log := splitted[5] switch { case strings.HasPrefix(log, "WARN"): - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, log, config.Client.TermColors.RemoteWarnFg, config.Client.TermColors.RemoteWarnBg, config.Client.TermColors.RemoteWarnAttr) case strings.HasPrefix(log, "ERROR"): - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, log, config.Client.TermColors.RemoteErrorFg, config.Client.TermColors.RemoteErrorBg, config.Client.TermColors.RemoteErrorAttr) case strings.HasPrefix(log, "FATAL"): - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, log, config.Client.TermColors.RemoteFatalFg, config.Client.TermColors.RemoteFatalBg, config.Client.TermColors.RemoteFatalAttr) case strings.HasPrefix(log, "DEBUG"): - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, log, config.Client.TermColors.RemoteDebugFg, config.Client.TermColors.RemoteDebugBg, config.Client.TermColors.RemoteDebugAttr) case strings.HasPrefix(log, "TRACE"): - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, log, config.Client.TermColors.RemoteTraceFg, config.Client.TermColors.RemoteTraceBg, config.Client.TermColors.RemoteTraceAttr) default: - log = color.PaintWithAttr(log, + color.PaintWithAttr(sb, 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, protocol.FieldDelimiter) - first := strings.Join(splitted[0:4], protocol.FieldDelimiter) - 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 { + sb := strings.Builder{} + switch { case strings.HasPrefix(line, "REMOTE"): - return paintRemote(line) - - case strings.HasPrefix(line, "CLIENT") && strings.Contains(line, "|stats|"): - return paintClientStats(line) + paintRemote(&sb, line) case strings.Contains(line, "ERROR"): - return color.PaintWithAttr(line, + color.PaintWithAttr(&sb, line, config.Client.TermColors.ClientErrorFg, config.Client.TermColors.ClientErrorBg, config.Client.TermColors.ClientErrorAttr) case strings.Contains(line, "WARN"): - return color.PaintWithAttr(line, + color.PaintWithAttr(&sb, line, config.Client.TermColors.ClientWarnFg, config.Client.TermColors.ClientWarnBg, config.Client.TermColors.ClientWarnAttr) + + default: + color.PaintWithAttr(&sb, line, + color.FgDefault, + color.BgDefault, + color.AttrNone) } - return line + color.ResetWithAttr(&sb) + return sb.String() } diff --git a/internal/color/paint.go b/internal/color/paint.go index 7862467..2798ff7 100644 --- a/internal/color/paint.go +++ b/internal/color/paint.go @@ -1,31 +1,66 @@ package color -import "fmt" +import ( + "fmt" + "strings" +) -// Paint paints a given text in a given foreground/background color combination. -func Paint(text string, fg FgColor, bg BgColor) string { +// PaintStr paints a given text in a given foreground/background color combination. +func PaintStr(text string, fg FgColor, bg BgColor) string { return fmt.Sprintf("%s%s%s%s%s", fg, bg, text, BgDefault, FgDefault) } -// PaintWithAttr paints a given text in a given foreground/background/attribute combination -func PaintWithAttr(text string, fg FgColor, bg BgColor, attr Attribute) string { +// PaintStrWithAttr paints a given text in a given foreground/background/attribute combination +func PaintStrWithAttr(text string, fg FgColor, bg BgColor, attr Attribute) string { if attr == AttrNone { - return Paint(text, fg, bg) + return PaintStr(text, fg, bg) } return fmt.Sprintf("%s%s%s%s%s%s%s", fg, bg, attr, text, AttrReset, BgDefault, FgDefault) } -// PaintFg paints a given text in a given foreground color. -func PaintFg(text string, fg FgColor) string { +// PaintStrFg paints a given text in a given foreground color. +func PaintStrFg(text string, fg FgColor) string { return fmt.Sprintf("%s%s%s", fg, text, FgDefault) } -// PaintBg paints a given text in a given background color. -func PaintBg(text string, bg BgColor) string { +// PaintStrBg paints a given text in a given background color. +func PaintStrBg(text string, bg BgColor) string { return fmt.Sprintf("%s%s%s", bg, text, BgDefault) } -// PaintAttr adds a given attribute to a given text, such as "bold" or "italic". -func PaintAttr(text string, attr Attribute) string { +// PaintStrAttr adds a given attribute to a given text, such as "bold" or "italic". +func PaintStrAttr(text string, attr Attribute) string { return fmt.Sprintf("%s%s%s", attr, text, AttrReset) } + +// Paint paints a given text in a given foreground/background color combination. +func Paint(sb *strings.Builder, text string, fg FgColor, bg BgColor) { + sb.WriteString(string(fg)) + sb.WriteString(string(bg)) + sb.WriteString(text) +} + +// Reset background and foreground colors. +func Reset(sb *strings.Builder) { + sb.WriteString(string(BgDefault)) + sb.WriteString(string(FgDefault)) +} + +// PaintWithAttr starts painting a given text in a given foreground/background/attribute combination. +func PaintWithAttr(sb *strings.Builder, text string, fg FgColor, bg BgColor, attr Attribute) { + if attr == AttrNone { + Paint(sb, text, fg, bg) + return + } + sb.WriteString(string(fg)) + sb.WriteString(string(bg)) + sb.WriteString(string(attr)) + sb.WriteString(text) +} + +// ResetWithAttr resets background, foreground and attributes. +func ResetWithAttr(sb *strings.Builder) { + sb.WriteString(string(AttrReset)) + sb.WriteString(string(BgDefault)) + sb.WriteString(string(FgDefault)) +} diff --git a/internal/color/table.go b/internal/color/table.go index 8c36047..e2265e3 100644 --- a/internal/color/table.go +++ b/internal/color/table.go @@ -26,7 +26,7 @@ func printColorTable(attr string) { attribute, _ := ToAttribute(attr) text := fmt.Sprintf(" Foreground:%10s | Background:%10s | Attribute:%10s ", fg, bg, attr) - fmt.Print(PaintWithAttr(text, fgColor, bgColor, attribute)) + fmt.Print(PaintStrWithAttr(text, fgColor, bgColor, attribute)) fmt.Print("\n") } } |
