Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/github.com/mattn/go-is…
Browse files Browse the repository at this point in the history
…atty-0.0.20
  • Loading branch information
fatih authored Oct 18, 2023
2 parents 3c00bfd + 1c78ded commit f3e45da
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
36 changes: 35 additions & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ const (
CrossedOut
)

const (
ResetBold Attribute = iota + 22
ResetItalic
ResetUnderline
ResetBlinking
_
ResetReversed
ResetConcealed
ResetCrossedOut
)

var mapResetAttributes map[Attribute]Attribute = map[Attribute]Attribute{
Bold: ResetBold,
Faint: ResetBold,
Italic: ResetItalic,
Underline: ResetUnderline,
BlinkSlow: ResetBlinking,
BlinkRapid: ResetBlinking,
ReverseVideo: ResetReversed,
Concealed: ResetConcealed,
CrossedOut: ResetCrossedOut,
}

// Foreground text colors
const (
FgBlack Attribute = iota + 30
Expand Down Expand Up @@ -377,7 +400,18 @@ func (c *Color) format() string {
}

func (c *Color) unformat() string {
return fmt.Sprintf("%s[%dm", escape, Reset)
//return fmt.Sprintf("%s[%dm", escape, Reset)
//for each element in sequence let's use the speficic reset escape, ou the generic one if not found
format := make([]string, len(c.params))
for i, v := range c.params {
format[i] = strconv.Itoa(int(Reset))
ra, ok := mapResetAttributes[v]
if ok {
format[i] = strconv.Itoa(int(ra))
}
}

return fmt.Sprintf("%s[%sm", escape, strings.Join(format, ";"))
}

// DisableColor disables the color output. Useful to not change any existing
Expand Down
35 changes: 35 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,38 @@ func readRaw(t *testing.T, r io.Reader) string {

return string(out)
}

func TestIssue206_1(t *testing.T) {
//visual test, go test -v .
//to see the string with escape codes, use go test -v . > c:\temp\test.txt
var underline = New(Underline).Sprint

var line = fmt.Sprintf("%s %s %s %s", "word1", underline("word2"), "word3", underline("word4"))

line = CyanString(line)

fmt.Println(line)

var result = fmt.Sprintf("%v", line)
const expectedResult = "\x1b[36mword1 \x1b[4mword2\x1b[24m word3 \x1b[4mword4\x1b[24m\x1b[0m"

if !bytes.Equal([]byte(result), []byte(expectedResult)) {
t.Errorf("Expecting %v, got '%v'\n", expectedResult, result)
}
}

func TestIssue206_2(t *testing.T) {
var underline = New(Underline).Sprint
var bold = New(Bold).Sprint

var line = fmt.Sprintf("%s %s", GreenString(underline("underlined regular green")), RedString(bold("bold red")))

fmt.Println(line)

var result = fmt.Sprintf("%v", line)
const expectedResult = "\x1b[32m\x1b[4munderlined regular green\x1b[24m\x1b[0m \x1b[31m\x1b[1mbold red\x1b[22m\x1b[0m"

if !bytes.Equal([]byte(result), []byte(expectedResult)) {
t.Errorf("Expecting %v, got '%v'\n", expectedResult, result)
}
}

0 comments on commit f3e45da

Please sign in to comment.