This example demonstrates how to use the SGR parser to parse terminal styling sequences and extract text attributes like colors and underline styles.
#include <assert.h>
#include <stdio.h>
void basic_example() {
uint16_t params[] = {1, 31};
case GHOSTTY_SGR_ATTR_BOLD:
printf("Bold enabled\n");
break;
case GHOSTTY_SGR_ATTR_FG_8:
printf(
"Foreground color: %d\n", attr.
value.
fg_8);
break;
default:
break;
}
}
}
void advanced_example() {
uint16_t params[] = {4, 3, 38, 2, 51, 51, 51, 48, 2, 170, 170, 170, 58, 2, 255, 97, 136};
char separators[] = ";;;;;;;;;;;;;;;;";
separators[0] = ':';
printf("\nParsing Kakoune SGR sequence:\n");
printf("ESC[4:3;38;2;51;51;51;48;2;170;170;170;58;2;255;97;136m\n\n");
int count = 0;
count++;
printf("Attribute %d: ", count);
switch (attr.tag) {
case GHOSTTY_SGR_ATTR_UNDERLINE:
printf("Underline style = ");
switch (attr.value.
underline) {
case GHOSTTY_SGR_UNDERLINE_NONE:
printf("none\n");
break;
case GHOSTTY_SGR_UNDERLINE_SINGLE:
printf("single\n");
break;
case GHOSTTY_SGR_UNDERLINE_DOUBLE:
printf("double\n");
break;
case GHOSTTY_SGR_UNDERLINE_CURLY:
printf("curly\n");
break;
case GHOSTTY_SGR_UNDERLINE_DOTTED:
printf("dotted\n");
break;
case GHOSTTY_SGR_UNDERLINE_DASHED:
printf("dashed\n");
break;
default:
printf("unknown (%d)\n", attr.value.underline);
break;
}
break;
case GHOSTTY_SGR_ATTR_DIRECT_COLOR_FG:
printf("Foreground RGB = (%d, %d, %d)\n",
attr.value.
direct_color_fg.
r,
attr.value.direct_color_fg.
g,
attr.value.direct_color_fg.
b);
break;
case GHOSTTY_SGR_ATTR_DIRECT_COLOR_BG:
printf("Background RGB = (%d, %d, %d)\n",
attr.value.
direct_color_bg.
r,
attr.value.direct_color_bg.
g,
attr.value.direct_color_bg.
b);
break;
case GHOSTTY_SGR_ATTR_UNDERLINE_COLOR:
printf("Underline color RGB = (%d, %d, %d)\n",
attr.value.
underline_color.
r,
attr.value.underline_color.
g,
attr.value.underline_color.
b);
break;
case GHOSTTY_SGR_ATTR_FG_8:
printf("Foreground 8-color = %d\n", attr.value.fg_8);
break;
case GHOSTTY_SGR_ATTR_BG_8:
printf(
"Background 8-color = %d\n", attr.value.
bg_8);
break;
case GHOSTTY_SGR_ATTR_FG_256:
printf(
"Foreground 256-color = %d\n", attr.value.
fg_256);
break;
case GHOSTTY_SGR_ATTR_BG_256:
printf(
"Background 256-color = %d\n", attr.value.
bg_256);
break;
case GHOSTTY_SGR_ATTR_BOLD:
printf("Bold\n");
break;
case GHOSTTY_SGR_ATTR_ITALIC:
printf("Italic\n");
break;
case GHOSTTY_SGR_ATTR_UNSET:
printf("Reset all attributes\n");
break;
case GHOSTTY_SGR_ATTR_UNKNOWN:
printf("Unknown attribute\n");
break;
default:
printf("Other attribute (tag=%d)\n", attr.tag);
break;
}
}
printf("\nTotal attributes parsed: %d\n", count);
}
int main() {
basic_example();
advanced_example();
return 0;
}
GHOSTTY_API void ghostty_sgr_free(GhosttySgrParser parser)
struct GhosttySgrParserImpl * GhosttySgrParser
GHOSTTY_API GhosttyResult ghostty_sgr_set_params(GhosttySgrParser parser, const uint16_t *params, const char *separators, size_t len)
GHOSTTY_API GhosttyResult ghostty_sgr_new(const GhosttyAllocator *allocator, GhosttySgrParser *parser)
GHOSTTY_API bool ghostty_sgr_next(GhosttySgrParser parser, GhosttySgrAttribute *attr)