Skip to main content

StreamRenderer

Struct StreamRenderer 

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

Incrementally renders markdown text chunks as they arrive.

StreamRenderer is designed for streaming LLM responses: as the model generates markdown text chunk by chunk, this renderer produces complete, renderable lines as soon as enough input has been buffered to form a complete markdown element (e.g. a paragraph ended by a blank line, a complete table, a closed fenced code block).

§Examples

use smart_markdown::{StreamRenderer, ThemeMode, is_light_terminal};

let width = terminal_size::terminal_size()
    .map(|(w, _)| w.0 as usize)
    .unwrap_or(80);
let theme = if is_light_terminal() { ThemeMode::Light } else { ThemeMode::Dark };
let mut sr = StreamRenderer::new(width, theme)
    .with_ascii_table_borders(true)
    .with_code_theme("base16-ocean.dark");

// Feed chunks as they arrive from the LLM
for line in sr.push("# Hello\n\n") {
    println!("{line}");
}
for line in sr.push("this is **bold** text") {
    println!("{line}");
}

// Flush anything still buffered at the end
for line in sr.flush_remaining() {
    println!("{line}");
}

Implementations§

Source§

impl StreamRenderer

Source

pub fn new(width: usize, theme_mode: ThemeMode) -> Self

Create a new stream renderer.

  • width: terminal width in columns (e.g. from the terminal_size crate).
  • theme_mode: controls syntax highlighting theme for code blocks.
Source

pub fn with_code_theme(self, theme: &str) -> Self

Set a custom syntax highlighting theme by name.

See crate::highlight::list_themes for available theme names.

Source

pub fn with_ascii_table_borders(self, ascii: bool) -> Self

Use ASCII-only table borders (+, -, |) instead of Unicode box-drawing characters (, , , etc.).

Useful for terminals where Unicode box-drawing renders poorly (e.g. light-background themes without proper color inversion).

Source

pub fn push(&mut self, text: &str) -> Vec<String>

Push additional text chunks.

Returns rendered complete lines as they become available. Incomplete markdown (partial fenced blocks, tables, paragraphs) is buffered internally.

Source

pub fn flush_remaining(&mut self) -> Vec<String>

Flush any remaining buffered content and return the final lines.

Call this once at the end of the stream to emit any markdown that hasn’t been completed by a blank line or structural close.

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.