This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.go
180 lines (157 loc) · 5.12 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Package log based on https://en.wikipedia.org/wiki/ANSI_escape_code
package log
import (
"fmt"
"strconv"
)
// general Style(s)any
var (
StyleReset = newInt(0)
StyleBold = newInt(1)
StyleFaint = newInt(2)
StyleItalic = newInt(3)
StyleUnderline = newInt(4)
StyleSlowBlink = newInt(5)
StyleRapidBlink = newInt(6)
StyleInvert = newInt(7)
StyleHide = newInt(8)
StyleStrike = newInt(9)
StyleDefaultFont = newInt(10)
)
// AlternateFont returns a Style which alternates the font
func AlternateFont(font int) Style {
if font < 1 || font > 9 {
panic("font can't be smaller than 1 and bigger than 9")
}
return newInt(font + 10)
}
// more general Style(s)any
var (
StyleBlackLetterFont = newInt(20)
StyleDoubleUnderlined = newInt(21)
StyleNormalIntensity = newInt(22)
StyleNeitherItalicNorBlackLetter = newInt(23)
StyleNotUnderlined = newInt(24)
StyleNotBlinking = newInt(25)
StyleProportionalSpacing = newInt(26)
StyleNotReversed = newInt(27)
StyleReveal = newInt(28)
StyleNotCrossedOut = newInt(29)
)
// foreground color Style(s)any
var (
ForegroundColorBlack = newInt(30)
ForegroundColorRed = newInt(31)
ForegroundColorGreen = newInt(32)
ForegroundColorYellow = newInt(33)
ForegroundColorBlue = newInt(34)
ForegroundColorMagenta = newInt(35)
ForegroundColorCyan = newInt(36)
ForegroundColorWhite = newInt(37)
)
// SetForegroundColor returns a Style which sets the foreground color
func SetForegroundColor(r, g, b int) Style {
return colorStyle(38, r, g, b)
}
// background color Style(s)any
var (
DefaultForegroundColor = newInt(39)
BackgroundColorBlack = newInt(40)
BackgroundColorRed = newInt(41)
BackgroundColorGreen = newInt(42)
BackgroundColorYellow = newInt(43)
BackgroundColorBlue = newInt(44)
BackgroundColorMagenta = newInt(45)
BackgroundColorCyan = newInt(46)
BackgroundColorWhite = newInt(47)
)
// SetBackgroundColor returns a Style which sets the background color
func SetBackgroundColor(r, g, b int) Style {
return colorStyle(48, r, g, b)
}
// more general Style(s)any
var (
DefaultBackgroundColor = newInt(49)
StyleDisableProportionalSpacing = newInt(50)
StyleFramed = newInt(51)
StyleEncircled = newInt(52)
StyleOverlined = newInt(53)
StyleNeitherFramedNorEncircled = newInt(54)
StyleNotOverlined = newInt(55)
)
// SetUnderlineColor returns a Style which sets the underline color
func SetUnderlineColor(r, g, b int) Style {
return colorStyle(58, r, g, b)
}
// more general Style(s)any
var (
StyleDefaultUnderlineColor = newInt(59)
StyleIdeogramUnderlineOrRightSideLine = newInt(60)
StyleIdeogramDoubleUnderlineOrDoubleLineOnRightSide = newInt(61)
StyleIdeogramOverlineOrLeftSideLine = newInt(62)
StyleIdeogramDoubleOverlineOrDoubleLineOnTheLeftSide = newInt(63)
StyleIdeogramStressMarking = newInt(64)
StyleNoIdeogramAttributes = newInt(65)
)
// super/subscript Style(s)any
var (
StyleSuperscript = newInt(73)
StyleSubscript = newInt(74)
StyleNeitherSuperscriptNorSubscript = newInt(75)
)
// foreground bright color Style(s)any
var (
ForegroundColorBrightBlack = newInt(90)
ForegroundColorBrightRed = newInt(91)
ForegroundColorBrightGreen = newInt(92)
ForegroundColorBrightYellow = newInt(93)
ForegroundColorBrightBlue = newInt(94)
ForegroundColorBrightMagenta = newInt(95)
ForegroundColorBrightCyan = newInt(96)
ForegroundColorBrightWhite = newInt(97)
)
// background bright color Style(s)any
var (
BackgroundColorBrightBlack = newInt(100)
BackgroundColorBrightRed = newInt(101)
BackgroundColorBrightGreen = newInt(102)
BackgroundColorBrightYellow = newInt(103)
BackgroundColorBrightBlue = newInt(104)
BackgroundColorBrightMagenta = newInt(105)
BackgroundColorBrightCyan = newInt(106)
BackgroundColorBrightWhite = newInt(107)
)
func newInt(c int) Style {
return Style("\033[" + strconv.Itoa(c) + "m")
}
func newStr(str string) Style {
return Style("\033[" + str + "m")
}
func colorStyle(c, r, g, b int) Style {
return newStr(fmt.Sprintf("%d;%d;%d;%d;%d m", c, 2, r, g, b))
}
// Style represents a text
type Style string
// String returns the Style as string to be used in a Terminal
func (s Style) String() string {
return string(s)
}
// And adds 2 Style(s) together
func (s Style) And(style Style) Style {
return s + style
}
// Apply applies a given Style to the given text
func (s Style) Apply(text string) string {
return s.String() + text + StyleReset.String()
}
// ApplyClear applies a given Style to the given text with clearing all Style(s) before
func (s Style) ApplyClear(text string) string {
return StyleReset.String() + s.Apply(text)
}
// ApplyStyles wraps a given message in the given Style(s).
func ApplyStyles(message string, colors ...Style) string {
for _, color := range colors {
message = color.Apply(message)
}
return message
}