-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhighlight.go
131 lines (101 loc) · 2.98 KB
/
highlight.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package highlight
import (
"bufio"
"fmt"
"io"
"strings"
. "github.com/logrusorgru/aurora"
)
func Highlight(r io.Reader) (string, error) {
// Service vars
foundChompingIndicator := false
indentationSpacesBeforeComment := 0
// Warm-up the engine
scanner := bufio.NewScanner(r)
var buf strings.Builder
// Get the juice
for scanner.Scan() {
if scanner.Text() == "EOF" {
break
}
// Check for errors during Stdin read
if err := scanner.Err(); err != nil {
return "", err
}
// Drink the juice
l := yamlLine{raw: scanner.Text()}
if foundChompingIndicator && (l.indentationSpaces() > indentationSpacesBeforeComment) {
// Found multiline comment or configmap, not treated as YAML at all
buf.WriteString(multiline(l))
} else if l.isKeyValue() {
// This is a valid YAML key: value line. Key and value are returned in l
if l.isComment() {
// This line is a comment
buf.WriteString(comment(l))
} else if l.valueIsNumberOrIP() {
// The value is a number or an IP address x.x.x.x
buf.WriteString(keyNumberOrIP(l))
} else if l.valueIsBoolean() {
// The value is boolean true or false
buf.WriteString(keyBool(l))
} else {
// The is a normal key/value line
buf.WriteString(keyValue(l))
}
if l.valueContainsChompingIndicator() {
// This line contains a chomping indicator, sign of a possible multiline text coming next
// Setting flag for next execution
foundChompingIndicator = true
// Saving current number of indentation spaces
indentationSpacesBeforeComment = l.indentationSpaces()
} else {
// Resetting multiline flag
foundChompingIndicator = false
}
} else if !l.isEmptyLine() {
// This is not a YAML key: value line and is not empty
if l.isUrl() {
// Value is a URL
buf.WriteString(url(l))
} else if l.isComment() {
// This line is a comment
buf.WriteString(comment(l))
} else if l.isElementOfList() {
// This line is an element of a list
buf.WriteString(listElement(l))
} else {
// This line is not valid YAML
buf.WriteString(invalidLine(l))
}
foundChompingIndicator = false
} else if l.isEmptyLine() {
// This is an empty line
fmt.Println(l.raw)
}
}
return buf.String(), nil
}
func keyValue(l yamlLine) string {
return fmt.Sprintf("%v: %v\n", BrightRed(l.key), Yellow(l.value))
}
func keyNumberOrIP(l yamlLine) string {
return fmt.Sprintf("%v: %v\n", BrightRed(l.key), Blue(l.value))
}
func keyBool(l yamlLine) string {
return fmt.Sprintf("%v: %v\n", BrightRed(l.key), Blue(l.value))
}
func comment(l yamlLine) string {
return fmt.Sprintf("%v %v\n", Gray(13, l.key), Gray(13, l.value))
}
func listElement(l yamlLine) string {
return fmt.Sprintf("%v\n", Yellow(l.raw))
}
func invalidLine(l yamlLine) string {
return fmt.Sprintf("%v\n", Black(l.raw).BgBrightRed())
}
func multiline(l yamlLine) string {
return fmt.Sprintf("%v\n", Gray(20-1, l.raw))
}
func url(l yamlLine) string {
return fmt.Sprintf("%v\n", Yellow(l.raw))
}