-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.go
72 lines (61 loc) · 1.01 KB
/
style.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
package ui
import (
"fmt"
"strconv"
"strings"
)
type ANSICode int
const (
Reset ANSICode = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)
const (
FgBlack ANSICode = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
const (
BgBlack ANSICode = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
type Style []ANSICode
var (
Black = Style{FgBlack}
Red = Style{FgRed}
Green = Style{FgGreen}
Yellow = Style{FgYellow}
Blue = Style{FgBlue}
Magenta = Style{FgMagenta}
Cyan = Style{FgCyan}
White = Style{FgWhite}
)
func (s Style) sprintf(format string, a ...interface{}) string {
const escape = "\x1b"
codes := make([]string, len(s))
for i, v := range s {
codes[i] = strconv.Itoa(int(v))
}
sequence := strings.Join(codes, ";")
ansiFormat := fmt.Sprintf("%s[%sm", escape, sequence)
ansiReset := fmt.Sprintf("%s[%dm", escape, Reset)
return ansiFormat + fmt.Sprintf(format, a...) + ansiReset
}