Browse Source

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
boyska 5 years ago
parent
commit
46e3f6c883
1 changed files with 10 additions and 1 deletions
  1. 10 1
      formatter/format.go

+ 10 - 1
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\"}}",