[GH-ISSUE #914] Bypassing text view and printing directly on screen using fmt.Print #665

Open
opened 2026-03-04 01:06:53 +03:00 by kerem · 2 comments
Owner

Originally created by @ArthurD3nt on GitHub (Nov 7, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/914

I have a simple Flex view with two panes, a sort of file explorer on the left and a markdown renderer view on the right

image

I want to use a custom renderer called glamour and (ideally) simply fmt.Print() the output of the render like it's indicated on the library's wiki.

So I basically want to avoid translating the output from ANSI to TextView using the TranslateANSI() function that defeats the purpose of using this render, is there any way I can do that?

Thanks

Originally created by @ArthurD3nt on GitHub (Nov 7, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/914 I have a simple Flex view with two panes, a sort of file explorer on the left and a markdown renderer view on the right <img width="1728" alt="image" src="https://github.com/rivo/tview/assets/40174350/8e90e8eb-5828-40ab-95bf-457cc649c4aa"> I want to use a custom renderer called [glamour](https://github.com/charmbracelet/glamour) and (ideally) simply `fmt.Print()` the output of the render like it's indicated on the library's wiki. So I basically want to avoid translating the output from ANSI to `TextView` using the `TranslateANSI()` function that defeats the purpose of using this render, is there any way I can do that? Thanks
Author
Owner

@rivo commented on GitHub (Nov 7, 2023):

So I basically want to avoid translating the output from ANSI to TextView using the TranslateANSI() function that defeats the purpose of using this render, is there any way I can do that?

This part I don't understand. It seems to me that glamour outputs ANSI escape codes. To display them in a TextView, you'll have to use TranslateANSI(). Or the ANSIWriter if you're dealing with a continuous stream of text.

<!-- gh-comment-id:1798779698 --> @rivo commented on GitHub (Nov 7, 2023): > So I basically want to avoid translating the output from ANSI to `TextView` using the `TranslateANSI()` function that defeats the purpose of using this render, is there any way I can do that? This part I don't understand. It seems to me that `glamour` outputs ANSI escape codes. To display them in a `TextView`, you'll *have* to use `TranslateANSI()`. Or the [`ANSIWriter`](https://pkg.go.dev/github.com/rivo/tview#ANSIWriter) if you're dealing with a continuous stream of text.
Author
Owner

@ArthurD3nt commented on GitHub (Nov 7, 2023):

That's what I thought at first but it seems that I lose some formatting by doing so.
By using the TranslateAnsi() function like this:

func render() *tview.TextView {

	in := `# Hello World

This is a simple example of Markdown rendering with Glamour!
Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too.

Bye!
`

	out, err := glamour.Render(string(in), "dark")
	if err != nil {
		log.Fatal(err)
	}

	view := tview.NewTextView().SetText(tview.TranslateANSI(out)).SetDynamicColors(true)

	return view
}

I get this output:
image

While I want to get this:

I read the docs of the function TranslateANSI() and as the name rightly suggests it translates the string while I want to preserve the original formatting.
Is there any way I can avoid to use textView to display the rendered result?

<!-- gh-comment-id:1798865383 --> @ArthurD3nt commented on GitHub (Nov 7, 2023): That's what I thought at first but it seems that I lose some formatting by doing so. By using the `TranslateAnsi()` function like this: ```golang func render() *tview.TextView { in := `# Hello World This is a simple example of Markdown rendering with Glamour! Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too. Bye! ` out, err := glamour.Render(string(in), "dark") if err != nil { log.Fatal(err) } view := tview.NewTextView().SetText(tview.TranslateANSI(out)).SetDynamicColors(true) return view } ``` I get this output: <img width="844" alt="image" src="https://github.com/rivo/tview/assets/40174350/3d5c150d-acd2-4c77-9dcd-7f7b4bdf5d9f"> While I want to get this: ![](https://raw.githubusercontent.com/charmbracelet/glamour/master/examples/helloworld/helloworld.png) I read the [docs](https://pkg.go.dev/github.com/rivo/tview#TranslateANSI) of the function `TranslateANSI()` and as the name rightly suggests it translates the string while I want to preserve the original formatting. Is there any way I can avoid to use textView to display the rendered result?
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#665
No description provided.