tui-rs
tui-rs copied to clipboard
How to prevent a paragraph widget from scrolling beyond it's text?
I need to display a large amount of text using a relatively smaller sized paragraph widget. I need to give the user the ability to scroll this text and therefore used code like so -
Paragraph::new(content.clone())
.scroll((scroll, 0))
The problem is that the scroll
variable is unbounded, making it possible for the user to scroll beyond all text. I don't think this is ideal and am looking for a means to remedy this.
A solution would be found if there's a way to get the number of lines after the content is wrapped and the number of lines displayed on the screen at a time. Any ideas? Thank you!
I am having the same issue. While it is possible to get the heigth from the text displayed inside the paragraph, there does not seem to be any means to retrieve the height of the wrapped paragraph.
If it's any help, I ended up making an estimate of the length of the wrapped text based on the width of the terminal and the number of characters. It still over scrolls quite a bit, but not indefinitely.
Slightly better workaround
The variable y
here conveniently contains the sum scroll_offset + lines rendered
, but there's no way to return it to the caller because the Paragraph
widget is consumed when render
is called on it, and the signature of the render
function cannot be changed (because it is a trait implementation).
Therefore, in the forked Tui-rs repository linked above, the variable is stored into a temporary file and then read by the calling function (my application) after render
to decide whether an over scroll has occurred. Here's the commit using this workaround in my application.