-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.go
56 lines (44 loc) · 1.08 KB
/
string.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
package pp
import "fmt"
type String struct {
s string
color *Color
}
// Pure 返回不包括颜色的底层字符串
func (s String) Pure() string {
return s.s
}
// Get 返回字符串
func (s String) Get(noColor bool) string {
return s.color.Sprint(noColor, s.s)
}
func (s String) GetForStdout() string {
return s.Get(Stdout.NoColor)
}
func (s String) GetForStderr() string {
return s.Get(Stdout.NoColor)
}
// Wrap 返回添加了颜色头尾的底层字符串
func (s String) Wrap() string {
return s.color.Sprint(false, s.s)
}
// Color 返回底层颜色数据
func (s String) Color() *Color {
return s.color
}
func (s *String) ChangeColor(newColor *Color) {
s.color = newColor
}
func (s String) WithColor(newColor *Color) String {
return String{s: s.s, color: newColor}
}
func ColorString(color *Color, format string, a ...interface{}) String {
return colorString(color, format, a...)
}
func colorString(color *Color, format string, a ...interface{}) String {
if len(a) == 0 {
return String{format, color}
} else {
return String{fmt.Sprintf(format, a...), color}
}
}