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
impl StreamWidget
Sourcepub fn with_config(bounds: Rect, config: StreamConfig) -> Self
pub fn with_config(bounds: Rect, config: StreamConfig) -> Self
Create a new stream widget with custom configuration.
Sourcepub fn set_bounds(&mut self, bounds: Rect)
pub fn set_bounds(&mut self, bounds: Rect)
Set new bounds for the widget.
If the width changes, content will be rewrapped to fit the new width.
Sourcepub const fn reset_colors(&mut self)
pub const fn reset_colors(&mut self)
Reset colors to defaults.
Sourcepub fn append(&mut self, text: &str) -> AppendResult
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.
Sourcepub fn render(&mut self, buffer: &mut Buffer)
pub fn render(&mut self, buffer: &mut Buffer)
Render the widget to a buffer.
This renders the visible content to the given buffer.
Sourcepub fn write_fast_path(
&self,
result: AppendResult,
text: &str,
output: &mut Vec<u8>,
)
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.
Sourcepub fn append_fast_into(&mut self, text: &str, output: &mut Vec<u8>) -> bool
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).
Sourcepub fn push(&mut self, engine: &Engine, text: &str)
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!");Sourcepub const fn needs_redraw(&self) -> bool
pub const fn needs_redraw(&self) -> bool
Check if a full redraw is needed.
Sourcepub fn dirty_rects(&self) -> &[Rect]
pub fn dirty_rects(&self) -> &[Rect]
Get the dirty rectangles.
Sourcepub const fn invalidate(&mut self)
pub const fn invalidate(&mut self)
Mark the widget for full redraw.
Sourcepub const fn scroll_down(&mut self, lines: usize)
pub const fn scroll_down(&mut self, lines: usize)
Scroll down by the given number of lines.
Sourcepub const fn cursor_position(&self) -> (u16, u16)
pub const fn cursor_position(&self) -> (u16, u16)
Get the current cursor position within the widget.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Get the number of lines in the buffer.