-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.go
More file actions
126 lines (114 loc) · 2.88 KB
/
Shell.go
File metadata and controls
126 lines (114 loc) · 2.88 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
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
package u
import (
"fmt"
"runtime"
"strconv"
"strings"
)
type AttribCode int
const (
AttrNone AttribCode = iota
AttrBold
AttrDim
AttrItalic
AttrUnderline
AttrBlink
AttrFastBlink
AttrReverse
AttrHidden
AttrCrossedOut
)
type TextColor int
const (
TextBlack TextColor = iota + 30
TextRed
TextGreen
TextYellow
TextBlue
TextMagenta
TextCyan
TextWhite
TextNone TextColor = 0
)
type BgColor int
const (
BgBlack BgColor = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgNone BgColor = 0
)
func Black(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlack, BgNone, attribs...)
}
func Red(s interface{}, attribs ...AttribCode) string {
return Color(s, TextRed, BgNone, attribs...)
}
func Green(s interface{}, attribs ...AttribCode) string {
return Color(s, TextGreen, BgNone, attribs...)
}
func Yellow(s interface{}, attribs ...AttribCode) string {
return Color(s, TextYellow, BgNone, attribs...)
}
func Blue(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlue, BgNone, attribs...)
}
func Magenta(s interface{}, attribs ...AttribCode) string {
return Color(s, TextMagenta, BgNone, attribs...)
}
func Cyan(s interface{}, attribs ...AttribCode) string {
return Color(s, TextCyan, BgNone, attribs...)
}
func White(s interface{}, attribs ...AttribCode) string {
return Color(s, TextWhite, BgNone, attribs...)
}
func Dim(s interface{}) string {
return Color(s, TextWhite, BgNone, AttrDim)
}
func Italic(s interface{}) string {
return Color(s, TextWhite, BgNone, AttrItalic)
}
func BBlack(s interface{}, attribs ...AttribCode) string {
return Color(s, TextWhite, BgBlack, attribs...)
}
func BRed(s interface{}, attribs ...AttribCode) string {
return Color(s, TextWhite, BgRed, attribs...)
}
func BGreen(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlack, BgGreen, attribs...)
}
func BYellow(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlack, BgYellow, attribs...)
}
func BBlue(s interface{}, attribs ...AttribCode) string {
return Color(s, TextWhite, BgBlue, attribs...)
}
func BMagenta(s interface{}, attribs ...AttribCode) string {
return Color(s, TextWhite, BgMagenta, attribs...)
}
func BCyan(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlack, BgCyan, attribs...)
}
func BWhite(s interface{}, attribs ...AttribCode) string {
return Color(s, TextBlack, BgWhite, attribs...)
}
func Color(s interface{}, textColor TextColor, bgColor BgColor, attribs ...AttribCode) string {
if runtime.GOOS == "windows" {
return String(s)
}
sets := make([]string, 0)
for _, attr := range attribs {
sets = append(sets, strconv.Itoa(int(attr)))
}
if textColor != TextNone {
sets = append(sets, strconv.Itoa(int(textColor)))
}
if bgColor != BgNone {
sets = append(sets, strconv.Itoa(int(bgColor)))
}
return fmt.Sprint("\033[", strings.Join(sets, ";"), "m", String(s), "\033[0m")
}