mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
enable coloring by UIDs
This commit is contained in:
@@ -2,14 +2,20 @@ package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
ColorNone = iota
|
||||
ColorRed
|
||||
ColorGreen
|
||||
ColorYellow
|
||||
ColorBlue
|
||||
ColorPurple
|
||||
ColorTeal
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
@@ -43,14 +49,15 @@ func (l *Logger) Errorf(debug bool, format string, v ...interface{}) {
|
||||
// Eventf writes an event with timestamp to stdout
|
||||
func (l *Logger) Eventf(color int, format string, v ...interface{}) {
|
||||
msg := fmt.Sprintf(format, v...)
|
||||
|
||||
switch color {
|
||||
case ColorRed:
|
||||
msg = fmt.Sprintf("\x1b[31;1m%s\x1b[0m", msg)
|
||||
case ColorGreen:
|
||||
msg = fmt.Sprintf("\x1b[32;1m%s\x1b[0m", msg)
|
||||
default:
|
||||
if color != ColorNone {
|
||||
msg = fmt.Sprintf("\x1b[%d;1m%s\x1b[0m", 30+color, msg)
|
||||
}
|
||||
|
||||
l.eventLogger.Printf("%s", msg)
|
||||
}
|
||||
|
||||
func GetColorByUID(uid int) int {
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(strconv.Itoa(uid)))
|
||||
return (int(h.Sum32()) % (ColorTeal)) + 1
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ var logTests = []struct {
|
||||
func TestLogging(t *testing.T) {
|
||||
for i, tt := range logTests {
|
||||
actual := captureOutput(tt.logger, tt.test)
|
||||
log.Printf("OUT: %s", actual)
|
||||
|
||||
// check colors and remove afterwards
|
||||
colors := ansiMatcher.FindAll(actual, 2)
|
||||
@@ -55,3 +56,46 @@ func captureOutput(logger *log.Logger, f func()) []byte {
|
||||
f()
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestGetColorByUID(t *testing.T) {
|
||||
tests := []struct {
|
||||
uid int
|
||||
color int
|
||||
}{
|
||||
{uid: 0, color: 4},
|
||||
{uid: 1, color: 5},
|
||||
{uid: 2, color: 2},
|
||||
{uid: 3, color: 3},
|
||||
{uid: 99999999999, color: 5},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
color := GetColorByUID(tt.uid)
|
||||
if color != tt.color {
|
||||
t.Errorf("GetColorByUID(%d)=%d but want %d", tt.uid, color, tt.color)
|
||||
}
|
||||
}
|
||||
|
||||
minColor := 9999999
|
||||
maxColor := -9999999
|
||||
for i := 0; i < 1000; i++ {
|
||||
color := GetColorByUID(i)
|
||||
if color < 1 || color > ColorTeal {
|
||||
t.Fatalf("GetColorByUID(%d)=%d but this is out of range [%d, %d]", i, color, ColorRed, ColorTeal)
|
||||
}
|
||||
|
||||
if color < minColor {
|
||||
minColor = color
|
||||
}
|
||||
if color > maxColor {
|
||||
maxColor = color
|
||||
}
|
||||
}
|
||||
|
||||
if minColor != 1 {
|
||||
t.Errorf("GetColorByUID returned minimum color %d, not %d, on 1000 trials, which is extremely unlikely", minColor, 1)
|
||||
}
|
||||
if maxColor != ColorTeal {
|
||||
t.Errorf("GetColorByUID returned maximum color %d, not %d, on 1000 trials, which is extremely unlikely", maxColor, ColorTeal)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user