summaryrefslogtreecommitdiff
path: root/internal/color
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-08-12 10:56:36 +0300
committerPaul Buetow <paul@buetow.org>2021-08-12 10:56:36 +0300
commit3cc8887885f24a3f0d607af24197bc364ab16b8d (patch)
tree48e85a54d3e4f6139001a58a3e57dd03b27c780a /internal/color
parent1e00c256842e125a865a6cc3f9aa70a1a498f6dc (diff)
add missing brush and also add color client configs plus jsonschema
Diffstat (limited to 'internal/color')
-rw-r--r--internal/color/brush/brush.go104
-rw-r--r--internal/color/color.go2
-rw-r--r--internal/color/color_test.go1
3 files changed, 106 insertions, 1 deletions
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{}