pub struct InlineViewport<T: TermOut> { /* private fields */ }Expand description
A custom inline viewport that lives at the bottom of the terminal screen.
Unlike ratatui’s built-in Viewport::Inline, this implementation handles
resize correctly by re-deriving its anchor from a single cursor query and
local knowledge of what it drew, avoiding terminal round-trips during
normal operation entirely.
§Anchor tracking
All positioning is relative to the anchor: the DECSC saved cursor position, which sits at the end of the scrollback content, one row above the viewport (mid-line while a response is streaming). Reflowing terminals (kitty/alacritty/VTE) translate the live cursor through a resize reflow but merely clamp the saved cursor, so after a resize the anchor must be reconstructed from the live cursor:
- when the cursor is hidden it is parked on the anchor, so the queried position is the anchor;
- when the cursor is shown in the viewport, we compute how many rows the previously drawn viewport rows rewrap to at the new width (we know exactly what we drew) and subtract;
- non-reflowing terminals (xterm-class) keep everything in place — they are detected by the queried position matching the old one, in which case the old anchor remains valid.
The anchor’s column (end of a partially streamed line) is tracked by
parsing everything printed through InlineViewport::print_above, so it
can be recomputed for any width without asking the terminal.
All terminal output and queries go through the owned TermOut, so the
viewport can be driven against a virtual terminal in tests.
Implementations§
Source§impl<T: TermOut> InlineViewport<T>
impl<T: TermOut> InlineViewport<T>
pub fn new(term: T, height: u16) -> Result<Self>
Sourcepub fn term_mut(&mut self) -> &mut T
pub fn term_mut(&mut self) -> &mut T
Direct access to the underlying terminal, e.g. for cleanup sequences or setting the terminal title.
pub fn area(&self) -> Rect
Sourcepub fn handle_resize(&mut self) -> Result<()>
pub fn handle_resize(&mut self) -> Result<()>
Handle a terminal resize with NO terminal output or queries.
Marks the anchor stale; the next draw or print re-derives it with a
single cursor-position query (see InlineViewport::re_anchor).
pub fn print_above( &mut self, writer: impl FnOnce(&mut Vec<u8>) -> Result<()>, ) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn print_line_above( &mut self, line: Line<'_>, ) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn print_spans_above( &mut self, line: Line<'_>, ) -> Result<(), Box<dyn Error + Send + Sync>>
Sourcepub fn draw<F>(&mut self, desired_lines: u16, render_fn: F) -> Result<()>where
F: FnOnce(&mut ViewportFrame<'_>),
pub fn draw<F>(&mut self, desired_lines: u16, render_fn: F) -> Result<()>where
F: FnOnce(&mut ViewportFrame<'_>),
Draw the viewport using relative cursor movement from the saved
cursor position (end of scrollback). Restores the saved cursor,
then uses MoveToNextLine / MoveToColumn to reach each cell,
avoiding any absolute coordinates.
Auto Trait Implementations§
impl<T> Freeze for InlineViewport<T>where
T: Freeze,
impl<T> RefUnwindSafe for InlineViewport<T>where
T: RefUnwindSafe,
impl<T> Send for InlineViewport<T>where
T: Send,
impl<T> Sync for InlineViewport<T>where
T: Sync,
impl<T> Unpin for InlineViewport<T>where
T: Unpin,
impl<T> UnsafeUnpin for InlineViewport<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for InlineViewport<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more