Skip to content

Commit

Permalink
up: add Set and Reset method to Color256 and RgbColor
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 8, 2021
1 parent 3f8c668 commit a0e9078
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
23 changes: 5 additions & 18 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,15 @@ func SupportTrueColor() bool {

// Set set console color attributes
func Set(colors ...Color) (int, error) {
if !Enable { // not enable
return 0, nil
}

if !SupportColor() {
return 0, nil
}

return fmt.Printf(SettingTpl, Colors2code(colors...))
code := Colors2code(colors...)
err := SetTerminal(code)
return 0, err
}

// Reset reset console color attributes
func Reset() (int, error) {
if !Enable { // not enable
return 0, nil
}

if !SupportColor() {
return 0, nil
}

return fmt.Print(ResetSet)
err := ResetTerminal()
return 0, err
}

// Disable disable color output
Expand Down
12 changes: 11 additions & 1 deletion color_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func C256(val uint8, isBg ...bool) Color256 {
return bc
}

// Set terminal by 256 color code
func (c Color256) Set() error {
return SetTerminal(c.String())
}

// Reset terminal. alias of the ResetTerminal()
func (c Color256) Reset() error {
return ResetTerminal()
}

// Print print message
func (c Color256) Print(a ...interface{}) {
doPrintV2(c.String(), fmt.Sprint(a...))
Expand Down Expand Up @@ -108,7 +118,7 @@ func (c Color256) Value() uint8 {
return c[0]
}

// String convert to color code string.
// String convert to color code string. eg: "38;5;12"
func (c Color256) String() string {
if c[1] == AsFg { // 0 is Fg
return fmt.Sprintf(TplFg256, c[0])
Expand Down
16 changes: 13 additions & 3 deletions color_rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func RGBFromString(rgb string, isBg ...bool) RGBColor {
return RGB(uint8(ar[0]), uint8(ar[1]), uint8(ar[2]), isBg...)
}

// Set terminal by rgb/true color code
func (c RGBColor) Set() error {
return SetTerminal(c.String())
}

// Reset terminal. alias of the ResetTerminal()
func (c RGBColor) Reset() error {
return ResetTerminal()
}

// Print print message
func (c RGBColor) Print(a ...interface{}) {
doPrintV2(c.String(), fmt.Sprint(a...))
Expand Down Expand Up @@ -177,13 +187,13 @@ func (c RGBColor) Code() string {
return c.String()
}

// Hex color rgb to hex string. as in #ff0080.
// Hex color rgb to hex string. as in "ff0080".
func (c RGBColor) Hex() string {
// Add 0.5 for rounding
return fmt.Sprintf("#%02x%02x%02x", c[0], c[1], c[2])
return fmt.Sprintf("%02x%02x%02x", c[0], c[1], c[2])
}

// String to color code string
// String to color code string. eg: "38;2;204;123;56"
func (c RGBColor) String() string {
if c[3] == AsFg {
return fmt.Sprintf(TplFgRGB, c[0], c[1], c[2])
Expand Down
6 changes: 3 additions & 3 deletions color_rgb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestRGBColor(t *testing.T) {
str = c.Sprintf("msg")
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
is.Equal("[204 204 204]", fmt.Sprint(c.Values()))
is.Equal("#cccccc", c.Hex())
is.Equal("cccccc", c.Hex())

// RGBColor.Print
c.Print("msg")
Expand Down Expand Up @@ -86,11 +86,11 @@ func TestHexToRGB(t *testing.T) {
c := HEX("ccc") // rgb: [204 204 204]
is.False(c.IsEmpty())
is.Equal("38;2;204;204;204", c.String())
is.Equal("#cccccc", c.Hex())
is.Equal("cccccc", c.Hex())

c = HEX("aabbcc") // rgb: [170 187 204]
is.Equal("38;2;170;187;204", c.String())
is.Equal("#aabbcc", c.Hex())
is.Equal("aabbcc", c.Hex())

c = Hex("#aabbcc") // rgb: [170 187 204]
is.Equal("38;2;170;187;204", c.String())
Expand Down
1 change: 0 additions & 1 deletion detect_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func detectColorLevelFromEnv(termVal string, isWin bool) (terminfo.ColorLevel, e
if ok && v >= 256 {
return terminfo.ColorLevelHundreds, nil
}

return fallbackCheckTermValue(termVal)
}

Expand Down
20 changes: 20 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"strings"
)

// SetTerminal by given code.
func SetTerminal(code string) error {
if !Enable || !SupportColor() {
return nil
}

_,err := fmt.Fprintf(output, SettingTpl, code)
return err
}

// ResetTerminal terminal setting.
func ResetTerminal() error {
if !Enable || !SupportColor() {
return nil
}

_,err := fmt.Fprint(output, ResetSet)
return err
}

/*************************************************************
* print methods(will auto parse color tags)
*************************************************************/
Expand Down

0 comments on commit a0e9078

Please sign in to comment.