Skip to content

Commit

Permalink
fixes #206
Browse files Browse the repository at this point in the history
  • Loading branch information
gregpoirson committed Oct 13, 2023
1 parent d5c210c commit c33f29c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
37 changes: 36 additions & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ const (
CrossedOut
)

const (
ResetBoldFaint Attribute = iota + 22
ResetItalic
ResetUnderline
ResetBlinkSlow
ResetBlinkRapid
ResetReverseVideo
ResetConcealed
ResetCrossedOut
)

var mapResetAttributes map[Attribute]Attribute = map[Attribute]Attribute{
Bold: ResetBoldFaint,
Faint: ResetBoldFaint,
Italic: ResetItalic,
Underline: ResetUnderline,
BlinkSlow: ResetBlinkSlow,
BlinkRapid: ResetBlinkRapid,
ReverseVideo: ResetReverseVideo,
Concealed: ResetConcealed,
CrossedOut: ResetCrossedOut,
}

// Foreground text colors
const (
FgBlack Attribute = iota + 30
Expand Down Expand Up @@ -377,7 +400,19 @@ 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 {
ra, ok := mapResetAttributes[v]
if !ok {
format[i] = strconv.Itoa(int(Reset))
} else {
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
16 changes: 16 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,19 @@ func readRaw(t *testing.T, r io.Reader) string {

return string(out)
}

func TestResetSecondFormat(t *testing.T) {

var underline = New(Underline).Sprint

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

line = CyanString(line)

var result = fmt.Sprintf("%v", line)
const expectedResult = "word1 word2 word3 word4"

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

0 comments on commit c33f29c

Please sign in to comment.