tui-rs icon indicating copy to clipboard operation
tui-rs copied to clipboard

How to prevent a paragraph widget from scrolling beyond it's text?

Open actuday6418 opened this issue 3 years ago • 3 comments

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!

actuday6418 avatar Apr 10 '21 10:04 actuday6418

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.

on3iro avatar Dec 02 '21 10:12 on3iro

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.

actuday6418 avatar Dec 03 '21 04:12 actuday6418

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.

actuday6418 avatar Dec 03 '21 09:12 actuday6418