prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.1k stars 717 forks source link

Store calculated height on ScrollablePane to enable tail -f like behaviour #1791

Open msopacua opened 8 months ago

msopacua commented 8 months ago

We can use pane.vertical_scroll to manipulate the scrolling so it always shows the last line, useful for having a pane that shows some log or command output. However, this value needs to be equal to the lines written minus the height of the pane, which is easy to get if it's set on initialisation, but it is not if the pane is dynamic and supposed to fill the available height.

The height is calculated by preferred_height, but that receives input from its caller that we can't easily ascertain. The result of this calculation is kept private.

To correctly use vertical_scroll, we need to implement the following:

if pane.height and state.line_counter > pane.height:
    pane.vertical_scroll = state.line_counter - pane.height

Which works for the fixed height case and can be simplified to: max(0, state.line_counter - pane.height) if we had some attribute available that has the current height of the pane, regardless of whether it's fixed or dynamically sized.

But see also the related bug.