|
| 1 | +#include <assert.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <ghostty/vt.h> |
| 4 | + |
| 5 | +int main() { |
| 6 | + // Create parser |
| 7 | + GhosttySgrParser parser; |
| 8 | + GhosttyResult result = ghostty_sgr_new(NULL, &parser); |
| 9 | + assert(result == GHOSTTY_SUCCESS); |
| 10 | + |
| 11 | + // Parse a complex SGR sequence from Kakoune |
| 12 | + // This corresponds to the escape sequence: |
| 13 | + // ESC[4:3;38;2;51;51;51;48;2;170;170;170;58;2;255;97;136m |
| 14 | + // |
| 15 | + // Breaking down the sequence: |
| 16 | + // - 4:3 = curly underline (colon-separated sub-parameters) |
| 17 | + // - 38;2;51;51;51 = foreground RGB color (51, 51, 51) - dark gray |
| 18 | + // - 48;2;170;170;170 = background RGB color (170, 170, 170) - light gray |
| 19 | + // - 58;2;255;97;136 = underline RGB color (255, 97, 136) - pink |
| 20 | + uint16_t params[] = {4, 3, 38, 2, 51, 51, 51, 48, 2, 170, 170, 170, 58, 2, 255, 97, 136}; |
| 21 | + |
| 22 | + // Separator array: ':' at position 0 (between 4 and 3), ';' elsewhere |
| 23 | + char separators[] = ";;;;;;;;;;;;;;;;"; |
| 24 | + separators[0] = ':'; |
| 25 | + |
| 26 | + result = ghostty_sgr_set_params(parser, params, separators, sizeof(params) / sizeof(params[0])); |
| 27 | + assert(result == GHOSTTY_SUCCESS); |
| 28 | + |
| 29 | + printf("Parsing Kakoune SGR sequence:\n"); |
| 30 | + printf("ESC[4:3;38;2;51;51;51;48;2;170;170;170;58;2;255;97;136m\n\n"); |
| 31 | + |
| 32 | + // Iterate through attributes |
| 33 | + GhosttySgrAttribute attr; |
| 34 | + int count = 0; |
| 35 | + while (ghostty_sgr_next(parser, &attr)) { |
| 36 | + count++; |
| 37 | + printf("Attribute %d: ", count); |
| 38 | + |
| 39 | + switch (attr.tag) { |
| 40 | + case GHOSTTY_SGR_ATTR_UNDERLINE: |
| 41 | + printf("Underline style = "); |
| 42 | + switch (attr.value.underline) { |
| 43 | + case GHOSTTY_SGR_UNDERLINE_NONE: |
| 44 | + printf("none\n"); |
| 45 | + break; |
| 46 | + case GHOSTTY_SGR_UNDERLINE_SINGLE: |
| 47 | + printf("single\n"); |
| 48 | + break; |
| 49 | + case GHOSTTY_SGR_UNDERLINE_DOUBLE: |
| 50 | + printf("double\n"); |
| 51 | + break; |
| 52 | + case GHOSTTY_SGR_UNDERLINE_CURLY: |
| 53 | + printf("curly\n"); |
| 54 | + break; |
| 55 | + case GHOSTTY_SGR_UNDERLINE_DOTTED: |
| 56 | + printf("dotted\n"); |
| 57 | + break; |
| 58 | + case GHOSTTY_SGR_UNDERLINE_DASHED: |
| 59 | + printf("dashed\n"); |
| 60 | + break; |
| 61 | + default: |
| 62 | + printf("unknown (%d)\n", attr.value.underline); |
| 63 | + break; |
| 64 | + } |
| 65 | + break; |
| 66 | + |
| 67 | + case GHOSTTY_SGR_ATTR_DIRECT_COLOR_FG: |
| 68 | + printf("Foreground RGB = (%d, %d, %d)\n", |
| 69 | + attr.value.direct_color_fg.r, |
| 70 | + attr.value.direct_color_fg.g, |
| 71 | + attr.value.direct_color_fg.b); |
| 72 | + break; |
| 73 | + |
| 74 | + case GHOSTTY_SGR_ATTR_DIRECT_COLOR_BG: |
| 75 | + printf("Background RGB = (%d, %d, %d)\n", |
| 76 | + attr.value.direct_color_bg.r, |
| 77 | + attr.value.direct_color_bg.g, |
| 78 | + attr.value.direct_color_bg.b); |
| 79 | + break; |
| 80 | + |
| 81 | + case GHOSTTY_SGR_ATTR_UNDERLINE_COLOR: |
| 82 | + printf("Underline color RGB = (%d, %d, %d)\n", |
| 83 | + attr.value.underline_color.r, |
| 84 | + attr.value.underline_color.g, |
| 85 | + attr.value.underline_color.b); |
| 86 | + break; |
| 87 | + |
| 88 | + case GHOSTTY_SGR_ATTR_FG_8: |
| 89 | + printf("Foreground 8-color = %d\n", attr.value.fg_8); |
| 90 | + break; |
| 91 | + |
| 92 | + case GHOSTTY_SGR_ATTR_BG_8: |
| 93 | + printf("Background 8-color = %d\n", attr.value.bg_8); |
| 94 | + break; |
| 95 | + |
| 96 | + case GHOSTTY_SGR_ATTR_FG_256: |
| 97 | + printf("Foreground 256-color = %d\n", attr.value.fg_256); |
| 98 | + break; |
| 99 | + |
| 100 | + case GHOSTTY_SGR_ATTR_BG_256: |
| 101 | + printf("Background 256-color = %d\n", attr.value.bg_256); |
| 102 | + break; |
| 103 | + |
| 104 | + case GHOSTTY_SGR_ATTR_BOLD: |
| 105 | + printf("Bold\n"); |
| 106 | + break; |
| 107 | + |
| 108 | + case GHOSTTY_SGR_ATTR_ITALIC: |
| 109 | + printf("Italic\n"); |
| 110 | + break; |
| 111 | + |
| 112 | + case GHOSTTY_SGR_ATTR_UNSET: |
| 113 | + printf("Reset all attributes\n"); |
| 114 | + break; |
| 115 | + |
| 116 | + case GHOSTTY_SGR_ATTR_UNKNOWN: |
| 117 | + printf("Unknown attribute\n"); |
| 118 | + break; |
| 119 | + |
| 120 | + default: |
| 121 | + printf("Other attribute (tag=%d)\n", attr.tag); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + printf("\nTotal attributes parsed: %d\n", count); |
| 127 | + |
| 128 | + // Cleanup |
| 129 | + ghostty_sgr_free(parser); |
| 130 | + return 0; |
| 131 | +} |
0 commit comments