-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathshow.go
More file actions
80 lines (65 loc) · 1.45 KB
/
show.go
File metadata and controls
80 lines (65 loc) · 1.45 KB
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
package cli
import (
"fmt"
"runtime/debug"
"strings"
"git.urbach.dev/go/color/ansi"
)
// show parses the text and prints it with colors.
func show(text string) {
for line := range strings.Lines(text) {
start := strings.IndexByte(line, '{')
for start != -1 {
end := strings.IndexByte(line, '}')
key := line[start+1 : end]
value := ""
switch key {
case "commit":
value = buildInfo("vcs.revision")
case "date":
value = buildInfo("vcs.time")
}
line = line[:start] + value + line[end+1:]
start = strings.IndexByte(line, '{')
}
startDimmed := strings.IndexByte(line, '#')
dimmed := ""
if startDimmed != -1 {
dimmed = line[startDimmed:]
line = line[:startDimmed]
}
switch {
case strings.HasPrefix(line, " "):
for {
start := strings.IndexByte(line, '[')
if start == -1 {
ansi.Cyan.Print(line)
break
}
end := strings.IndexByte(line, ']')
ansi.Cyan.Print(line[:start])
ansi.Yellow.Print(line[start : end+1])
line = line[end+1:]
}
case strings.HasPrefix(line, " "):
ansi.Green.Print(line)
case line == "EOF":
default:
fmt.Print(line)
}
if len(dimmed) > 0 {
ansi.Dim.Print(dimmed)
}
}
}
// buildInfo retrieves information about the build.
func buildInfo(key string) string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == key {
return setting.Value
}
}
}
return ""
}