[GH-ISSUE #782] [QUESTION] About automatically coloring specific word patterns in TextView #573

Closed
opened 2026-03-04 01:06:08 +03:00 by kerem · 3 comments
Owner

Originally created by @AmrGanz on GitHub (Dec 17, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/782

Hello Rivo,

I usually use the same method as in here to color specific words. For example, making the word "error" shown in red and the word "success" shown in green.

I was wondering, is there a way to make it happen without explicitly reformat each instance of these words? Maybe to give an example, when I use a terminal like MobaXterm, it has this functionality of coloring specific words with a unique color automatically.

If this is can be already achieved, then I would really appreciate you pointing me to the document covering it and I will read more about it.

Kind regards

Originally created by @AmrGanz on GitHub (Dec 17, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/782 Hello Rivo, I usually use the same method as in [here](https://github.com/rivo/tview/blob/master/demos/textview/main.go) to color specific words. For example, making the word "error" shown in red and the word "success" shown in green. I was wondering, is there a way to make it happen without explicitly reformat each instance of these words? Maybe to give an example, when I use a terminal like MobaXterm, it has this functionality of coloring specific words with a unique color automatically. If this is can be already achieved, then I would really appreciate you pointing me to the document covering it and I will read more about it. Kind regards
kerem closed this issue 2026-03-04 01:06:08 +03:00
Author
Owner

@rivo commented on GitHub (Dec 21, 2022):

Every colour tag introduces a new colour for the following characters, until the next colour tag. So, no, there is no way to automatically colour specific words. But I think it should be fairly straightforward to come up with a regular expression to wrap specific words with colour tags.

<!-- gh-comment-id:1361615512 --> @rivo commented on GitHub (Dec 21, 2022): Every colour tag introduces a new colour for the following characters, until the next colour tag. So, no, there is no way to automatically colour specific words. But I think it should be fairly straightforward to come up with a regular expression to wrap specific words with colour tags.
Author
Owner

@AmrGanz commented on GitHub (Dec 27, 2022):

Thanks @digitallyserviced , I didn't know about it before and I have started testing it and hopefully it will get me the results I am seeking.

@rivo @digitallyserviced BTW, the reason I am asking this question is because when I use a TextView (while enabling dynamic coloring) and I wanted to format the shown text, lets say by column-formatting the output. The added color tags mess it up.

For example:

This code block will work perfectly fine and will show the desired column-formatted output:

package main

import (
	"github.com/rivo/tview"
	"github.com/ryanuber/columnize"
)

func main() {

	data := []string{"field1" + "|" + "fiel2"}
	data = append(data, "value1"+"|"+"value2")

	app := tview.NewApplication()
	textView := tview.NewTextView().
		SetChangedFunc(func() {
			app.Draw()
		}).SetDynamicColors(true)
	textView.SetBorder(false)
	textView.SetWrap(true)
	FormatedOutput := columnize.SimpleFormat(data)
	textView.SetText(FormatedOutput)

	app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false)
	app.Run()
}

output:

field1  fiel2                                                                                                                                                                                 
value1  value2

Now, if I added color tags like the below example, it will mess up the output formatting:

package main

import (
	"github.com/rivo/tview"
	"github.com/ryanuber/columnize"
)

func main() {
	Yellow := "[#FFFF00]"
	White := "[#FFFFFF]"

	data := []string{"field1" + "|" + "fiel2"}
	data = append(data, Yellow+"value1"+White+"|"+"value2")

	app := tview.NewApplication()
	textView := tview.NewTextView().
		SetChangedFunc(func() {
			app.Draw()
		}).SetDynamicColors(true)
	textView.SetBorder(false)
	textView.SetWrap(true)
	FormatedOutput := columnize.SimpleFormat(data)
	textView.SetText(FormatedOutput)

	app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false)
	app.Run()
}

output (coloring will work fine in the terminal):

field1                    fiel2                                                                                                                                                               
value1  value2

I understand that this is working as designed since the Columnize see the colors tag as text and working as expected. And this is why I am looking for other ways to natively color specific words/patterns instead.

<!-- gh-comment-id:1365648747 --> @AmrGanz commented on GitHub (Dec 27, 2022): Thanks @digitallyserviced , I didn't know about it before and I have started testing it and hopefully it will get me the results I am seeking. @rivo @digitallyserviced BTW, the reason I am asking this question is because when I use a `TextView` (while enabling dynamic coloring) and I wanted to format the shown text, lets say by [column-formatting](github.com/ryanuber/columnize) the output. The added color tags mess it up. For example: This code block will work perfectly fine and will show the desired column-formatted output: ``` package main import ( "github.com/rivo/tview" "github.com/ryanuber/columnize" ) func main() { data := []string{"field1" + "|" + "fiel2"} data = append(data, "value1"+"|"+"value2") app := tview.NewApplication() textView := tview.NewTextView(). SetChangedFunc(func() { app.Draw() }).SetDynamicColors(true) textView.SetBorder(false) textView.SetWrap(true) FormatedOutput := columnize.SimpleFormat(data) textView.SetText(FormatedOutput) app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false) app.Run() } ``` output: ``` field1 fiel2 value1 value2 ``` Now, if I added color tags like the below example, it will mess up the output formatting: ``` package main import ( "github.com/rivo/tview" "github.com/ryanuber/columnize" ) func main() { Yellow := "[#FFFF00]" White := "[#FFFFFF]" data := []string{"field1" + "|" + "fiel2"} data = append(data, Yellow+"value1"+White+"|"+"value2") app := tview.NewApplication() textView := tview.NewTextView(). SetChangedFunc(func() { app.Draw() }).SetDynamicColors(true) textView.SetBorder(false) textView.SetWrap(true) FormatedOutput := columnize.SimpleFormat(data) textView.SetText(FormatedOutput) app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false) app.Run() } ``` output (coloring will work fine in the terminal): ``` field1 fiel2 value1 value2 ``` I understand that this is working as designed since the `Columnize` see the colors tag as text and working as expected. And this is why I am looking for other ways to natively color specific words/patterns instead.
Author
Owner

@rivo commented on GitHub (Aug 26, 2023):

@AmrGanz I think you'll want to add color tags at the end of your text processing chain, i.e. after using columnize. You might be able to insert them at specific locations in the string or use regular expressions to insert them.

Finally, to answer your initial question, no, there is no way to apply colours to specific word patterns. This kind of behaviour is meant to be implemented outside of tview.

I'm closing this now as I don't think there is anything to do for me here. Feel free to respond or open a new issue if necessary.

<!-- gh-comment-id:1694357406 --> @rivo commented on GitHub (Aug 26, 2023): @AmrGanz I think you'll want to add color tags at the end of your text processing chain, i.e. after using `columnize`. You might be able to insert them at specific locations in the string or use regular expressions to insert them. Finally, to answer your initial question, no, there is no way to apply colours to specific word patterns. This kind of behaviour is meant to be implemented outside of `tview`. I'm closing this now as I don't think there is anything to do for me here. Feel free to respond or open a new issue if necessary.
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#573
No description provided.