[GH-ISSUE #648] feature request: expose number of lines in text on TextView #474

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

Originally created by @rgrannell1 on GitHub (Sep 14, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/648

Use-case:

I'm dynamically updating a TextView with content from stdout (so is #645), and I'd like to efficiently check the total number of lines ("\n" delimited) in the view. I'm using the view's line-count to:

  • Handle empty standard-output appropriately
  • Display a total-line and current-line widget in the header

At the moment, I use a multiwriter to copy stdout to a buffer, and count lines from there. This is untidy.

Ideally I could call

tview.GetLineCount()

that would wrap this functionality.

Implementation:

tview.GetLineCount()

is based of a private value lineCount in the TextView struct. This value would be updated in .Write and .SetText and wherever else it's needed (reindexBuffer?), and would be capped by the struct value maxLines if present.

Originally created by @rgrannell1 on GitHub (Sep 14, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/648 ## Use-case: I'm dynamically updating a `TextView` with content from stdout (so is #645), and I'd like to efficiently check the total number of lines ("\n" delimited) in the view. I'm using the view's line-count to: - Handle empty standard-output appropriately - Display a total-line and current-line widget in the header At the moment, I use a multiwriter to copy stdout to a buffer, and count lines from there. This is untidy. Ideally I could call ```go tview.GetLineCount() ``` that would wrap this functionality. ## Implementation: `tview.GetLineCount()` is based of a private value `lineCount` in the `TextView` struct. This value would be updated in `.Write` and `.SetText` and wherever else it's needed (reindexBuffer?), and would be capped by the struct value `maxLines` if present. ## Related Issues: - #451
kerem closed this issue 2026-03-04 01:05:18 +03:00
Author
Owner

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

There are multiple "lines" in TextView this could refer to. Which one is the line count that you mean?

  1. Independent of how the TextView displays text, the number of lines in the original text buffer, i.e. the number of \n characters (plus one)
  2. The word-wrapped number of lines in the buffer, i.e. if the TextView is not as wide as the longest line in the original text buffer (and if word wrapping is enabled), this number will be greater than the number of \n characters in the buffer.
  3. The number of lines visible on screen. After a certain buffer size, this will always be the (inner) height of the TextView.
<!-- gh-comment-id:927813081 --> @rivo commented on GitHub (Sep 27, 2021): There are multiple "lines" in `TextView` this could refer to. Which one is the line count that you mean? 1. Independent of how the `TextView` displays text, the number of lines in the original text buffer, i.e. the number of `\n` characters (plus one) 1. The word-wrapped number of lines in the buffer, i.e. if the `TextView` is not as wide as the longest line in the original text buffer (and if [word wrapping](https://pkg.go.dev/github.com/rivo/tview#TextView.SetWordWrap) is enabled), this number will be greater than the number of `\n` characters in the buffer. 1. The number of lines visible on screen. After a certain buffer size, this will always be the (inner) height of the `TextView`.
Author
Owner

@rgrannell1 commented on GitHub (Sep 27, 2021):

Sorry, I didn't mean to be ambiguous. Option 3 is well covered by the API already, so I'm referring to options one. I personally won't need option two, but one idea would be to add a wrapped: bool parameter to allow people to select this

<!-- gh-comment-id:927941684 --> @rgrannell1 commented on GitHub (Sep 27, 2021): Sorry, I didn't mean to be ambiguous. Option 3 is well covered by the API already, so I'm referring to options one. I personally won't need option two, but one idea would be to add a `wrapped: bool` parameter to allow people to select this
Author
Owner

@rivo commented on GitHub (Feb 15, 2022):

I added TextView.GetOriginalLineCount() in the latest commit.

<!-- gh-comment-id:1040375159 --> @rivo commented on GitHub (Feb 15, 2022): I added `TextView.GetOriginalLineCount()` in the latest commit.
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#474
No description provided.