pub struct StreamLineRenderer { /* private fields */ }Expand description
Incremental renderer for a single mutable line without terminal width assumptions.
This renderer avoids absolute cursor positioning. It assumes each emitted patch is written to the same terminal line and the cursor remains at the end of the previously rendered line.
Implementations§
Source§impl StreamLineRenderer
impl StreamLineRenderer
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a line renderer with truecolor output.
Examples found in repository?
examples/stream_line_bridge.rs (line 137)
120fn main() -> Result<(), Box<dyn Error>> {
121 let args: Vec<String> = env::args().collect();
122 let options = match parse_args(&args) {
123 Ok(options) => options,
124 Err(err) => {
125 eprintln!("invalid arguments: {err}");
126 eprintln!();
127 print_usage();
128 return Ok(());
129 }
130 };
131
132 let grammar =
133 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
134 let source = fs::read(&options.source_path)?;
135 let theme = load_theme(&options.theme_name)?;
136 let mut highlighter = SpanHighlighter::new()?;
137 let mut renderer = StreamLineRenderer::new();
138 renderer.set_color_mode(options.color_mode);
139
140 if let Some(previous_source_path) = &options.previous_source_path {
141 let previous_source = fs::read(previous_source_path)?;
142 let _ = renderer.highlight_line_to_patch(
143 &mut highlighter,
144 &previous_source,
145 grammar,
146 &theme,
147 )?;
148 }
149
150 let patch = renderer.highlight_line_to_patch(&mut highlighter, &source, grammar, &theme)?;
151 print!("{patch}");
152 io::stdout().flush()?;
153
154 Ok(())
155}Sourcepub fn clear_state(&mut self)
pub fn clear_state(&mut self)
Clears prior line state.
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?
examples/stream_line_bridge.rs (line 138)
120fn main() -> Result<(), Box<dyn Error>> {
121 let args: Vec<String> = env::args().collect();
122 let options = match parse_args(&args) {
123 Ok(options) => options,
124 Err(err) => {
125 eprintln!("invalid arguments: {err}");
126 eprintln!();
127 print_usage();
128 return Ok(());
129 }
130 };
131
132 let grammar =
133 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
134 let source = fs::read(&options.source_path)?;
135 let theme = load_theme(&options.theme_name)?;
136 let mut highlighter = SpanHighlighter::new()?;
137 let mut renderer = StreamLineRenderer::new();
138 renderer.set_color_mode(options.color_mode);
139
140 if let Some(previous_source_path) = &options.previous_source_path {
141 let previous_source = fs::read(previous_source_path)?;
142 let _ = renderer.highlight_line_to_patch(
143 &mut highlighter,
144 &previous_source,
145 grammar,
146 &theme,
147 )?;
148 }
149
150 let patch = renderer.highlight_line_to_patch(&mut highlighter, &source, grammar, &theme)?;
151 print!("{patch}");
152 io::stdout().flush()?;
153
154 Ok(())
155}Sourcepub fn color_mode(&self) -> ColorMode
pub fn color_mode(&self) -> ColorMode
Returns the current ANSI color mode.
Sourcepub fn render_line_patch(
&mut self,
source: &[u8],
spans: &[StyledSpan],
) -> Result<String, RenderError>
pub fn render_line_patch( &mut self, source: &[u8], spans: &[StyledSpan], ) -> Result<String, RenderError>
Renders a width-independent patch for a single line.
§Errors
Returns an error when spans are invalid or input contains a newline.
Sourcepub fn highlight_line_to_patch(
&mut self,
highlighter: &mut SpanHighlighter,
source: &[u8],
flavor: Grammar,
theme: &Theme,
) -> Result<String, RenderError>
pub fn highlight_line_to_patch( &mut self, highlighter: &mut SpanHighlighter, source: &[u8], flavor: Grammar, theme: &Theme, ) -> Result<String, RenderError>
Runs highlight + theme resolution + stream-safe single-line diff.
§Errors
Returns an error if highlighting fails, spans are invalid, or input has newlines.
Examples found in repository?
examples/stream_line_bridge.rs (lines 142-147)
120fn main() -> Result<(), Box<dyn Error>> {
121 let args: Vec<String> = env::args().collect();
122 let options = match parse_args(&args) {
123 Ok(options) => options,
124 Err(err) => {
125 eprintln!("invalid arguments: {err}");
126 eprintln!();
127 print_usage();
128 return Ok(());
129 }
130 };
131
132 let grammar =
133 parse_grammar(&options.grammar_name).map_err(|msg| format!("invalid grammar: {msg}"))?;
134 let source = fs::read(&options.source_path)?;
135 let theme = load_theme(&options.theme_name)?;
136 let mut highlighter = SpanHighlighter::new()?;
137 let mut renderer = StreamLineRenderer::new();
138 renderer.set_color_mode(options.color_mode);
139
140 if let Some(previous_source_path) = &options.previous_source_path {
141 let previous_source = fs::read(previous_source_path)?;
142 let _ = renderer.highlight_line_to_patch(
143 &mut highlighter,
144 &previous_source,
145 grammar,
146 &theme,
147 )?;
148 }
149
150 let patch = renderer.highlight_line_to_patch(&mut highlighter, &source, grammar, &theme)?;
151 print!("{patch}");
152 io::stdout().flush()?;
153
154 Ok(())
155}Trait Implementations§
Source§impl Clone for StreamLineRenderer
impl Clone for StreamLineRenderer
Source§fn clone(&self) -> StreamLineRenderer
fn clone(&self) -> StreamLineRenderer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for StreamLineRenderer
impl Debug for StreamLineRenderer
Source§impl Default for StreamLineRenderer
impl Default for StreamLineRenderer
Source§fn default() -> StreamLineRenderer
fn default() -> StreamLineRenderer
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for StreamLineRenderer
impl RefUnwindSafe for StreamLineRenderer
impl Send for StreamLineRenderer
impl Sync for StreamLineRenderer
impl Unpin for StreamLineRenderer
impl UnsafeUnpin for StreamLineRenderer
impl UnwindSafe for StreamLineRenderer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more