fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.15k stars 607 forks source link

using underline with a different fg color breaks it #206

Closed Yakiyo closed 8 months ago

Yakiyo commented 10 months ago

I'm trying to pretty print a markdown file. The file uses {{ some-text }} in it for indicating variables, im removing the {{/}} and underline the inner text, and i'm also making the entire line cyan. But it seems, cyan get's removed after the first occurrence of the underlined word. An example image: image

The part of the code is somewhat like this

var underline = color.New(color.Underline).Sprint

line = highLightVariable(line)
line = color.CyanString(line)
fmt.Println(line)

func highLightVariable(line string) string {
    lines := strings.Split(line, "}}")
    lines = lo.Map(lines, func(line string, _ int) string {
        l := strings.Split(line, "{{")
        if len(l) > 1 {
            l[1] = underline(l[1])
        }
        return strings.Join(l, "{{")
    })
    line = strings.Join(lines, "}}")
    line = strings.ReplaceAll(line, "{{", "")
    line = strings.ReplaceAll(line, "}}", "")
    return line
}

the cyan colors stops after the first underlined word, even tho consequent words to have underlines.

In case, the full code is needed, check this

gregpoirson commented 8 months ago

After the second format is applied (underline in your case), color appends a reset [0m. see this example: image and the corresponding raw text : [36mword1 word2 [4mword3[0m word4 [1mword5[0m[0m

color should be reappling any other format set before the underline.

gregpoirson commented 8 months ago

Maybe instead of applying a generic reset [0m, color should apply the specific reset for the specified format. for instance, to reset the underline [4m, color should use [24m. seems to me that the problem is in the unformat function (line 380)

gregpoirson commented 8 months ago

I will be submitting a branch 'issue_206' to resolve this issue.