Skip to main content

Parser

Struct Parser 

Source
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>

Source

pub fn new() -> Result<Self>

Create a new SGR parser.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'alloc> Debug for Parser<'alloc>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Parser<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'alloc> Freeze for Parser<'alloc>

§

impl<'alloc> RefUnwindSafe for Parser<'alloc>

§

impl<'alloc> !Send for Parser<'alloc>

§

impl<'alloc> !Sync for Parser<'alloc>

§

impl<'alloc> Unpin for Parser<'alloc>

§

impl<'alloc> UnsafeUnpin for Parser<'alloc>

§

impl<'alloc> UnwindSafe for Parser<'alloc>

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.