[GH-ISSUE #774] Text selection in TextView #567

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

Originally created by @F1bonacc1 on GitHub (Nov 29, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/774

First of all, absolutely love the library, its flexibility, the extensive documentation, and all the examples you provide.

I noticed that you've asked before about text selection in #694
My issue is a little different - when using the regular (terminal) selection (mouse mode on + shift || mouse off) it also selects the borders which surround the TextView.
You mentioned that maybe it can be done using regions. I also have been thinking along those lines, but I am not sure it will be responsive enough with the regex (for region eval) that runs during the selection.

Do you have any ideas what would be the best way to approach this?
Thanks!

Originally created by @F1bonacc1 on GitHub (Nov 29, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/774 First of all, absolutely love the library, its flexibility, the extensive documentation, and all the examples you provide. I noticed that you've asked before about text selection in #694 My issue is a little different - when using the regular (terminal) selection (mouse mode on + shift || mouse off) it also selects the borders which surround the TextView. You mentioned that maybe it can be done using regions. I also have been thinking along those lines, but I am not sure it will be responsive enough with the regex (for region eval) that runs during the selection. Do you have any ideas what would be the best way to approach this? Thanks!
Author
Owner

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

Well, I guess I was a bit quick to say that because while I still think it may be possible, it might be difficult to be made to work. Regions allow you to show selections in a TextView object. But then you still have to map mouse positions to locations in the text. Or you have to insert regions inbetween each character, which could work but is not great.

Would the new TextArea primitive help you? That primitive supports text selections like the ones you're asking for. You'd still have to integrate the clipboard with your OS's clipboard, though. And it's not a read-only element. (I don't know your use case so maybe that's ok?)

<!-- gh-comment-id:1335247908 --> @rivo commented on GitHub (Dec 2, 2022): Well, I guess I was a bit quick to say that because while I still think it may be possible, it might be difficult to be made to work. Regions allow you to show selections in a `TextView` object. But then you still have to map mouse positions to locations in the text. Or you have to insert regions inbetween each character, which could work but is not great. Would the new `TextArea` primitive help you? That primitive supports text selections like the ones you're asking for. You'd still have to integrate the clipboard with your OS's clipboard, though. And it's not a read-only element. (I don't know your use case so maybe that's ok?)
Author
Owner

@F1bonacc1 commented on GitHub (Dec 2, 2022):

@rivo Thank you for getting back to me.
Using TextArea is an interesting idea... Maybe if I can convert it to a read-only, it would actually work.
My use case is a log viewer for running processes and some users asked for the ability to select snippets from that log. It's an open-source project so feel free to check it out here:
https://github.com/F1bonacc1/process-compose

Also, TextArea doesn't implement the Writer pattern, which is very useful for a log viewer.
I suppose that can be added. I am still not sure what is the best direction here:

  1. The text selection functionality to TextView
  2. Add read-only, Write interface to TextArea
<!-- gh-comment-id:1335655274 --> @F1bonacc1 commented on GitHub (Dec 2, 2022): @rivo Thank you for getting back to me. Using TextArea is an interesting idea... Maybe if I can convert it to a read-only, it would actually work. My use case is a log viewer for running processes and some users asked for the ability to select snippets from that log. It's an open-source project so feel free to check it out here: https://github.com/F1bonacc1/process-compose Also, TextArea doesn't implement the Writer pattern, which is very useful for a log viewer. I suppose that can be added. I am still not sure what is the best direction here: 1. The text selection functionality to TextView 2. Add read-only, Write interface to TextArea
Author
Owner

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

With the TextArea, you could set max-length to 1 or something. But thinking about it some more, I don't think it's a great solution. TextArea is made for text input primarily. Scrolling, for example, happens by positioning the cursor. Its keyboard controls are very different from those of TextView. It doesn't support colours etc.

I guess the only correct way to do it is for me to implement mouse selection support in TextView.

I will keep this issue open until this has been added. I should note, though, that I don't want to give you false hope of this being available very soon. I.e. it might take a while. There are a bunch of other issues that need to be taken care of first.

<!-- gh-comment-id:1336510896 --> @rivo commented on GitHub (Dec 4, 2022): With the `TextArea`, you could set [max-length](https://pkg.go.dev/github.com/rivo/tview#TextArea.SetMaxLength) to 1 or something. But thinking about it some more, I don't think it's a great solution. `TextArea` is made for text input primarily. Scrolling, for example, happens by positioning the cursor. Its keyboard controls are very different from those of `TextView`. It doesn't support colours etc. I guess the only correct way to do it is for me to implement mouse selection support in `TextView`. I will keep this issue open until this has been added. I should note, though, that I don't want to give you false hope of this being available very soon. I.e. it might take a while. There are a bunch of other issues that need to be taken care of first.
Author
Owner

@F1bonacc1 commented on GitHub (Dec 4, 2022):

Thank you for taking this under consideration.
And once again thank you for such a great library!

<!-- gh-comment-id:1336534291 --> @F1bonacc1 commented on GitHub (Dec 4, 2022): Thank you for taking this under consideration. And once again thank you for such a great library!
Author
Owner

@F1bonacc1 commented on GitHub (Dec 26, 2022):

@digitallyserviced
Thanks for pointing this out, I will definitely check it.
The solution that I eventually implemented in my project is:

  1. When a user enters Selection Mode I switch to TextArea (from TextView)
  2. I keep all the styling and copy the text so it is almost seamless.
  3. When Selection Mode is exited, I reverse the whole process.

Surprisingly this wasn't the most difficult part. The most difficult part was finding a cross-platform clipboard library.
There are solutions that require additional installations (xclip or xsel), and I didn't want to ask my users to mess with all that. So I created a new Frankenstein package from all the pieces that I found:
https://github.com/F1bonacc1/glippy

<!-- gh-comment-id:1365414281 --> @F1bonacc1 commented on GitHub (Dec 26, 2022): @digitallyserviced Thanks for pointing this out, I will definitely check it. The solution that I eventually implemented in my project is: 1. When a user enters Selection Mode I switch to TextArea (from TextView) 2. I keep all the styling and copy the text so it is almost seamless. 3. When Selection Mode is exited, I reverse the whole process. Surprisingly this wasn't the most difficult part. The most difficult part was finding a cross-platform clipboard library. There are solutions that require additional installations (`xclip` or `xsel`), and I didn't want to ask my users to mess with all that. So I created a new Frankenstein package from all the pieces that I found: https://github.com/F1bonacc1/glippy
Author
Owner

@quantonganh commented on GitHub (Mar 30, 2023):

I'm building a simple terminal UI for ChatGPT: https://github.com/quantonganh/chatgpt

My issue is the same as @F1bonacc1:

when using the regular (terminal) selection (mouse mode on + shift || mouse off) it also selects the borders which surround the TextView.

Regions allow you to show selections in a TextView object. But then you still have to map mouse positions to locations in the text. Or you have to insert regions inbetween each character, which could work but is not great.

Could you please elaborate more? How can I use regions to make the selections does not select the border?

I guess the only correct way to do it is for me to implement mouse selection support in TextView.

I can use mouse to select text in TextView, but the issue is, as I said above, it also selects the border:

Screen Shot 2023-03-30 at 5 23 24 PM

Btw, do you know why the highlight is not clear?

I will keep this issue open until this has been added. I should note, though, that I don't want to give you false hope of this being available very soon. I.e. it might take a while. There are a bunch of other issues that need to be taken care of first.

Do you have a plan to add it?

Thank you for great work!

The solution that I eventually implemented in my project is:

When a user enters Selection Mode I switch to TextArea (from TextView)
I keep all the styling and copy the text so it is almost seamless.
When Selection Mode is exited, I reverse the whole process.

Not sure if I miss something, but when I select text in TextArea, it also selects the border:

Screen Shot 2023-03-30 at 5 41 53 PM
<!-- gh-comment-id:1490065120 --> @quantonganh commented on GitHub (Mar 30, 2023): I'm building a simple terminal UI for ChatGPT: https://github.com/quantonganh/chatgpt My issue is the same as @F1bonacc1: > when using the regular (terminal) selection (mouse mode on + shift || mouse off) it also selects the borders which surround the TextView. > Regions allow you to show selections in a TextView object. But then you still have to map mouse positions to locations in the text. Or you have to insert regions inbetween each character, which could work but is not great. Could you please elaborate more? How can I use regions to make the selections does not select the border? > I guess the only correct way to do it is for me to implement mouse selection support in TextView. I can use mouse to select text in TextView, but the issue is, as I said above, it also selects the border: <img width="1279" alt="Screen Shot 2023-03-30 at 5 23 24 PM" src="https://user-images.githubusercontent.com/1568504/228807588-ad8426a3-a170-4f3a-9b76-f70ea0c607aa.png"> Btw, do you know why the highlight is not clear? > I will keep this issue open until this has been added. I should note, though, that I don't want to give you false hope of this being available very soon. I.e. it might take a while. There are a bunch of other issues that need to be taken care of first. Do you have a plan to add it? Thank you for great work! > The solution that I eventually implemented in my project is: > > When a user enters Selection Mode I switch to TextArea (from TextView) > I keep all the styling and copy the text so it is almost seamless. > When Selection Mode is exited, I reverse the whole process. Not sure if I miss something, but when I select text in TextArea, it also selects the border: <img width="546" alt="Screen Shot 2023-03-30 at 5 41 53 PM" src="https://user-images.githubusercontent.com/1568504/228811785-86de6e5e-fbe6-4fb8-99da-9ac7cd2e2d1f.png">
Author
Owner

@quantonganh commented on GitHub (Mar 30, 2023):

As a workaround, I have to hold the option-command key down while dragging the left mouse button to select block of text.

<!-- gh-comment-id:1490118191 --> @quantonganh commented on GitHub (Mar 30, 2023): As a workaround, I have to hold the `option`-`command` key down while dragging the left mouse button to select block of text.
Author
Owner

@rivo commented on GitHub (Mar 30, 2023):

@quantonganh The answers to all of your questions are given in this thread further above.

<!-- gh-comment-id:1490754976 --> @rivo commented on GitHub (Mar 30, 2023): @quantonganh The answers to all of your questions are given in this thread further above.
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#567
No description provided.