[GH-ISSUE #542] ANSIWriter background converting from chroma #395

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

Originally created by @neezer on GitHub (Dec 31, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/542

I'm rendering JSON in a TextView with syntax highlighting provided by chroma passed through an ANSIWriter, roughly like so:

import (
  "github.com/alecthomas/chroma/quick"
)

// elsewhere

writer := tview.ANSIWriter(myTextView)
quick.Highlight(writer, fmt.Sprintln(myText), "json", "terminal256", "dracula")

The TextView puts a background behind my text, which I'm not expecting:

Screen Shot 2020-12-31 at 1 15 14 AM

If I use the same quick.Highlight command but change the writer to os.Stdout, then I don't see a different-color background:

Screen Shot 2020-12-31 at 1 14 54 AM

I have not called .SetBackground on my TextView—I tried it and it completely wrecked the performance and functionality of my app... text got all garbled and rendered really slow.

I suspect there's something getting messed up in the ANSIWriter translation, but I've no clue what. I tried creating my own custom Chroma theme that explicitly disables background color (eg. chroma.Background: "bg:0") but I got the same behavior in my tview.TextBox.

Any idea what I might be doing wrong or how I could eliminate the discrepancy in background color?

Originally created by @neezer on GitHub (Dec 31, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/542 I'm rendering JSON in a TextView with syntax highlighting provided by [chroma](https://github.com/alecthomas/chroma) passed through an ANSIWriter, roughly like so: ```go import ( "github.com/alecthomas/chroma/quick" ) // elsewhere writer := tview.ANSIWriter(myTextView) quick.Highlight(writer, fmt.Sprintln(myText), "json", "terminal256", "dracula") ``` The TextView puts a background behind my text, which I'm not expecting: <img width="651" alt="Screen Shot 2020-12-31 at 1 15 14 AM" src="https://user-images.githubusercontent.com/29997/103403780-3b32bd00-4b06-11eb-9ed1-434cb4cbecc0.png"> If I use the same `quick.Highlight` command but change the writer to `os.Stdout`, then I don't see a different-color background: <img width="695" alt="Screen Shot 2020-12-31 at 1 14 54 AM" src="https://user-images.githubusercontent.com/29997/103404115-44705980-4b07-11eb-9a1f-a4c8208d2785.png"> I have not called `.SetBackground` on my TextView—I tried it and it completely wrecked the performance and functionality of my app... text got all garbled and rendered really slow. I suspect there's something getting messed up in the ANSIWriter translation, but I've no clue what. I tried creating my own custom Chroma theme that explicitly disables background color (eg. [`chroma.Background: "bg:0"`](https://github.com/alecthomas/chroma/blob/master/style.go#L320-L321)) but I got the same behavior in my `tview.TextBox`. Any idea what I might be doing wrong or how I could eliminate the discrepancy in background color?
kerem closed this issue 2026-03-04 01:04:37 +03:00
Author
Owner

@rivo commented on GitHub (Jan 11, 2021):

It's hard to say without being able to reproduce this easily. It looks to me like your tool is generating a background colour that is different from tview's default background colour (which is tcell.ColorBlack and I don't really know what Escape codes are generated for this colour).

Maybe it helps if you try with a minimal JSON file and post the output of tview.TranslateANSI() here. It could give us some clues.

<!-- gh-comment-id:758078268 --> @rivo commented on GitHub (Jan 11, 2021): It's hard to say without being able to reproduce this easily. It looks to me like your tool is generating a background colour that is different from `tview`'s default background colour (which is [`tcell.ColorBlack`](https://github.com/gdamore/tcell/blob/29bb185874fd86b7d0a5b57e1a17d6f8ed9a9040/color.go#L57) and I don't really know what Escape codes are generated for this colour). Maybe it helps if you try with a minimal JSON file and post the output of `tview.TranslateANSI()` here. It could give us some clues.
Author
Owner

@rivo commented on GitHub (Mar 13, 2021):

@neezer The most recent update to tview introduces some fixes to its colour handling. (Actually, I had to work around a change that was introduced in tcell.) Could you please try your code again with the latest version and let me know if the problem persists?

<!-- gh-comment-id:798788469 --> @rivo commented on GitHub (Mar 13, 2021): @neezer The most recent update to `tview` introduces some fixes to its colour handling. (Actually, I had to work around a change that was introduced in `tcell`.) Could you please try your code again with the latest version and let me know if the problem persists?
Author
Owner

@rivo commented on GitHub (Apr 27, 2021):

You can reopen if there's anything else that needs to be reviewed.

<!-- gh-comment-id:827455572 --> @rivo commented on GitHub (Apr 27, 2021): You can reopen if there's anything else that needs to be reviewed.
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#395
No description provided.