Skip to main content

StreamWidget

Struct StreamWidget 

Source
pub struct StreamWidget { /* private fields */ }
Expand description

A streaming text widget optimized for LLM token output.

This widget maintains its own content buffer and provides two rendering paths:

  • Fast path: Direct cursor-based append for simple cases
  • Slow path: Full dirty-rect re-render for complex cases

Implementations§

Source§

impl StreamWidget

Source

pub fn new(bounds: Rect) -> Self

Create a new stream widget with the given bounds.

Source

pub fn with_config(bounds: Rect, config: StreamConfig) -> Self

Create a new stream widget with custom configuration.

Source

pub const fn bounds(&self) -> Rect

Get the widget bounds.

Source

pub fn set_bounds(&mut self, bounds: Rect)

Set new bounds for the widget.

Source

pub const fn set_fg(&mut self, fg: Rgb)

Set the foreground color for subsequent text.

Source

pub const fn set_bg(&mut self, bg: Rgb)

Set the background color for subsequent text.

Source

pub const fn reset_colors(&mut self)

Reset colors to defaults.

Source

pub fn append(&mut self, text: &str) -> AppendResult

Append text to the widget.

This automatically chooses between fast and slow path based on the text content and current state.

Source

pub fn render(&mut self, buffer: &mut Buffer)

Render the widget to a buffer.

This renders the visible content to the given buffer.

Source

pub fn write_fast_path( &self, result: AppendResult, text: &str, output: &mut Vec<u8>, )

Write fast-path output directly to an output buffer.

This generates ANSI sequences for direct terminal output, bypassing the buffer diffing.

Source

pub fn append_fast_into(&mut self, text: &str, output: &mut Vec<u8>) -> bool

Append text and perform fast-path generation if possible.

If the text was successfully appended via fast path (no wrap, no scroll), the ANSI sequence is written to output and true is returned. Otherwise returns false (caller should rely on standard cycle).

Source

pub fn push(&mut self, engine: &Engine, text: &str)

Push text to the stream with automatic optimization.

This is the recommended API for appending content. It handles all rendering decisions internally:

  • Fast Path: If the text fits on the current line without wrapping or scrolling, ANSI codes are emitted directly to the terminal for zero-latency display.
  • Slow Path: If wrapping or scrolling is required, the internal buffer is updated and the widget is marked dirty for the next frame.
§Example
// Just push. The engine handles the rest.
stream.push(&engine, "Hello ");
stream.push(&engine, "world!");
Source

pub const fn needs_redraw(&self) -> bool

Check if a full redraw is needed.

Source

pub fn dirty_rects(&self) -> &[Rect]

Get the dirty rectangles.

Source

pub const fn invalidate(&mut self)

Mark the widget for full redraw.

Source

pub fn clear(&mut self)

Clear all content.

Source

pub fn scroll_up(&mut self, lines: usize)

Scroll up by the given number of lines.

Source

pub const fn scroll_down(&mut self, lines: usize)

Scroll down by the given number of lines.

Source

pub const fn cursor_position(&self) -> (u16, u16)

Get the current cursor position within the widget.

Source

pub fn line_count(&self) -> usize

Get the number of lines in the buffer.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.