Skip to content

Commit

Permalink
up: update some for build full color string
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 19, 2022
1 parent 85b9eb0 commit d5924d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
24 changes: 16 additions & 8 deletions color.go
Expand Up @@ -28,16 +28,18 @@ const (
)

// color render templates
//
// ESC 操作的表示:
// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
const (
SettingTpl = "\x1b[%sm"
StartSet = "\x1b["
// ResetSet close all properties.
ResetSet = "\x1b[0m"
SettingTpl = "\x1b[%sm"
// FullColorTpl for build color code
FullColorTpl = "\x1b[%sm%s\x1b[0m"
)

// ResetSet Close all properties.
const ResetSet = "\x1b[0m"

// CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
const CodeExpr = `\033\[[\d;?]+m`

Expand Down Expand Up @@ -144,7 +146,7 @@ func ResetOptions() {
output = os.Stdout
}

// ForceColor force open color render
// ForceSetColorLevel force open color render
func ForceSetColorLevel(level terminfo.ColorLevel) terminfo.ColorLevel {
oldLevelVal := colorLevel
colorLevel = level
Expand Down Expand Up @@ -178,6 +180,7 @@ func InnerErrs() []error {
*************************************************************/

// RenderCode render message by color code.
//
// Usage:
// msg := RenderCode("3;32;45", "some", "message")
func RenderCode(code string, args ...interface{}) string {
Expand All @@ -196,7 +199,8 @@ func RenderCode(code string, args ...interface{}) string {
return ClearCode(message)
}

return fmt.Sprintf(FullColorTpl, code, message)
// return fmt.Sprintf(FullColorTpl, code, message)
return StartSet + code + "m" + message + ResetSet
}

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

return fmt.Sprintf(FullColorTpl, code, message)
// return fmt.Sprintf(FullColorTpl, code, message)
return StartSet + code + "m" + message + ResetSet
}

// RenderString render a string with color code.
//
// Usage:
// msg := RenderString("3;32;45", "a message")
func RenderString(code string, str string) string {
Expand All @@ -228,10 +234,12 @@ func RenderString(code string, str string) string {
return ClearCode(str)
}

return fmt.Sprintf(FullColorTpl, code, str)
// return fmt.Sprintf(FullColorTpl, code, str)
return StartSet + code + "m" + str + ResetSet
}

// ClearCode clear color codes.
//
// eg: "\033[36;1mText\x1b[0m" -> "Text"
func ClearCode(str string) string {
return codeRegex.ReplaceAllString(str, "")
Expand Down
42 changes: 13 additions & 29 deletions color_16.go
Expand Up @@ -167,9 +167,7 @@ const (
)

// Bit4 an method for create Color
func Bit4(code uint8) Color {
return Color(code)
}
func Bit4(code uint8) Color { return Color(code) }

/*************************************************************
* Color render methods
Expand All @@ -185,33 +183,27 @@ func (c Color) Name() string {
}

// Text render a text message
func (c Color) Text(message string) string {
return RenderString(c.String(), message)
}
func (c Color) Text(message string) string { return RenderString(c.String(), message) }

// Render messages by color setting
// Usage:
// green := color.FgGreen.Render
// fmt.Println(green("message"))
func (c Color) Render(a ...interface{}) string {
return RenderCode(c.String(), a...)
}
func (c Color) Render(a ...interface{}) string { return RenderCode(c.String(), a...) }

// Renderln messages by color setting.
// like Println, will add spaces for each argument
//
// Usage:
// green := color.FgGreen.Renderln
// fmt.Println(green("message"))
func (c Color) Renderln(a ...interface{}) string {
return RenderWithSpaces(c.String(), a...)
}
func (c Color) Renderln(a ...interface{}) string { return RenderWithSpaces(c.String(), a...) }

// Sprint render messages by color setting. is alias of the Render()
func (c Color) Sprint(a ...interface{}) string {
return RenderCode(c.String(), a...)
}
func (c Color) Sprint(a ...interface{}) string { return RenderCode(c.String(), a...) }

// Sprintf format and render message.
//
// Usage:
// green := color.Green.Sprintf
// colored := green("message")
Expand All @@ -220,6 +212,7 @@ func (c Color) Sprintf(format string, args ...interface{}) string {
}

// Print messages.
//
// Usage:
// color.Green.Print("message")
// OR:
Expand All @@ -230,16 +223,15 @@ func (c Color) Print(args ...interface{}) {
}

// Printf format and print messages.
//
// Usage:
// color.Cyan.Printf("string %s", "arg0")
func (c Color) Printf(format string, a ...interface{}) {
doPrintV2(c.Code(), fmt.Sprintf(format, a...))
}

// Println messages with new line
func (c Color) Println(a ...interface{}) {
doPrintlnV2(c.String(), a)
}
func (c Color) Println(a ...interface{}) { doPrintlnV2(c.String(), a) }

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

// Code convert to code string. eg "35"
func (c Color) Code() string {
// return fmt.Sprintf("%d", c)
return strconv.Itoa(int(c))
}
func (c Color) Code() string { return strconv.Itoa(int(c)) }

// String convert to code string. eg "35"
func (c Color) String() string {
// return fmt.Sprintf("%d", c)
return strconv.Itoa(int(c))
}
func (c Color) String() string { return strconv.Itoa(int(c)) }

// IsValid color value
func (c Color) IsValid() bool {
return c < 107
}
func (c Color) IsValid() bool { return c < 107 }

/*************************************************************
* basic color maps
Expand Down

0 comments on commit d5924d1

Please sign in to comment.