summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorMitchell Hashimoto <m@mitchellh.com>2025-10-04 20:32:42 -0700
committerMitchell Hashimoto <m@mitchellh.com>2025-10-05 14:40:20 -0700
commit61fe78c1d304b31400609d8191e427466ebcec25 (patch)
tree95a2cf8c8880d0020d4d5905365ff239f53c517f /example
parent31ba6534cf16a1799b5fdc15641e4efeb483b5f7 (diff)
lib-vt: expose key encoding as a C API
Diffstat (limited to 'example')
-rw-r--r--example/c-vt-key-encode/README.md22
-rw-r--r--example/c-vt-key-encode/build.zig42
-rw-r--r--example/c-vt-key-encode/build.zig.zon24
-rw-r--r--example/c-vt-key-encode/src/main.c59
4 files changed, 147 insertions, 0 deletions
diff --git a/example/c-vt-key-encode/README.md b/example/c-vt-key-encode/README.md
new file mode 100644
index 000000000..05ee3fc31
--- /dev/null
+++ b/example/c-vt-key-encode/README.md
@@ -0,0 +1,22 @@
+# Example: `ghostty-vt` C Key Encoding
+
+This example demonstrates how to use the `ghostty-vt` C library to encode key
+events into terminal escape sequences.
+
+This example specifically shows how to:
+
+1. Create a key encoder with the C API
+2. Configure Kitty keyboard protocol flags (this example uses KKP)
+3. Create and configure a key event
+4. Encode the key event into a terminal escape sequence
+
+The example encodes a Ctrl key release event with the Ctrl modifier set,
+producing the escape sequence `\x1b[57442;5:3u`.
+
+## Usage
+
+Run the program:
+
+```shell-session
+zig build run
+```
diff --git a/example/c-vt-key-encode/build.zig b/example/c-vt-key-encode/build.zig
new file mode 100644
index 000000000..b4b759744
--- /dev/null
+++ b/example/c-vt-key-encode/build.zig
@@ -0,0 +1,42 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const target = b.standardTargetOptions(.{});
+ const optimize = b.standardOptimizeOption(.{});
+
+ const run_step = b.step("run", "Run the app");
+
+ const exe_mod = b.createModule(.{
+ .target = target,
+ .optimize = optimize,
+ });
+ exe_mod.addCSourceFiles(.{
+ .root = b.path("src"),
+ .files = &.{"main.c"},
+ });
+
+ // You'll want to use a lazy dependency here so that ghostty is only
+ // downloaded if you actually need it.
+ if (b.lazyDependency("ghostty", .{
+ // Setting simd to false will force a pure static build that
+ // doesn't even require libc, but it has a significant performance
+ // penalty. If your embedding app requires libc anyway, you should
+ // always keep simd enabled.
+ // .simd = false,
+ })) |dep| {
+ exe_mod.linkLibrary(dep.artifact("ghostty-vt"));
+ }
+
+ // Exe
+ const exe = b.addExecutable(.{
+ .name = "c_vt_key_encode",
+ .root_module = exe_mod,
+ });
+ b.installArtifact(exe);
+
+ // Run
+ const run_cmd = b.addRunArtifact(exe);
+ run_cmd.step.dependOn(b.getInstallStep());
+ if (b.args) |args| run_cmd.addArgs(args);
+ run_step.dependOn(&run_cmd.step);
+}
diff --git a/example/c-vt-key-encode/build.zig.zon b/example/c-vt-key-encode/build.zig.zon
new file mode 100644
index 000000000..5da1a9168
--- /dev/null
+++ b/example/c-vt-key-encode/build.zig.zon
@@ -0,0 +1,24 @@
+.{
+ .name = .c_vt,
+ .version = "0.0.0",
+ .fingerprint = 0x413a8529b1255f9a,
+ .minimum_zig_version = "0.15.1",
+ .dependencies = .{
+ // Ghostty dependency. In reality, you'd probably use a URL-based
+ // dependency like the one showed (and commented out) below this one.
+ // We use a path dependency here for simplicity and to ensure our
+ // examples always test against the source they're bundled with.
+ .ghostty = .{ .path = "../../" },
+
+ // Example of what a URL-based dependency looks like:
+ // .ghostty = .{
+ // .url = "https://github.com/ghostty-org/ghostty/archive/COMMIT.tar.gz",
+ // .hash = "N-V-__8AAMVLTABmYkLqhZPLXnMl-KyN38R8UVYqGrxqO36s",
+ // },
+ },
+ .paths = .{
+ "build.zig",
+ "build.zig.zon",
+ "src",
+ },
+}
diff --git a/example/c-vt-key-encode/src/main.c b/example/c-vt-key-encode/src/main.c
new file mode 100644
index 000000000..82444f99d
--- /dev/null
+++ b/example/c-vt-key-encode/src/main.c
@@ -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;
+}