Skip to content

Commit d5924d1

Browse files
committed
up: update some for build full color string
1 parent 85b9eb0 commit d5924d1

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

color.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ const (
2828
)
2929

3030
// color render templates
31+
//
3132
// ESC 操作的表示:
3233
// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
3334
const (
34-
SettingTpl = "\x1b[%sm"
35+
StartSet = "\x1b["
36+
// ResetSet close all properties.
37+
ResetSet = "\x1b[0m"
38+
SettingTpl = "\x1b[%sm"
39+
// FullColorTpl for build color code
3540
FullColorTpl = "\x1b[%sm%s\x1b[0m"
3641
)
3742

38-
// ResetSet Close all properties.
39-
const ResetSet = "\x1b[0m"
40-
4143
// CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
4244
const CodeExpr = `\033\[[\d;?]+m`
4345

@@ -144,7 +146,7 @@ func ResetOptions() {
144146
output = os.Stdout
145147
}
146148

147-
// ForceColor force open color render
149+
// ForceSetColorLevel force open color render
148150
func ForceSetColorLevel(level terminfo.ColorLevel) terminfo.ColorLevel {
149151
oldLevelVal := colorLevel
150152
colorLevel = level
@@ -178,6 +180,7 @@ func InnerErrs() []error {
178180
*************************************************************/
179181

180182
// RenderCode render message by color code.
183+
//
181184
// Usage:
182185
// msg := RenderCode("3;32;45", "some", "message")
183186
func RenderCode(code string, args ...interface{}) string {
@@ -196,7 +199,8 @@ func RenderCode(code string, args ...interface{}) string {
196199
return ClearCode(message)
197200
}
198201

199-
return fmt.Sprintf(FullColorTpl, code, message)
202+
// return fmt.Sprintf(FullColorTpl, code, message)
203+
return StartSet + code + "m" + message + ResetSet
200204
}
201205

202206
// RenderWithSpaces Render code with spaces.
@@ -212,10 +216,12 @@ func RenderWithSpaces(code string, args ...interface{}) string {
212216
return ClearCode(message)
213217
}
214218

215-
return fmt.Sprintf(FullColorTpl, code, message)
219+
// return fmt.Sprintf(FullColorTpl, code, message)
220+
return StartSet + code + "m" + message + ResetSet
216221
}
217222

218223
// RenderString render a string with color code.
224+
//
219225
// Usage:
220226
// msg := RenderString("3;32;45", "a message")
221227
func RenderString(code string, str string) string {
@@ -228,10 +234,12 @@ func RenderString(code string, str string) string {
228234
return ClearCode(str)
229235
}
230236

231-
return fmt.Sprintf(FullColorTpl, code, str)
237+
// return fmt.Sprintf(FullColorTpl, code, str)
238+
return StartSet + code + "m" + str + ResetSet
232239
}
233240

234241
// ClearCode clear color codes.
242+
//
235243
// eg: "\033[36;1mText\x1b[0m" -> "Text"
236244
func ClearCode(str string) string {
237245
return codeRegex.ReplaceAllString(str, "")

color_16.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ const (
167167
)
168168

169169
// Bit4 an method for create Color
170-
func Bit4(code uint8) Color {
171-
return Color(code)
172-
}
170+
func Bit4(code uint8) Color { return Color(code) }
173171

174172
/*************************************************************
175173
* Color render methods
@@ -185,33 +183,27 @@ func (c Color) Name() string {
185183
}
186184

187185
// Text render a text message
188-
func (c Color) Text(message string) string {
189-
return RenderString(c.String(), message)
190-
}
186+
func (c Color) Text(message string) string { return RenderString(c.String(), message) }
191187

192188
// Render messages by color setting
193189
// Usage:
194190
// green := color.FgGreen.Render
195191
// fmt.Println(green("message"))
196-
func (c Color) Render(a ...interface{}) string {
197-
return RenderCode(c.String(), a...)
198-
}
192+
func (c Color) Render(a ...interface{}) string { return RenderCode(c.String(), a...) }
199193

200194
// Renderln messages by color setting.
201195
// like Println, will add spaces for each argument
196+
//
202197
// Usage:
203198
// green := color.FgGreen.Renderln
204199
// fmt.Println(green("message"))
205-
func (c Color) Renderln(a ...interface{}) string {
206-
return RenderWithSpaces(c.String(), a...)
207-
}
200+
func (c Color) Renderln(a ...interface{}) string { return RenderWithSpaces(c.String(), a...) }
208201

209202
// Sprint render messages by color setting. is alias of the Render()
210-
func (c Color) Sprint(a ...interface{}) string {
211-
return RenderCode(c.String(), a...)
212-
}
203+
func (c Color) Sprint(a ...interface{}) string { return RenderCode(c.String(), a...) }
213204

214205
// Sprintf format and render message.
206+
//
215207
// Usage:
216208
// green := color.Green.Sprintf
217209
// colored := green("message")
@@ -220,6 +212,7 @@ func (c Color) Sprintf(format string, args ...interface{}) string {
220212
}
221213

222214
// Print messages.
215+
//
223216
// Usage:
224217
// color.Green.Print("message")
225218
// OR:
@@ -230,16 +223,15 @@ func (c Color) Print(args ...interface{}) {
230223
}
231224

232225
// Printf format and print messages.
226+
//
233227
// Usage:
234228
// color.Cyan.Printf("string %s", "arg0")
235229
func (c Color) Printf(format string, a ...interface{}) {
236230
doPrintV2(c.Code(), fmt.Sprintf(format, a...))
237231
}
238232

239233
// Println messages with new line
240-
func (c Color) Println(a ...interface{}) {
241-
doPrintlnV2(c.String(), a)
242-
}
234+
func (c Color) Println(a ...interface{}) { doPrintlnV2(c.String(), a) }
243235

244236
// Light current color. eg: 36(FgCyan) -> 96(FgLightCyan).
245237
//
@@ -326,21 +318,13 @@ func (c Color) RGB() RGBColor {
326318
}
327319

328320
// Code convert to code string. eg "35"
329-
func (c Color) Code() string {
330-
// return fmt.Sprintf("%d", c)
331-
return strconv.Itoa(int(c))
332-
}
321+
func (c Color) Code() string { return strconv.Itoa(int(c)) }
333322

334323
// String convert to code string. eg "35"
335-
func (c Color) String() string {
336-
// return fmt.Sprintf("%d", c)
337-
return strconv.Itoa(int(c))
338-
}
324+
func (c Color) String() string { return strconv.Itoa(int(c)) }
339325

340326
// IsValid color value
341-
func (c Color) IsValid() bool {
342-
return c < 107
343-
}
327+
func (c Color) IsValid() bool { return c < 107 }
344328

345329
/*************************************************************
346330
* basic color maps

0 commit comments

Comments
 (0)