pub struct Parser<'alloc> { /* private fields */ }Expand description
SGR (Select Graphic Rendition) attribute parser.
SGR sequences are the syntax used to set styling attributes such as bold,
italic, underline, and colors for text in terminal emulators. For example,
you may be familiar with sequences like ESC[1;31m. The 1;31 is the SGR
attribute list.
The parser processes SGR parameters from CSI sequences (e.g., ESC[1;31m)
and returns individual text attributes like bold, italic, colors, etc. It
supports both semicolon (;) and colon (:) separators, possibly mixed,
and handles various color formats including 8-color, 16-color, 256-color,
X11 named colors, and RGB in multiple formats.
§Example
use libghostty_vt::sgr::{Parser, Attribute};
let mut parser = Parser::new().unwrap();
parser.set_params(&[1, 31], None).unwrap();
while let Some(attr) = parser.next().unwrap() {
match attr {
Attribute::Bold => println!("Bold enabled"),
Attribute::Fg8(color) => println!("Foreground color: {color:?}"),
_ => {},
}
}Implementations§
Source§impl<'alloc> Parser<'alloc>
impl<'alloc> Parser<'alloc>
Sourcepub fn new_with_alloc<'ctx: 'alloc, Ctx>(
alloc: &'alloc Allocator<'ctx, Ctx>,
) -> Result<Self>
pub fn new_with_alloc<'ctx: 'alloc, Ctx>( alloc: &'alloc Allocator<'ctx, Ctx>, ) -> Result<Self>
Create a new SGR parser with a custom allocator.
See the crate-level documentation regarding custom memory management and lifetimes.
Sourcepub fn set_params(
&mut self,
params: &[u16],
separators: Option<&[u8]>,
) -> Result<()>
pub fn set_params( &mut self, params: &[u16], separators: Option<&[u8]>, ) -> Result<()>
Set SGR parameters for parsing.
Parameters are the numeric values from a CSI SGR sequence (e.g., for ESC[1;31m, params
would be [1, 31]).
The separators slice optionally specifies the separator type for each parameter position.
Each byte should be either b';' for semicolon or b':' for colon.
This is needed for certain color formats that use colon separators (e.g., ESC[4:3m
for curly underline). Any invalid separator values are treated as semicolons.
If separators is None, all parameters are assumed to be semicolon-separated.
After calling this function, the parser is automatically reset and ready to iterate from the beginning.
§Panics
Panics if separators is not None and is not the same length as params.
Sourcepub fn next(&mut self) -> Result<Option<Attribute<'_>>>
pub fn next(&mut self) -> Result<Option<Attribute<'_>>>
Get the next SGR attribute.
Parses and returns the next attribute from the parameter list.
Call this function repeatedly until it returns None to process all
attributes in the sequence.
This cannot be expressed as a regular iterator since the returned attribute borrows memory from the parser directly.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset an SGR parser instance to the beginning of the parameter list.
Resets the parser’s iteration state without clearing the parameters.
After calling this, Parser::next will start from the beginning of the
parameter list again.