Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #206 #210

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
gregpoirson marked this conversation as resolved.
Show resolved Hide resolved
)

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
gregpoirson marked this conversation as resolved.
Show resolved Hide resolved
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
37 changes: 37 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,40 @@ func readRaw(t *testing.T, r io.Reader) string {

return string(out)
}

func TestIssue206_1(t *testing.T) {

fatih marked this conversation as resolved.
Show resolved Hide resolved
//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) {

fatih marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}