feat(vt-capi): port the libghostty-vt C API
Ports ZIG/src/lib_vt.zig and terminal/c/* as crates/ghostty-vt-capi (cdylib + staticlib + rlib, soname libghostty-vt.so) implementing the unchanged include/ghostty/vt.h + vt/*.h ABI: OSC parser, SGR parser, key events and key encoding, paste safety, colors, result codes - all 38 native symbols, plus the ghostty_wasm_* helpers on wasm32. Verification: - tests/abi.rs generates a C probe from the headers, compiled with cc, that checks sizeof/alignof/offsetof of every struct/union, every enumerator and #define against Rust, and links every declared symbol; nm -D exports equal the declared functions. - Ghostty's example/c-vt* programs compile unchanged against the Rust library (shared and static) and produce the expected output. - The wasm32 build runs Ghostty's wasm-key-encode/wasm-sgr example scripts unmodified under node (upstream's wasm-sgr tag table is stale vs sgr.h; a regenerated copy checks full output). Test Plan: - cargo test -p ghostty-vt-capi (22 unit + 2 ABI + 5 C + 3 wasm tests)
This commit is contained in:
1 parent
03e6e33686
commit
0915f7b935
35 files changed
+6911
No files matched your search
@@ -0,0 +1,59 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ghostty/vt.h>
|
||||
|
||||
int main() {
|
||||
GhosttyKeyEncoder encoder;
|
||||
GhosttyResult result = ghostty_key_encoder_new(NULL, &encoder);
|
||||
assert(result == GHOSTTY_SUCCESS);
|
||||
|
||||
// Set kitty flags with all features enabled
|
||||
ghostty_key_encoder_setopt(encoder, GHOSTTY_KEY_ENCODER_OPT_KITTY_FLAGS, &(uint8_t){GHOSTTY_KITTY_KEY_ALL});
|
||||
|
||||
// Create key event
|
||||
GhosttyKeyEvent event;
|
||||
result = ghostty_key_event_new(NULL, &event);
|
||||
assert(result == GHOSTTY_SUCCESS);
|
||||
ghostty_key_event_set_action(event, GHOSTTY_KEY_ACTION_RELEASE);
|
||||
ghostty_key_event_set_key(event, GHOSTTY_KEY_CONTROL_LEFT);
|
||||
ghostty_key_event_set_mods(event, GHOSTTY_MODS_CTRL);
|
||||
printf("Encoding event: left ctrl release with all Kitty flags enabled\n");
|
||||
|
||||
// Optionally, encode with null buffer to get required size. You can
|
||||
// skip this step and provide a sufficiently large buffer directly.
|
||||
// If there isn't enoug hspace, the function will return an out of memory
|
||||
// error.
|
||||
size_t required = 0;
|
||||
result = ghostty_key_encoder_encode(encoder, event, NULL, 0, &required);
|
||||
assert(result == GHOSTTY_OUT_OF_MEMORY);
|
||||
printf("Required buffer size: %zu bytes\n", required);
|
||||
|
||||
// Encode the key event. We don't use our required size above because
|
||||
// that was just an example; we know 128 bytes is enough.
|
||||
char buf[128];
|
||||
size_t written = 0;
|
||||
result = ghostty_key_encoder_encode(encoder, event, buf, sizeof(buf), &written);
|
||||
assert(result == GHOSTTY_SUCCESS);
|
||||
printf("Encoded %zu bytes\n", written);
|
||||
|
||||
// Print the encoded sequence (hex and string)
|
||||
printf("Hex: ");
|
||||
for (size_t i = 0; i < written; i++) printf("%02x ", (unsigned char)buf[i]);
|
||||
printf("\n");
|
||||
|
||||
printf("String: ");
|
||||
for (size_t i = 0; i < written; i++) {
|
||||
if (buf[i] == 0x1b) {
|
||||
printf("\\x1b");
|
||||
} else {
|
||||
printf("%c", buf[i]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
ghostty_key_event_free(event);
|
||||
ghostty_key_encoder_free(encoder);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ghostty/vt.h>
|
||||
|
||||
int main() {
|
||||
// Test safe paste data
|
||||
const char *safe_data = "hello world";
|
||||
if (ghostty_paste_is_safe(safe_data, strlen(safe_data))) {
|
||||
printf("'%s' is safe to paste\n", safe_data);
|
||||
}
|
||||
|
||||
// Test unsafe paste data with newline
|
||||
const char *unsafe_newline = "rm -rf /\n";
|
||||
if (!ghostty_paste_is_safe(unsafe_newline, strlen(unsafe_newline))) {
|
||||
printf("'%s' is UNSAFE - contains newline\n", unsafe_newline);
|
||||
}
|
||||
|
||||
// Test unsafe paste data with bracketed paste end sequence
|
||||
const char *unsafe_escape = "evil\x1b[201~code";
|
||||
if (!ghostty_paste_is_safe(unsafe_escape, strlen(unsafe_escape))) {
|
||||
printf("Data with escape sequence is UNSAFE\n");
|
||||
}
|
||||
|
||||
// Test empty data
|
||||
const char *empty_data = "";
|
||||
if (ghostty_paste_is_safe(empty_data, 0)) {
|
||||
printf("Empty data is safe\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <ghostty/vt.h>
|
||||
|
||||
int main() {
|
||||
// Create parser
|
||||
GhosttySgrParser parser;
|
||||
GhosttyResult result = ghostty_sgr_new(NULL, &parser);
|
||||
assert(result == GHOSTTY_SUCCESS);
|
||||
|
||||
// Parse a complex SGR sequence from Kakoune
|
||||
// This corresponds to the escape sequence:
|
||||
// ESC[4:3;38;2;51;51;51;48;2;170;170;170;58;2;255;97;136m
|
||||
//
|
||||
// Breaking down the sequence:
|
||||
// - 4:3 = curly underline (colon-separated sub-parameters)
|
||||
// - 38;2;51;51;51 = foreground RGB color (51, 51, 51) - dark gray
|
||||
// - 48;2;170;170;170 = background RGB color (170, 170, 170) - light gray
|
||||
// - 58;2;255;97;136 = underline RGB color (255, 97, 136) - pink
|
||||
uint16_t params[] = {4, 3, 38, 2, 51, 51, 51, 48, 2, 170, 170, 170, 58, 2, 255, 97, 136};
|
||||
|
||||
// Separator array: ':' at position 0 (between 4 and 3), ';' elsewhere
|
||||
char separators[] = ";;;;;;;;;;;;;;;;";
|
||||
separators[0] = ':';
|
||||
|
||||
result = ghostty_sgr_set_params(parser, params, separators, sizeof(params) / sizeof(params[0]));
|
||||
assert(result == GHOSTTY_SUCCESS);
|
||||
|
||||
printf("Parsing 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");
|
||||
|
||||
// Iterate through attributes
|
||||
GhosttySgrAttribute attr;
|
||||
int count = 0;
|
||||
while (ghostty_sgr_next(parser, &attr)) {
|
||||
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);
|
||||
|
||||
// Cleanup
|
||||
ghostty_sgr_free(parser);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ghostty/vt.h>
|
||||
|
||||
int main() {
|
||||
GhosttyOscParser parser;
|
||||
if (ghostty_osc_new(NULL, &parser) != GHOSTTY_SUCCESS) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup change window title command to change the title to "hello"
|
||||
ghostty_osc_next(parser, '0');
|
||||
ghostty_osc_next(parser, ';');
|
||||
const char *title = "hello";
|
||||
for (size_t i = 0; i < strlen(title); i++) {
|
||||
ghostty_osc_next(parser, title[i]);
|
||||
}
|
||||
|
||||
// End parsing and get command
|
||||
GhosttyOscCommand command = ghostty_osc_end(parser, 0);
|
||||
|
||||
// Get and print command type
|
||||
GhosttyOscCommandType type = ghostty_osc_command_type(command);
|
||||
printf("Command type: %d\n", type);
|
||||
|
||||
// Extract and print the title
|
||||
if (ghostty_osc_command_data(command, GHOSTTY_OSC_DATA_CHANGE_WINDOW_TITLE_STR, &title)) {
|
||||
printf("Extracted title: %s\n", title);
|
||||
} else {
|
||||
printf("Failed to extract title\n");
|
||||
}
|
||||
|
||||
ghostty_osc_free(parser);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user