summaryrefslogtreecommitdiff
path: root/internal/color/colorfy.go
blob: 9ae46f57cce0f9e3765ac9aa2fc8f4d121decba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package color

import (
	"fmt"
	"strings"
)

// 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] = Paint(BgGreen, splitted[2])
	} else {
		splitted[2] = Paint(BgRed, splitted[2])
	}
	info := strings.Join(splitted[0:5], "|")
	log := strings.Join(splitted[5:], "|")

	if strings.HasPrefix(log, "WARN") {
		log = Paint(BgYellow, log)
	} else if strings.HasPrefix(log, "ERROR") {
		log = Paint(BgRed, log)
	} else if strings.HasPrefix(log, "FATAL") {
		log = Attr(Bold, Paint(BgRed, log))
	} else {
		log = Paint(Blue, log)
	}

	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 := Paint(BgBlue, splitted[4])
	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 {
	if strings.HasPrefix(line, "REMOTE") {
		return paintRemote(line)
	}
	if strings.HasPrefix(line, "CLIENT") && strings.Contains(line, "|stats|") {
		return paintClientStats(line)
	}
	if strings.Contains(line, "ERROR") {
		return Paint(Magenta, line)
	}
	if strings.Contains(line, "WARN") {
		return Paint(Magenta, line)
	}

	return line
}