-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
118 lines (91 loc) · 2.12 KB
/
text.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
package iterm
import (
"fmt"
"github.com/normatov07/iterm/color"
)
type Text struct {
color color.Color
tRGB color.RGB
bColor color.BColor
bRGB color.RGB
bold bool
uline bool
italic bool
text string
}
func NewText(text string) *Text {
return &Text{text: text}
}
func (t *Text) Color(color color.Color) *Text {
t.color = color
return t
}
func (t *Text) BackgroudColor(color color.BColor) *Text {
t.bColor = color
return t
}
func (t *Text) Bold() *Text {
t.bold = true
return t
}
func (t *Text) Italic() *Text {
t.italic = true
return t
}
func (t *Text) UnderLine() *Text {
t.uline = true
return t
}
func (t *Text) GetStyledText() string {
return t.parseStyle()
}
func (t *Text) RGBColor(r, g, b int) *Text {
t.tRGB = color.RGB{R: r, G: g, B: b}
return t
}
func (t *Text) BackgroundRGBColor(r, g, b int) *Text {
t.bRGB = color.RGB{R: r, G: g, B: b}
return t
}
func (t *Text) Print() {
fmt.Print(t.parseStyle())
}
func (t *Text) Println() {
fmt.Println(t.parseStyle())
}
func (t *Text) parseStyle() string {
text := t.text
switch {
case t.bold:
text = fmt.Sprintf("\033[1m%s", text)
fallthrough
case t.italic:
text = fmt.Sprintf("\033[3m%s", text)
fallthrough
case t.uline:
text = fmt.Sprintf("\033[4m%s", text)
fallthrough
case t.color > 0:
text = fmt.Sprintf("\033[%dm%s", t.color, text)
fallthrough
case t.bColor > 0:
text = fmt.Sprintf("\033[%dm%s", t.bColor, text)
fallthrough
case t.tRGB.R > 0 && t.tRGB.G > 0 && t.tRGB.B > 0:
text = fmt.Sprintf("\033[38;2;%d;%d;%dm%s", t.tRGB.R, t.tRGB.G, t.tRGB.B, text)
fallthrough
case t.bRGB.R > 0 && t.bRGB.G > 0 && t.bRGB.B > 0:
text = fmt.Sprintf("\033[48;2;%d;%d;%dm%s", t.bRGB.R, t.bRGB.G, t.bRGB.B, text)
}
text = fmt.Sprintf("%s\033[0m", text)
return text
}
func Colorful(text string, color color.Color) string {
return fmt.Sprintf("\033[%dm%s\033[0m", color, text)
}
func RGBColor(text string, r, g, b int) string {
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, text)
}
func BackgroundRGBColor(text string, r, g, b int) string {
return fmt.Sprintf("\033[48;2;%d;%d;%dm%s\033[0m", r, g, b, text)
}