From 46e3f6c883237d86e62a490b3f0f6260c13ea0d3 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 3 Jan 2019 12:05:04 +0100 Subject: [PATCH] each app has its color it is picked from a palette based on its hash: it is pseudorandom, but still consistent across different lines and different runs. The palette is a bit too vivid, but let's stick with it for now. fixes #10 --- formatter/format.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/formatter/format.go b/formatter/format.go index 86e0a09..eda0a41 100644 --- a/formatter/format.go +++ b/formatter/format.go @@ -3,6 +3,7 @@ package formatter import ( "encoding/json" "fmt" + "hash/fnv" "io" "text/template" "time" @@ -45,10 +46,18 @@ func init() { return ansi.Color(text, color) // slow; should use colorfunc }, "red": ansi.ColorFunc("red+b"), + "autoColor": func(s string) string { + // from https://weechat.org/blog/post/2011/08/28/Beautify-your-WeeChat + palette := []string{"31", "35", "38", "40", "49", "63", "70", "80", "92", "99", "112", "126", "130", "138", "142", "148", "167", "169", "174", "176", "178", "184", "186", "210", "212", "215", "247"} + hash := fnv.New32() + hash.Write([]byte(s)) + picked := palette[int(hash.Sum32())%len(palette)] + return ansi.Color(s, picked) + }, } syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse( "{{color \"yellow\" (rfc822 (index . \"timestamp\")) }} {{index . \"hostname\"}} " + - "{{index . \"app_name\"}}" + + "{{index . \"app_name\" | autoColor}}" + "{{ if (ne (index . \"proc_id\") \"-\")}}[{{index . \"proc_id\"}}]{{end}}: " + "{{ sevName (index . \"severity\") }} " + "{{index . \"message\"}}",