-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
174 lines (138 loc) · 3.36 KB
/
message.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
package goconsole
// MessagePrinter print a colored message (Title: [tags] message).
type MessagePrinter interface {
// Indent add indent to message.
Indent() MessagePrinter
// Red make message title red.
Red(title string) MessagePrinter
// Green make message title green.
Green(title string) MessagePrinter
// Yellow make message title yellow.
Yellow(title string) MessagePrinter
// Blue make message title blue.
Blue(title string) MessagePrinter
// Purple make message title purple.
Purple(title string) MessagePrinter
// Cyan make message title cyan.
Cyan(title string) MessagePrinter
// Underline add underline style to message details.
Underline() MessagePrinter
// Strike add strike style to message details.
Strike() MessagePrinter
// Italic add italic style to message details.
Italic() MessagePrinter
// Tags add tags to message.
Tags(tags ...string) MessagePrinter
// Print print message
Print(message string)
// Printf print message with fmt style
Printf(pattern string, args ...any)
}
func Message() MessagePrinter {
return new(message)
}
// implementation
type message struct {
title string
color string
style string
indent string
tags []string
}
func (msg *message) Indent() MessagePrinter {
msg.indent = msg.indent + " "
return msg
}
func (msg *message) Red(title string) MessagePrinter {
msg.color = "r"
msg.title = title
return msg
}
func (msg *message) Green(title string) MessagePrinter {
msg.color = "g"
msg.title = title
return msg
}
func (msg *message) Yellow(title string) MessagePrinter {
msg.color = "y"
msg.title = title
return msg
}
func (msg *message) Blue(title string) MessagePrinter {
msg.color = "b"
msg.title = title
return msg
}
func (msg *message) Purple(title string) MessagePrinter {
msg.color = "p"
msg.title = title
return msg
}
func (msg *message) Cyan(title string) MessagePrinter {
msg.color = "c"
msg.title = title
return msg
}
func (msg *message) Underline() MessagePrinter {
msg.style = msg.style + "U"
return msg
}
func (msg *message) Strike() MessagePrinter {
msg.style = msg.style + "S"
return msg
}
func (msg *message) Italic() MessagePrinter {
msg.style = msg.style + "I"
return msg
}
func (msg *message) Tags(tags ...string) MessagePrinter {
msg.tags = append(msg.tags, tags...)
return msg
}
func (msg *message) Print(message string) {
title := ""
tags := ""
params := make([]any, 0)
// Generate title style
if msg.title != "" {
title = "@B" + msg.color + "{%s:} "
params = append(params, msg.title)
}
// Generate tags
for _, tag := range msg.tags {
if tag != "" {
tags = tags + "[%s] "
params = append(params, tag)
}
}
// Generate message
if msg.style != "" {
message = "@" + msg.style + "{" + message + "}"
}
// Print message
PrintF(msg.indent+title+tags+message+"\n", params...)
}
func (msg *message) Printf(pattern string, args ...any) {
title := ""
tags := ""
params := make([]any, 0)
// Generate title style
if msg.title != "" {
title = "@B" + msg.color + "{%s:} "
params = append(params, msg.title)
}
// Generate tags
for _, tag := range msg.tags {
if tag != "" {
tags = tags + "[%s] "
params = append(params, tag)
}
}
// Generate message
if msg.style != "" {
pattern = "@" + msg.style + "{" + pattern + "}"
}
// Print message
params = append(params, args...)
PrintF(msg.indent+title+tags+pattern+"\n", params...)
}