[GH-ISSUE #91] feature request: add background color support for textview #70

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

Originally created by @stephencheng on GitHub (Mar 29, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/91

I found no implementation of supporting background color in textview.

eg, you have the decorated text color like below, it is foreground for font but not the background color.

[green]import[white] (
[red]"github.com/rivo/tview"[white]
)

I was using termui, basically it uses similar way to render color, but it uses below format to support both bg and fg color

fmt.Sprintf("%s", string(s), bg)
fmt.Sprintf("%s", string(s), fg)

I wonder why this is not supported and could it be added?

The reason I use it is to render a customised implementation of my own table, similar to your's table with colored cell.

I'd really like the feature to be added if it's possible so that I could migrate my code to use textview to simple render already made table

Thanks

Originally created by @stephencheng on GitHub (Mar 29, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/91 I found no implementation of supporting background color in textview. eg, you have the decorated text color like below, it is foreground for font but not the background color. > [green]import[white] ( > [red]"github.com/rivo/tview"[white] > ) > I was using termui, basically it uses similar way to render color, but it uses below format to support both bg and fg color > fmt.Sprintf("[%s](bg-%s)", string(s), bg) > fmt.Sprintf("[%s](fg-%s)", string(s), fg) I wonder why this is not supported and could it be added? The reason I use it is to render a customised implementation of my own table, similar to your's table with colored cell. I'd really like the feature to be added if it's possible so that I could migrate my code to use textview to simple render already made table Thanks
kerem 2026-03-04 01:01:39 +03:00
Author
Owner

@rivo commented on GitHub (Mar 29, 2018):

I guess this makes sense. It's not a trivial addition and will require some thinking. But I'm going to give it the "on roadmap" label. Can't yet say when I'll add this but the label means I will.

<!-- gh-comment-id:377294996 --> @rivo commented on GitHub (Mar 29, 2018): I guess this makes sense. It's not a trivial addition and will require some thinking. But I'm going to give it the "on roadmap" label. Can't yet say when I'll add this but the label means I will.
Author
Owner

@rivo commented on GitHub (Apr 1, 2018):

Have a look at tview's package description. You'll now find instructions on how to change background colors (as well as other text attributes), too.

<!-- gh-comment-id:377810587 --> @rivo commented on GitHub (Apr 1, 2018): Have a look at [`tview`'s package description](https://godoc.org/github.com/rivo/tview). You'll now find instructions on how to change background colors (as well as other text attributes), too.
Author
Owner

@stephencheng commented on GitHub (Apr 3, 2018):

@rivo Thanks for the effort, it's great job. This feature makes the tview more exciting to be used for fun things.

The demo for the box works well, however it seems has kind of default rendering for a few primitives and resulted in a unnecessary background for the presentation demo.

I have attached a few screenshots for the problem. It seems it mostly affects the textview.

I use both iterm2(with dark background) and terminal app(with white background) for a comparison to rule out possible problem from single case/tool for the test.

screen shot 2018-04-03 at 9 54 30 pm
screen shot 2018-04-03 at 9 55 25 pm
screen shot 2018-04-03 at 9 55 47 pm
screen shot 2018-04-03 at 9 57 02 pm

<!-- gh-comment-id:378226507 --> @stephencheng commented on GitHub (Apr 3, 2018): @rivo Thanks for the effort, it's great job. This feature makes the tview more exciting to be used for fun things. The demo for the box works well, however it seems has kind of default rendering for a few primitives and resulted in a unnecessary background for the presentation demo. I have attached a few screenshots for the problem. It seems it mostly affects the textview. I use both iterm2(with dark background) and terminal app(with white background) for a comparison to rule out possible problem from single case/tool for the test. ![screen shot 2018-04-03 at 9 54 30 pm](https://user-images.githubusercontent.com/1396675/38248094-a17a8990-378a-11e8-96b4-432482f15b33.png) ![screen shot 2018-04-03 at 9 55 25 pm](https://user-images.githubusercontent.com/1396675/38248095-a1bbde90-378a-11e8-9ec5-22a3c5b2c80e.png) ![screen shot 2018-04-03 at 9 55 47 pm](https://user-images.githubusercontent.com/1396675/38248096-a1f4ce62-378a-11e8-8f98-7bf08efa747d.png) ![screen shot 2018-04-03 at 9 57 02 pm](https://user-images.githubusercontent.com/1396675/38248097-a230f324-378a-11e8-96d4-01b742b0c77f.png)
Author
Owner

@stephencheng commented on GitHub (Apr 3, 2018):

@rivo I did a little further tests. The problem is that if we do not explicitly state background color, your code will use the default one from current terminal, in my case, it is light grey and white respectively, in order to make it work in textview, we can use something like [:black] to override the default background to be black, to be as same as the application's background.

I am not sure if this is good, rather there could be two solution in the tview's code:

  1. default the textview's (or whatever component having the same problem) global background to be black(which is the default app background color)

  2. or simply set the app's background color to be same as the current terminal's theme's background color, in this case, it could resolve the problem and also it makes it more stylish.

For you to consider

Thanks

<!-- gh-comment-id:378232692 --> @stephencheng commented on GitHub (Apr 3, 2018): @rivo I did a little further tests. The problem is that if we do not explicitly state background color, your code will use the default one from current terminal, in my case, it is light grey and white respectively, in order to make it work in textview, we can use something like [:black] to override the default background to be black, to be as same as the application's background. I am not sure if this is good, rather there could be two solution in the tview's code: 1. default the textview's (or whatever component having the same problem) global background to be black(which is the default app background color) 2. or simply set the app's background color to be same as the current terminal's theme's background color, in this case, it could resolve the problem and also it makes it more stylish. For you to consider Thanks
Author
Owner

@rivo commented on GitHub (Apr 7, 2018):

Thanks for spotting this. This was actually still a bug in the TextView primitive. It's now fixed.

I'm opting for your solution (1), not just to maintain backwards compatibility but also because while I can draw a transparent background (which will keep whatever you set for your terminal), I cannot determine your default text color. So if your users set their background to white, they won't be able to read any text anymore because it will also be white.

The good news, however, is that if you still want to implement solution (2), you can change the global style as follows:

tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault

This will keep whatever you have set, instead of forcing a black background.

<!-- gh-comment-id:379463020 --> @rivo commented on GitHub (Apr 7, 2018): Thanks for spotting this. This was actually still a bug in the `TextView` primitive. It's now fixed. I'm opting for your solution (1), not just to maintain backwards compatibility but also because while I can draw a transparent background (which will keep whatever you set for your terminal), I cannot determine your default text color. So if your users set their background to white, they won't be able to read any text anymore because it will also be white. The good news, however, is that if you still want to implement solution (2), you can change the global style as follows: ```go tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault ``` This will keep whatever you have set, instead of forcing a black background.
Author
Owner

@stephencheng commented on GitHub (Apr 9, 2018):

Thanks, that worked perfectly and thanks for the quick fix for the problem reported.

<!-- gh-comment-id:379626561 --> @stephencheng commented on GitHub (Apr 9, 2018): Thanks, that worked perfectly and thanks for the quick fix for the problem reported.
Author
Owner

@rivo commented on GitHub (Apr 14, 2018):

Just to let you know, in the course of implementing #84, I had to make minor changes to the handling of colour tags. As you can see in the documentation, leaving a color tag field empty will leave that part unchanged. E.g. to just change the background colour but not the foreground colour, you use a tag like this: [:blue]. In addition, now there is also a "reset" character -. So to reset the text colour to its default, you do this: [-] (or [-::], but you can omit trailing fields). As a consequence, an empty tag like this [::] does not do anything.

I don't know if this affects your application but you may want to have a look using the latest tview version and let me know if there are any issues.

<!-- gh-comment-id:381319840 --> @rivo commented on GitHub (Apr 14, 2018): Just to let you know, in the course of implementing #84, I had to make minor changes to the handling of colour tags. As you can see in the [documentation](https://godoc.org/github.com/rivo/tview), leaving a color tag field empty will leave that part unchanged. E.g. to just change the background colour but not the foreground colour, you use a tag like this: `[:blue]`. In addition, now there is also a "reset" character `-`. So to reset the text colour to its default, you do this: `[-]` (or `[-::]`, but you can omit trailing fields). As a consequence, an empty tag like this `[::]` does not do anything. I don't know if this affects your application but you may want to have a look using the latest `tview` version and let me know if there are any issues.
Author
Owner

@stephencheng commented on GitHub (Apr 17, 2018):

It's all good. My app would be brand new to take whatever your new features in.

Thanks for letting me know

<!-- gh-comment-id:381838260 --> @stephencheng commented on GitHub (Apr 17, 2018): It's all good. My app would be brand new to take whatever your new features in. Thanks for letting me know
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#70
No description provided.