From c33f29cd0eb1e67ca270ea3c0fa8f81265fb8231 Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Fri, 13 Oct 2023 19:30:42 -0300 Subject: [PATCH 1/8] 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) + } +} From c949fee4aa0c0d9f0e37851a06a797cd4973cd58 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Oct 2023 20:59:19 -0300 Subject: [PATCH 2/8] Update color.go Update color.go, following Fatih's suggestion Co-authored-by: Fatih Arslan --- color.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/color.go b/color.go index 332556d..0435b3a 100644 --- a/color.go +++ b/color.go @@ -404,10 +404,9 @@ func (c *Color) unformat() string { //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(Reset)) - } else { + if ok { format[i] = strconv.Itoa(int(ra)) } } From c211ac1e439c3688103d19f846fa80a96bda50ba Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Mon, 16 Oct 2023 21:11:05 -0300 Subject: [PATCH 3/8] renaming the Reset names --- color.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/color.go b/color.go index 0435b3a..c423428 100644 --- a/color.go +++ b/color.go @@ -66,24 +66,24 @@ const ( ) const ( - ResetBoldFaint Attribute = iota + 22 + ResetBold Attribute = iota + 22 ResetItalic ResetUnderline - ResetBlinkSlow - ResetBlinkRapid - ResetReverseVideo + ResetBlinking + _ + ResetReversed ResetConcealed ResetCrossedOut ) var mapResetAttributes map[Attribute]Attribute = map[Attribute]Attribute{ - Bold: ResetBoldFaint, - Faint: ResetBoldFaint, + Bold: ResetBold, + Faint: ResetBold, Italic: ResetItalic, Underline: ResetUnderline, - BlinkSlow: ResetBlinkSlow, - BlinkRapid: ResetBlinkRapid, - ReverseVideo: ResetReverseVideo, + BlinkSlow: ResetBlinking, + BlinkRapid: ResetBlinking, + ReverseVideo: ResetReversed, Concealed: ResetConcealed, CrossedOut: ResetCrossedOut, } From 641fc85af5c56261bc87799359c6e212ca41eb6e Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Mon, 16 Oct 2023 21:27:30 -0300 Subject: [PATCH 4/8] fixes #206 --- color_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/color_test.go b/color_test.go index 1c33065..0725c46 100644 --- a/color_test.go +++ b/color_test.go @@ -470,7 +470,7 @@ func readRaw(t *testing.T, r io.Reader) string { return string(out) } -func TestResetSecondFormat(t *testing.T) { +func TestIssue206_1(t *testing.T) { var underline = New(Underline).Sprint @@ -485,3 +485,20 @@ func TestResetSecondFormat(t *testing.T) { 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 = "underlined regular green bold red" + + if !bytes.Equal([]byte(result), []byte(expectedResult)) { + t.Errorf("Expecting %v, got '%v'\n", expectedResult, result) + } +} From db0d19c32448352ee3c231e9938e9aa5eff7c43e Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Tue, 17 Oct 2023 21:25:24 -0300 Subject: [PATCH 5/8] escape sequences instead of unicode ctrl char #206 --- color_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/color_test.go b/color_test.go index 0725c46..f29a787 100644 --- a/color_test.go +++ b/color_test.go @@ -472,14 +472,18 @@ func readRaw(t *testing.T, r io.Reader) string { 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 = "word1 word2 word3 word4" + 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) @@ -496,7 +500,7 @@ func TestIssue206_2(t *testing.T) { fmt.Println(line) var result = fmt.Sprintf("%v", line) - const expectedResult = "underlined regular green bold red" + 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) From 9f44e28c018ac5f97fcbaba7ae456cd7e0db3d0e Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 17 Oct 2023 21:42:34 -0300 Subject: [PATCH 6/8] Create manual.yml manual build workflow --- .github/workflows/manual.yml | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .github/workflows/manual.yml diff --git a/.github/workflows/manual.yml b/.github/workflows/manual.yml new file mode 100644 index 0000000..64dc44d --- /dev/null +++ b/.github/workflows/manual.yml @@ -0,0 +1,44 @@ +name: manual build +on: + workflow_dispatch: +jobs: + test: + name: Test & Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '>=1.20.0' + + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Run go mod tidy + run: | + set -e + go mod tidy + output=$(git status -s) + if [ -z "${output}" ]; then + exit 0 + fi + echo 'We wish to maintain a tidy state for go mod. Please run `go mod tidy` on your branch, commit and push again.' + echo 'Running `go mod tidy` on this CI test yields with the following changes:' + echo "$output" + exit 1 + + - name: Test + run: go test -race ./... + + - name: Lint + run: "go vet ./..." + + - name: Staticcheck + uses: dominikh/staticcheck-action@v1.3.0 + with: + version: "2023.1.2" + install-go: false + + - name: Build + run: go build ./... From b756d513efde0a21fe104812b81d342aa2c2ae0b Mon Sep 17 00:00:00 2001 From: Greg Poirson Date: Tue, 17 Oct 2023 21:52:56 -0300 Subject: [PATCH 7/8] deleting manual workflow --- .github/workflows/manual.yml | 44 ------------------------------------ 1 file changed, 44 deletions(-) delete mode 100644 .github/workflows/manual.yml diff --git a/.github/workflows/manual.yml b/.github/workflows/manual.yml deleted file mode 100644 index 64dc44d..0000000 --- a/.github/workflows/manual.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: manual build -on: - workflow_dispatch: -jobs: - test: - name: Test & Build - runs-on: ubuntu-latest - steps: - - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: '>=1.20.0' - - - name: Check out code into the Go module directory - uses: actions/checkout@v3 - - - name: Run go mod tidy - run: | - set -e - go mod tidy - output=$(git status -s) - if [ -z "${output}" ]; then - exit 0 - fi - echo 'We wish to maintain a tidy state for go mod. Please run `go mod tidy` on your branch, commit and push again.' - echo 'Running `go mod tidy` on this CI test yields with the following changes:' - echo "$output" - exit 1 - - - name: Test - run: go test -race ./... - - - name: Lint - run: "go vet ./..." - - - name: Staticcheck - uses: dominikh/staticcheck-action@v1.3.0 - with: - version: "2023.1.2" - install-go: false - - - name: Build - run: go build ./... From b5f9b4c5605ec36de8f31b9a1b4d777d38b817a4 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 18 Oct 2023 10:07:36 +0300 Subject: [PATCH 8/8] Apply suggestions from code review --- color_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/color_test.go b/color_test.go index f29a787..a8cc887 100644 --- a/color_test.go +++ b/color_test.go @@ -471,7 +471,6 @@ func readRaw(t *testing.T, r io.Reader) string { } 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 @@ -491,7 +490,6 @@ func TestIssue206_1(t *testing.T) { } func TestIssue206_2(t *testing.T) { - var underline = New(Underline).Sprint var bold = New(Bold).Sprint