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

using underline with a different fg color breaks it #206

Closed
Yakiyo opened this issue Aug 28, 2023 · 3 comments
Closed

using underline with a different fg color breaks it #206

Yakiyo opened this issue Aug 28, 2023 · 3 comments

Comments

@Yakiyo
Copy link

Yakiyo commented Aug 28, 2023

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
Copy link
Contributor

gregpoirson commented Oct 13, 2023

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
Copy link
Contributor

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
Copy link
Contributor

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

@gregpoirson gregpoirson mentioned this issue Oct 13, 2023
@fatih fatih closed this as completed in c33f29c Oct 18, 2023
fatih added a commit that referenced this issue Oct 18, 2023
mikelorant added a commit to mikelorant/coloredcobra that referenced this issue Apr 13, 2024
When upgrading the Color package from `v1.15.0` to `v1.16.0`, the list
of available commands becomes incorrectly aligned:

```text
Available Commands:

  completion Generate the autocompletion script for the specified shell
  help      Help about any command
  test      Test command for clarity
```

This was related to an [issue][1] where reset codes were not correctly
set. The [fix][2], which added extra reset codes, increased the length
of the string (which includes the ANSI codes) causing the amount of
padding to be insufficient.

This change, which upgrades the Color package to `v1.16.0`, also
increass the padding to account for the additional ANSI reset codes
that are now part of the string. This restores the text alignment to
how it was previously displayed.

Unit tests also needed to be updated to account for this change and are
passing successfully.

The Color package had updated its dependencies which has caused the
changes in `go.mod` to impact more than itself.

Fixes: ivanpirog#4

[1]: fatih/color#206
[2]: fatih/color#210

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
daylinmorgan pushed a commit to daylinmorgan/coloredcobra that referenced this issue May 27, 2024
When upgrading the Color package from `v1.15.0` to `v1.16.0`, the list
of available commands becomes incorrectly aligned:

```text
Available Commands:

  completion Generate the autocompletion script for the specified shell
  help      Help about any command
  test      Test command for clarity
```

This was related to an [issue][1] where reset codes were not correctly
set. The [fix][2], which added extra reset codes, increased the length
of the string (which includes the ANSI codes) causing the amount of
padding to be insufficient.

This change, which upgrades the Color package to `v1.16.0`, also
increass the padding to account for the additional ANSI reset codes
that are now part of the string. This restores the text alignment to
how it was previously displayed.

Unit tests also needed to be updated to account for this change and are
passing successfully.

The Color package had updated its dependencies which has caused the
changes in `go.mod` to impact more than itself.

Fixes: ivanpirog#4

[1]: fatih/color#206
[2]: fatih/color#210

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
Signed-off-by: Daylin Morgan <me@dayl.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants