pub struct IncrementalRenderer { /* private fields */ }Implementations§
Source§impl IncrementalRenderer
impl IncrementalRenderer
Sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Creates an incremental renderer with a bounded viewport size.
A minimum viewport size of 1x1 is enforced.
The render origin defaults to terminal row 1, column 1.
Examples found in repository?
182fn main() -> Result<(), Box<dyn Error>> {
183 let args: Vec<String> = env::args().collect();
184 let options = match parse_args(&args) {
185 Ok(options) => options,
186 Err(err) => {
187 eprintln!("invalid arguments: {err}");
188 eprintln!();
189 print_usage();
190 return Ok(());
191 }
192 };
193
194 let grammar =
195 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
196 let source = fs::read(&options.source_path)?;
197 let theme = load_theme(&options.theme_name)?;
198 let mut highlighter = SpanHighlighter::new()?;
199 let mut renderer = IncrementalRenderer::new(options.width, options.height);
200 renderer.set_origin(options.origin_row, options.origin_col);
201 renderer.set_color_mode(options.color_mode);
202
203 if let Some(previous_source_path) = &options.previous_source_path {
204 let previous_source = fs::read(previous_source_path)?;
205 let _ = renderer.highlight_to_patch(&mut highlighter, &previous_source, grammar, &theme)?;
206 }
207
208 let patch = renderer.highlight_to_patch(&mut highlighter, &source, grammar, &theme)?;
209 print!("{patch}");
210 io::stdout().flush()?;
211
212 Ok(())
213}Sourcepub fn resize(&mut self, width: usize, height: usize)
pub fn resize(&mut self, width: usize, height: usize)
Resizes the viewport and clips cached state to the new bounds.
Sourcepub fn clear_state(&mut self)
pub fn clear_state(&mut self)
Clears all cached frame state for this renderer.
Sourcepub fn set_origin(&mut self, row: usize, col: usize)
pub fn set_origin(&mut self, row: usize, col: usize)
Sets the terminal origin used for generated CUP cursor positions.
The origin is 1-based terminal coordinates (row, col) in display cells.
Values lower than 1 are clamped to 1.
Examples found in repository?
182fn main() -> Result<(), Box<dyn Error>> {
183 let args: Vec<String> = env::args().collect();
184 let options = match parse_args(&args) {
185 Ok(options) => options,
186 Err(err) => {
187 eprintln!("invalid arguments: {err}");
188 eprintln!();
189 print_usage();
190 return Ok(());
191 }
192 };
193
194 let grammar =
195 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
196 let source = fs::read(&options.source_path)?;
197 let theme = load_theme(&options.theme_name)?;
198 let mut highlighter = SpanHighlighter::new()?;
199 let mut renderer = IncrementalRenderer::new(options.width, options.height);
200 renderer.set_origin(options.origin_row, options.origin_col);
201 renderer.set_color_mode(options.color_mode);
202
203 if let Some(previous_source_path) = &options.previous_source_path {
204 let previous_source = fs::read(previous_source_path)?;
205 let _ = renderer.highlight_to_patch(&mut highlighter, &previous_source, grammar, &theme)?;
206 }
207
208 let patch = renderer.highlight_to_patch(&mut highlighter, &source, grammar, &theme)?;
209 print!("{patch}");
210 io::stdout().flush()?;
211
212 Ok(())
213}Sourcepub fn set_color_mode(&mut self, color_mode: ColorMode)
pub fn set_color_mode(&mut self, color_mode: ColorMode)
Sets the ANSI color mode used by this renderer.
Examples found in repository?
182fn main() -> Result<(), Box<dyn Error>> {
183 let args: Vec<String> = env::args().collect();
184 let options = match parse_args(&args) {
185 Ok(options) => options,
186 Err(err) => {
187 eprintln!("invalid arguments: {err}");
188 eprintln!();
189 print_usage();
190 return Ok(());
191 }
192 };
193
194 let grammar =
195 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
196 let source = fs::read(&options.source_path)?;
197 let theme = load_theme(&options.theme_name)?;
198 let mut highlighter = SpanHighlighter::new()?;
199 let mut renderer = IncrementalRenderer::new(options.width, options.height);
200 renderer.set_origin(options.origin_row, options.origin_col);
201 renderer.set_color_mode(options.color_mode);
202
203 if let Some(previous_source_path) = &options.previous_source_path {
204 let previous_source = fs::read(previous_source_path)?;
205 let _ = renderer.highlight_to_patch(&mut highlighter, &previous_source, grammar, &theme)?;
206 }
207
208 let patch = renderer.highlight_to_patch(&mut highlighter, &source, grammar, &theme)?;
209 print!("{patch}");
210 io::stdout().flush()?;
211
212 Ok(())
213}Sourcepub fn color_mode(&self) -> ColorMode
pub fn color_mode(&self) -> ColorMode
Returns the current ANSI color mode.
Sourcepub fn render_patch(
&mut self,
source: &[u8],
spans: &[StyledSpan],
) -> Result<String, RenderError>
pub fn render_patch( &mut self, source: &[u8], spans: &[StyledSpan], ) -> Result<String, RenderError>
Renders only the VT patch from the cached frame to source.
The method validates input spans, projects them to styled terminal cells, diffs against previous state, and returns only changed cursor/style output.
§Errors
Returns an error when spans are out of bounds, unsorted, or overlapping.
Sourcepub fn highlight_to_patch(
&mut self,
highlighter: &mut SpanHighlighter,
source: &[u8],
flavor: Grammar,
theme: &Theme,
) -> Result<String, RenderError>
pub fn highlight_to_patch( &mut self, highlighter: &mut SpanHighlighter, source: &[u8], flavor: Grammar, theme: &Theme, ) -> Result<String, RenderError>
Runs highlight + theme resolution + incremental diff in one call.
§Errors
Returns an error if highlighting fails or spans fail validation.
Examples found in repository?
182fn main() -> Result<(), Box<dyn Error>> {
183 let args: Vec<String> = env::args().collect();
184 let options = match parse_args(&args) {
185 Ok(options) => options,
186 Err(err) => {
187 eprintln!("invalid arguments: {err}");
188 eprintln!();
189 print_usage();
190 return Ok(());
191 }
192 };
193
194 let grammar =
195 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
196 let source = fs::read(&options.source_path)?;
197 let theme = load_theme(&options.theme_name)?;
198 let mut highlighter = SpanHighlighter::new()?;
199 let mut renderer = IncrementalRenderer::new(options.width, options.height);
200 renderer.set_origin(options.origin_row, options.origin_col);
201 renderer.set_color_mode(options.color_mode);
202
203 if let Some(previous_source_path) = &options.previous_source_path {
204 let previous_source = fs::read(previous_source_path)?;
205 let _ = renderer.highlight_to_patch(&mut highlighter, &previous_source, grammar, &theme)?;
206 }
207
208 let patch = renderer.highlight_to_patch(&mut highlighter, &source, grammar, &theme)?;
209 print!("{patch}");
210 io::stdout().flush()?;
211
212 Ok(())
213}Trait Implementations§
Source§impl Clone for IncrementalRenderer
impl Clone for IncrementalRenderer
Source§fn clone(&self) -> IncrementalRenderer
fn clone(&self) -> IncrementalRenderer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more