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
impl StreamRenderer
Sourcepub fn new(width: usize, theme_mode: ThemeMode) -> Self
pub fn new(width: usize, theme_mode: ThemeMode) -> Self
Create a new stream renderer.
width: terminal width in columns (e.g. from theterminal_sizecrate).theme_mode: controls syntax highlighting theme for code blocks.
Sourcepub fn with_code_theme(self, theme: &str) -> Self
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.
Sourcepub fn with_ascii_table_borders(self, ascii: bool) -> Self
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).
Sourcepub fn push(&mut self, text: &str) -> Vec<String>
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.
Sourcepub fn flush_remaining(&mut self) -> Vec<String>
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.