[GH-ISSUE #456] utils.go: Can't output normally if "[xxxx]" in string #325

Closed
opened 2026-03-04 01:04:01 +03:00 by kerem · 6 comments
Owner

Originally created by @Chen-Haohuang on GitHub (Jun 5, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/456

when I output a string into textview, I find it can't output normally if if "[xxxx]" in the string.
e.g.

output = tview.NewTextView()
fmt.Fprintf(output, "%s", "before[unknown]after")

I want to show "before[unknown]after" in output textview, but it only leaves "before".
I know maybe I can use "before[unknown[]after", but I can't know what the string contains before I output.

I have found where the bug is.
In function:utils.go:decomposeString

// Because the color pattern detects empty tags, we need to filter them out.
	for i := len(colorIndices) - 1; i >= 0; i-- {
		if colorIndices[i][1]-colorIndices[i][0] == 2 {
			colorIndices = append(colorIndices[:i], colorIndices[i+1:]...)
			colors = append(colors[:i], colors[i+1:]...)
		}
	}

We also need to filter it out if the tag is not a color tag. So I modified the function.

// Because the color pattern detects empty tags, we need to filter them out.
	for i := len(colorIndices) - 1; i >= 0; i-- {
		fgColor := colors[i][colorForegroundPos]
		bgColor := colors[i][colorBackgroundPos]
		if colorIndices[i][1]-colorIndices[i][0] == 2  || (fgColor != "" && fgColor != "-" && tcell.GetColor(fgColor) == -1) || (bgColor != "" && bgColor != "-" && tcell.GetColor(bgColor) == -1) {
			colorIndices = append(colorIndices[:i], colorIndices[i+1:]...)
			colors = append(colors[:i], colors[i+1:]...)
		}
	}
Originally created by @Chen-Haohuang on GitHub (Jun 5, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/456 when I output a string into textview, I find it can't output normally if if "[xxxx]" in the string. e.g. ```go output = tview.NewTextView() fmt.Fprintf(output, "%s", "before[unknown]after") ``` I want to show "before[unknown]after" in output textview, but it only leaves "before". I know maybe I can use "before[unknown[]after", but I can't know what the string contains before I output. I have found where the bug is. In function:utils.go:decomposeString ```go // Because the color pattern detects empty tags, we need to filter them out. for i := len(colorIndices) - 1; i >= 0; i-- { if colorIndices[i][1]-colorIndices[i][0] == 2 { colorIndices = append(colorIndices[:i], colorIndices[i+1:]...) colors = append(colors[:i], colors[i+1:]...) } } ``` We also need to filter it out if the tag is not a color tag. So I modified the function. ```go // Because the color pattern detects empty tags, we need to filter them out. for i := len(colorIndices) - 1; i >= 0; i-- { fgColor := colors[i][colorForegroundPos] bgColor := colors[i][colorBackgroundPos] if colorIndices[i][1]-colorIndices[i][0] == 2 || (fgColor != "" && fgColor != "-" && tcell.GetColor(fgColor) == -1) || (bgColor != "" && bgColor != "-" && tcell.GetColor(bgColor) == -1) { colorIndices = append(colorIndices[:i], colorIndices[i+1:]...) colors = append(colors[:i], colors[i+1:]...) } } ```
kerem closed this issue 2026-03-04 01:04:02 +03:00
Author
Owner

@abitrolly commented on GitHub (Jun 5, 2020):

Or just use Escape() function?

https://pkg.go.dev/github.com/rivo/tview?tab=doc#hdr-Colors

<!-- gh-comment-id:639424727 --> @abitrolly commented on GitHub (Jun 5, 2020): Or just use `Escape()` function? https://pkg.go.dev/github.com/rivo/tview?tab=doc#hdr-Colors
Author
Owner

@rivo commented on GitHub (Jun 10, 2020):

Checking for colour names will only work until somebody wants to output "[red]" without escaping it. It's not a good solution in my opinion.

As @abitrolly mentioned, there is an Escape() function. Or simply call textView.SetDynamicColors(false) to ignore any colour tags.

<!-- gh-comment-id:641879939 --> @rivo commented on GitHub (Jun 10, 2020): Checking for colour names will only work until somebody wants to output "[red]" without escaping it. It's not a good solution in my opinion. As @abitrolly mentioned, there is an [`Escape()`](https://pkg.go.dev/github.com/rivo/tview?tab=doc#Escape) function. Or simply call [`textView.SetDynamicColors(false)`](https://pkg.go.dev/github.com/rivo/tview?tab=doc#TextView.SetDynamicColors) to ignore any colour tags.
Author
Owner

@ghostsquad commented on GitHub (Jun 17, 2020):

I'm a bit confused here. If someone wants to output the literal value of [red], would you use Escape() first? then inject/wrap with color?

<!-- gh-comment-id:645108270 --> @ghostsquad commented on GitHub (Jun 17, 2020): I'm a bit confused here. If someone wants to output the literal value of `[red]`, would you use `Escape()` first? then inject/wrap with color?
Author
Owner

@ghostsquad commented on GitHub (Jun 17, 2020):

Also, what happens if [red] shows up in the text along with ANSI Escape Codes? Would you first Escape(), then use TranslateANSI() ?

<!-- gh-comment-id:645108610 --> @ghostsquad commented on GitHub (Jun 17, 2020): Also, what happens if `[red]` shows up in the text along with ANSI Escape Codes? Would you first `Escape()`, then use `TranslateANSI()` ?
Author
Owner

@rivo commented on GitHub (Jun 28, 2020):

If someone wants to output the literal value of [red], would you use Escape() first? then inject/wrap with color?

I guess that's what I would do.

Also, what happens if [red] shows up in the text along with ANSI Escape Codes? Would you first Escape(), then use TranslateANSI() ?

TranslateANSI() should do the escaping for you. There should be no need to call Escape() first if you're translating ANSI escape sequences. If that doesn't work, it's a bug and I have to fix it. (Please let me know if that's the case.)

<!-- gh-comment-id:650713465 --> @rivo commented on GitHub (Jun 28, 2020): > If someone wants to output the literal value of [red], would you use Escape() first? then inject/wrap with color? I guess that's what I would do. > Also, what happens if `[red]` shows up in the text along with ANSI Escape Codes? Would you first `Escape()`, then use `TranslateANSI()` ? `TranslateANSI()` should do the escaping for you. There should be no need to call `Escape()` first if you're translating ANSI escape sequences. If that doesn't work, it's a bug and I have to fix it. (Please let me know if that's the case.)
Author
Owner

@rivo commented on GitHub (Jul 12, 2020):

Please let me know if any more help is needed. If not, I'll close the issue soon.

<!-- gh-comment-id:657206993 --> @rivo commented on GitHub (Jul 12, 2020): Please let me know if any more help is needed. If not, I'll close the issue soon.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#325
No description provided.