From c33f29cd0eb1e67ca270ea3c0fa8f81265fb8231 Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Fri, 13 Oct 2023 19:30:42 -0300 Subject: [PATCH] fixes #206 --- color.go | 37 ++++++++++++++++++++++++++++++++++++- color_test.go | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/color.go b/color.go index 4d8b711..332556d 100644 --- a/color.go +++ b/color.go @@ -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 @@ -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 diff --git a/color_test.go b/color_test.go index ce80232..1c33065 100644 --- a/color_test.go +++ b/color_test.go @@ -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) + } +}