![]() |
libghostty
|
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.
Typedefs | |
| typedef uint8_t | GhosttyColorPaletteIndex |
| typedef struct GhosttySgrParserImpl * | GhosttySgrParser |
Enumerations | |
| enum | GhosttySgrAttributeTag |
| enum | GhosttySgrUnderline |
Functions | |
| GHOSTTY_API void | ghostty_color_rgb_get (GhosttyColorRgb color, uint8_t *r, uint8_t *g, uint8_t *b) |
| GHOSTTY_API GhosttyResult | ghostty_sgr_new (const GhosttyAllocator *allocator, GhosttySgrParser *parser) |
| GHOSTTY_API void | ghostty_sgr_free (GhosttySgrParser parser) |
| GHOSTTY_API void | ghostty_sgr_reset (GhosttySgrParser parser) |
| GHOSTTY_API GhosttyResult | ghostty_sgr_set_params (GhosttySgrParser parser, const uint16_t *params, const char *separators, size_t len) |
| GHOSTTY_API bool | ghostty_sgr_next (GhosttySgrParser parser, GhosttySgrAttribute *attr) |
| GHOSTTY_API size_t | ghostty_sgr_unknown_full (GhosttySgrUnknown unknown, const uint16_t **ptr) |
| GHOSTTY_API size_t | ghostty_sgr_unknown_partial (GhosttySgrUnknown unknown, const uint16_t **ptr) |
| GHOSTTY_API GhosttySgrAttributeTag | ghostty_sgr_attribute_tag (GhosttySgrAttribute attr) |
| GHOSTTY_API GhosttySgrAttributeValue * | ghostty_sgr_attribute_value (GhosttySgrAttribute *attr) |
Macros | |
| #define | GHOSTTY_COLOR_NAMED_BLACK 0 |
| #define | GHOSTTY_COLOR_NAMED_RED 1 |
| #define | GHOSTTY_COLOR_NAMED_GREEN 2 |
| #define | GHOSTTY_COLOR_NAMED_YELLOW 3 |
| #define | GHOSTTY_COLOR_NAMED_BLUE 4 |
| #define | GHOSTTY_COLOR_NAMED_MAGENTA 5 |
| #define | GHOSTTY_COLOR_NAMED_CYAN 6 |
| #define | GHOSTTY_COLOR_NAMED_WHITE 7 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_BLACK 8 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_RED 9 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_GREEN 10 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_YELLOW 11 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_BLUE 12 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_MAGENTA 13 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_CYAN 14 |
| #define | GHOSTTY_COLOR_NAMED_BRIGHT_WHITE 15 |
Data Structures | |
| struct | GhosttyColorRgb |
| struct | GhosttySgrUnknown |
| union | GhosttySgrAttributeValue |
| struct | GhosttySgrAttribute |
| typedef uint8_t GhosttyColorPaletteIndex |
| typedef struct GhosttySgrParserImpl* GhosttySgrParser |
Opaque handle to an SGR parser instance.
This handle represents an SGR (Select Graphic Rendition) parser that can be used to parse SGR sequences and extract individual text attributes.
| enum GhosttySgrUnderline |
| GHOSTTY_API void ghostty_color_rgb_get | ( | GhosttyColorRgb | color, |
| uint8_t * | r, | ||
| uint8_t * | g, | ||
| uint8_t * | b ) |
Get the RGB color components.
This function extracts the individual red, green, and blue components from a GhosttyColorRgb value. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| color | The RGB color value |
| r | Pointer to store the red component (0-255) |
| g | Pointer to store the green component (0-255) |
| b | Pointer to store the blue component (0-255) |
| GHOSTTY_API GhosttySgrAttributeTag ghostty_sgr_attribute_tag | ( | GhosttySgrAttribute | attr | ) |
Get the tag from an SGR attribute.
This function extracts the tag that identifies which type of attribute this is. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| attr | The SGR attribute |
| GHOSTTY_API GhosttySgrAttributeValue * ghostty_sgr_attribute_value | ( | GhosttySgrAttribute * | attr | ) |
Get the value from an SGR attribute.
This function returns a pointer to the value union from an SGR attribute. Use the tag to determine which field of the union is valid. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| attr | Pointer to the SGR attribute |
| GHOSTTY_API void ghostty_sgr_free | ( | GhosttySgrParser | parser | ) |
Free an SGR parser instance.
Releases all resources associated with the SGR parser. After this call, the parser handle becomes invalid and must not be used. This includes any attributes previously returned by ghostty_sgr_next().
| parser | The parser handle to free (may be NULL) |
| GHOSTTY_API GhosttyResult ghostty_sgr_new | ( | const GhosttyAllocator * | allocator, |
| GhosttySgrParser * | parser ) |
Create a new SGR parser instance.
Creates a new SGR (Select Graphic Rendition) parser using the provided allocator. The parser must be freed using ghostty_sgr_free() when no longer needed.
| Memory Management | Pointer to the allocator to use for memory management, or NULL to use the default allocator |
| parser | Pointer to store the created parser handle |
| GHOSTTY_API bool ghostty_sgr_next | ( | GhosttySgrParser | parser, |
| GhosttySgrAttribute * | attr ) |
Get the next SGR attribute.
Parses and returns the next attribute from the parameter list. Call this function repeatedly until it returns false to process all attributes in the sequence.
| parser | The parser handle, must not be NULL |
| attr | Pointer to store the next attribute |
| GHOSTTY_API void ghostty_sgr_reset | ( | GhosttySgrParser | parser | ) |
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, ghostty_sgr_next() will start from the beginning of the parameter list again.
| parser | The parser handle to reset, must not be NULL |
| GHOSTTY_API GhosttyResult ghostty_sgr_set_params | ( | GhosttySgrParser | parser, |
| const uint16_t * | params, | ||
| const char * | separators, | ||
| size_t | len ) |
Set SGR parameters for parsing.
Sets the SGR parameter list to parse. Parameters are the numeric values from a CSI SGR sequence (e.g., for ESC[1;31m, params would be {1, 31}).
The separators array optionally specifies the separator type for each parameter position. Each byte should be either ';' for semicolon or ':' 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. The separators array must have the same length as the params array, if it is not NULL.
If separators is NULL, all parameters are assumed to be semicolon-separated.
This function makes an internal copy of the parameter and separator data, so the caller can safely free or modify the input arrays after this call.
After calling this function, the parser is automatically reset and ready to iterate from the beginning.
| parser | The parser handle, must not be NULL |
| params | Array of SGR parameter values |
| separators | Optional array of separator characters (';' or ':'), or NULL |
| len | Number of parameters (and separators if provided) |
| GHOSTTY_API size_t ghostty_sgr_unknown_full | ( | GhosttySgrUnknown | unknown, |
| const uint16_t ** | ptr ) |
Get the full parameter list from an unknown SGR attribute.
This function retrieves the full parameter list that was provided to the parser when an unknown attribute was encountered. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| unknown | The unknown attribute data |
| ptr | Pointer to store the pointer to the parameter array (may be NULL) |
| GHOSTTY_API size_t ghostty_sgr_unknown_partial | ( | GhosttySgrUnknown | unknown, |
| const uint16_t ** | ptr ) |
Get the partial parameter list from an unknown SGR attribute.
This function retrieves the partial parameter list where parsing stopped when an unknown attribute was encountered. Primarily useful in WebAssembly environments where accessing struct fields directly is difficult.
| unknown | The unknown attribute data |
| ptr | Pointer to store the pointer to the parameter array (may be NULL) |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_BLACK 8 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_BLUE 12 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_CYAN 14 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_GREEN 10 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_MAGENTA 13 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_RED 9 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_WHITE 15 |
| #define GHOSTTY_COLOR_NAMED_BRIGHT_YELLOW 11 |