-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.go
67 lines (61 loc) · 1.52 KB
/
color.go
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
59
60
61
62
63
64
65
66
67
package printer
import (
"fmt"
"io"
"strings"
"github.com/SignorMercurio/limner/color"
"github.com/SignorMercurio/limner/util"
)
type ColorPrinter struct {
Type string
}
func (cp *ColorPrinter) Print(buf string, w io.Writer) {
var (
printer Printer = &SingleColorPrinter{Color: stringColor}
bufByte = []byte(buf)
jsonObj interface{}
isJson bool
)
// change the printer if type is already enforced
switch cp.Type {
case "yaml", "yml":
printer = NewYamlPrinter()
case "json":
if jsonObj, isJson = util.ShouldJson(bufByte); isJson {
printer = NewJsonPrinter(jsonObj)
} else {
fmt.Fprintln(w, color.Apply("Failed to unmarshal json, using default printer", color.Yellow))
}
case "table":
printer = NewTablePrinter(ColorStatus)
default:
// otherwise, try to determine the type
// try json first
jsonObj, isJson = util.ShouldJson(bufByte)
switch {
case isJson:
printer = NewJsonPrinter(jsonObj)
case util.ShouldTable(buf):
printer = NewTablePrinter(ColorStatus)
case util.ShouldYaml(buf):
printer = NewYamlPrinter()
}
}
// Finally, we can print something
printer.Print(buf, w)
}
// ColorStatus colorizes the status based on status texts
func ColorStatus(_ int, status string) (color.Color, bool) {
status = strings.ToLower(status)
switch {
// the order matters!
case seemsNegative(status):
return color.Red, true
case seemsPositive(status):
return color.Green, true
case seemsWarning(status):
return color.Yellow, true
default:
return seemsReadyStatus(status)
}
}