pub struct Renderer<W: Write> { /* private fields */ }Expand description
Diff-based terminal renderer that efficiently updates only changed content.
Renderer owns a writable output (anything implementing std::io::Write), a Theme, and the previous frame state. On each render_frame call it diffs the new frame against the previous one and emits only the changed ANSI sequences.
§Cursor invariant
Uses relative cursor movement (MoveUp + \r) to navigate back to the start of the managed region. This avoids absolute row tracking, which breaks when the terminal scrolls content upward. After every render or push_to_scrollback, the cursor sits at the end of the last managed line unless explicitly repositioned.
§Usage
use tui::{Renderer, Theme, Frame, Line};
let mut renderer = Renderer::new(std::io::stdout(), Theme::default(), (80, 24));
renderer.render_frame(|ctx| {
Frame::new(vec![
Line::new("Hello, world!"),
Line::styled("Status: OK", ctx.theme.success()),
])
}).unwrap();§Key methods
render_frame(f)— Render a frame. The closure receives aViewContextand returns aFrame.push_to_scrollback(lines)— Flush lines into terminal scrollback (they become non-managed history).on_resize(size)— Notify the renderer of a terminal resize. The next render will clear and redraw.clear_screen()— Clear the entire viewport and scrollback.set_theme(theme)— Replace the activeTheme.apply_commands(cmds)— Execute a batch ofRendererCommands.context()— Get the currentViewContextwithout rendering.
§RendererCommand
Commands that can be batched via apply_commands:
ClearScreen— Clear viewport and scrollback.SetTheme(Theme)— Replace the active theme.SetMouseCapture(bool)— Enable or disable mouse capture.
§See also
Frame— The logical output consumed by the renderer.Theme— The color palette used to buildViewContext.TerminalSession— Manages raw-mode lifecycle.
Implementations§
Source§impl<W: Write> Renderer<W>
impl<W: Write> Renderer<W>
pub fn new(writer: W, theme: Theme, size: impl Into<Size>) -> Self
Sourcepub fn render_frame(
&mut self,
f: impl FnOnce(&ViewContext) -> Frame,
) -> Result<()>
pub fn render_frame( &mut self, f: impl FnOnce(&ViewContext) -> Frame, ) -> Result<()>
Render a frame using a closure.
The closure receives a ViewContext and returns a Frame.
pub fn clear_screen(&mut self) -> Result<()>
pub fn push_to_scrollback(&mut self, lines: &[Line]) -> Result<()>
pub fn on_resize(&mut self, size: impl Into<Size>)
pub fn context(&self) -> ViewContext
pub fn set_theme(&mut self, theme: Theme)
pub fn apply_commands(&mut self, commands: Vec<RendererCommand>) -> Result<()>
pub fn writer(&self) -> &W
pub fn test_writer_mut(&mut self) -> &mut W
Auto Trait Implementations§
impl<W> Freeze for Renderer<W>where
W: Freeze,
impl<W> RefUnwindSafe for Renderer<W>where
W: RefUnwindSafe,
impl<W> Send for Renderer<W>where
W: Send,
impl<W> Sync for Renderer<W>where
W: Sync,
impl<W> Unpin for Renderer<W>where
W: Unpin,
impl<W> UnsafeUnpin for Renderer<W>where
W: UnsafeUnpin,
impl<W> UnwindSafe for Renderer<W>where
W: 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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