-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.go
87 lines (70 loc) · 1.37 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package style
import (
"fmt"
"github.com/hopeio/cherry/utils/slices"
"strconv"
)
var styleFormat = "\x1b[%sm%s\x1b[0m"
func colorize(colorCode Style, s string) string {
return fmt.Sprintf(styleFormat, colorCode.String(), s)
}
func StyleFormat(s string, styles ...Style) string {
if len(styles) == 0 {
return s
}
return fmt.Sprintf(styleFormat, slices.Join(styles, ";"), s)
}
type Style int
func (d Style) String() string {
return strconv.Itoa(int(d))
}
const (
DecorationNormal Style = iota
DecorationBold
DecorationItalic
DecorationUnderline
)
const (
ColorBlack Style = 30 + iota
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorPurple
ColorLightGreen
ColorGray
)
const (
BackGroundBlack Style = 40 + iota
BackgroundRed
BackgroundGreen
BackgroundYellow
BackgroundBlue
BackgroundPurple
BackgroundLightGreen
BackgroundGray
)
func Blue(s string) string {
return colorize(ColorBlue, s)
}
func LightGreen(s string) string {
return colorize(ColorLightGreen, s)
}
func Purple(s string) string {
return colorize(ColorPurple, s)
}
func Gray(s string) string {
return colorize(ColorGray, s)
}
func Red(s string) string {
return colorize(ColorRed, s)
}
func RedBackground(s string) string {
return colorize(BackgroundRed, s)
}
func Green(s string) string {
return colorize(ColorGreen, s)
}
func Yellow(s string) string {
return colorize(ColorYellow, s)
}