[GH-ISSUE #698] TextView adds extra spaces at the end of each line #509

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

Originally created by @AmrGanz on GitHub (Feb 3, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/698

When I display some text in TextView while disabling the mouse [EnableMouse(false)]. The lines seems to have unnecessary spaces at the end:

image

Example code:

package main

import (
	"io/ioutil"

	"github.com/rivo/tview"
)

func main() {
	data := "short line\na very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line\nsome extra data"

	app := tview.NewApplication()
	textView := tview.NewTextView().
		SetChangedFunc(func() {
			app.Draw()
		})
	textView.SetText(data)
	textView.SetBorder(false)
	textView.SetWrap(true)
	app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false)
	app.Run()
}
Originally created by @AmrGanz on GitHub (Feb 3, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/698 When I display some text in TextView while disabling the mouse [EnableMouse(false)]. The lines seems to have unnecessary spaces at the end: ![image](https://user-images.githubusercontent.com/43642001/152402989-8c120341-bb79-4fba-8f4d-cbda2994e76b.png) Example code: ``` package main import ( "io/ioutil" "github.com/rivo/tview" ) func main() { data := "short line\na very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line\nsome extra data" app := tview.NewApplication() textView := tview.NewTextView(). SetChangedFunc(func() { app.Draw() }) textView.SetText(data) textView.SetBorder(false) textView.SetWrap(true) app.SetRoot(textView, true).SetFocus(textView).EnableMouse(false) app.Run() } ```
kerem closed this issue 2026-03-04 01:05:36 +03:00
Author
Owner

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

I'm not sure I understand. If mouse handling is turned off, dragging your mouse on the screen will cause text to be highlighted/selected in most terminals. It's not a functionality of tview but of the terminal you're using. Here's what it looks like when I randomly drag the mouse in iTerm2 on macOS:

image

It has nothing to do with the TextView component.

Or am I missing something here?

<!-- gh-comment-id:1099502829 --> @rivo commented on GitHub (Apr 14, 2022): I'm not sure I understand. If mouse handling is turned off, dragging your mouse on the screen will cause text to be highlighted/selected in most terminals. It's not a functionality of `tview` but of the terminal you're using. Here's what it looks like when I randomly drag the mouse in iTerm2 on macOS: <img width="588" alt="image" src="https://user-images.githubusercontent.com/480930/163453040-ce470224-a2d0-4413-9f42-14118a821b39.png"> It has nothing to do with the `TextView` component. Or am I missing something here?
Author
Owner

@AmrGanz commented on GitHub (Apr 24, 2022):

Hello Rivo,
If I cat a file in the terminal for example /etc/passwd, and if I highlight the first line, it will not show extra empty characters at the end of it, rather it will immediately jump to the next line since it understands the new line character.

But, in the textview case, I think it adds empty characters till it hits the new line.

I could think of this test:
1- Use the same code I shared in this issue
2- Count the text characters without running the code
3- Run the code and highlight with mouse the text from the first character in the first line till the last character in the last line and copy it
4- Add the copied characters to a file and count the number of characters there and it will show a different and bigger number compared to the first count in step 2


I ran this test and got these counts:

$ wc -c /tmp/before    <--- from step 2
288 /tmp/before

# wc -c /tmp/after       <--- from step 4
490 /tmp/after
<!-- gh-comment-id:1107802970 --> @AmrGanz commented on GitHub (Apr 24, 2022): Hello Rivo, If I `cat` a file in the terminal `for example /etc/passwd`, and if I highlight the first line, it will not show extra empty characters at the end of it, rather it will immediately jump to the next line `since it understands the new line character`. But, in the textview case, I think it adds empty characters till it hits the new line. I could think of this test: 1- Use the same code I shared in this issue 2- Count the text characters `without running the code` 3- Run the code and highlight `with mouse` the text from the first character in the first line till the last character in the last line and copy it 4- Add the copied characters to a file and count the number of characters there and it will show a different and bigger number compared to the first count in step 2 -------------- I ran this test and got these counts: ``` $ wc -c /tmp/before <--- from step 2 288 /tmp/before # wc -c /tmp/after <--- from step 4 490 /tmp/after ```
Author
Owner

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

Yes, it does that. The nature of tview (or, rather, tcell, which tview uses) is that it uses the entire screen to draw content into it. That's very different from printing a few characters into a regular terminal session, like with echo or something similar. Terminals deal with that case differently.

Maybe this becomes more obvious if you include a border:

textView.SetBorder(true)

The characters between the text and the right border are technically spaces.

I would say that using TextView or actually tview in general is the wrong tool if you're looking for "normal" terminal-like behaviour. Having said that, there is a discussion (#774) which might result in me adding text selection to TextView when the mouse is enabled. I can't say at this point when this will be introduced, however. And even when it is, it will still work differently from directly selecting and copy+pasting content. (For example, it still won't be able to handle Cmd-C+Cmd-V on Macs as those are not sent directly to the application running inside the terminal.)

<!-- gh-comment-id:1356328498 --> @rivo commented on GitHub (Dec 17, 2022): Yes, it does that. The nature of `tview` (or, rather, `tcell`, which `tview` uses) is that it uses the entire screen to draw content into it. That's very different from printing a few characters into a regular terminal session, like with `echo` or something similar. Terminals deal with that case differently. Maybe this becomes more obvious if you include a border: ```go textView.SetBorder(true) ``` The characters between the text and the right border are technically spaces. I would say that using `TextView` or actually `tview` in general is the wrong tool if you're looking for "normal" terminal-like behaviour. Having said that, there is a discussion (#774) which might result in me adding text selection to `TextView` _when the mouse is enabled_. I can't say at this point when this will be introduced, however. And even when it is, it will still work differently from directly selecting and copy+pasting content. (For example, it still won't be able to handle <kbd>Cmd-C</kbd>+<kbd>Cmd-V</kbd> on Macs as those are not sent directly to the application running inside the terminal.)
Author
Owner

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

Hello @rivo ,

Thank you for getting back to me on this, I always appreciate your help.

You made a valid point, as I didn't look at this "issue" from such a perspective, and now I see why it won't be possible to show output on TextView on such a way.

Thanks again, I will close this ticket.

<!-- gh-comment-id:1356333296 --> @AmrGanz commented on GitHub (Dec 17, 2022): Hello @rivo , Thank you for getting back to me on this, I always appreciate your help. You made a valid point, as I didn't look at this "issue" from such a perspective, and now I see why it won't be possible to show output on TextView on such a way. Thanks again, I will close this ticket.
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#509
No description provided.