aboutsummaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorOgulcan Celik <ogulcancelik@gmail.com>2026-04-06 22:05:00 +0300
committerOgulcan Celik <ogulcancelik@gmail.com>2026-04-06 22:56:01 +0300
commit6ccf156a3dd23f419c03892ddc1136e72957f66a (patch)
tree1d5465e61d03344a63683ad3b9bbdec9a4476a5c /vendor
parent62b751b2b551d866e2aebfdecf89f7a003d64ebc (diff)
refactor: use libghostty-native selection text reading
Diffstat (limited to 'vendor')
-rw-r--r--vendor/libghostty-vt/include/ghostty/vt/terminal.h48
-rw-r--r--vendor/libghostty-vt/src/lib_vt.zig1
-rw-r--r--vendor/libghostty-vt/src/terminal/c/main.zig1
-rw-r--r--vendor/libghostty-vt/src/terminal/c/terminal.zig70
-rw-r--r--vendor/libghostty-vt/src/terminal/c/types.zig1
5 files changed, 113 insertions, 8 deletions
diff --git a/vendor/libghostty-vt/include/ghostty/vt/terminal.h b/vendor/libghostty-vt/include/ghostty/vt/terminal.h
index b43e37cf..b466bd84 100644
--- a/vendor/libghostty-vt/include/ghostty/vt/terminal.h
+++ b/vendor/libghostty-vt/include/ghostty/vt/terminal.h
@@ -254,6 +254,26 @@ typedef struct {
} GhosttyTerminalScrollbar;
/**
+ * Selection bounds for terminal text extraction.
+ *
+ * The start and end points may be in any supported point coordinate system
+ * (active, viewport, screen, or history). Text is always returned in reading
+ * order. Set `rectangle` to true for rectangular selection semantics.
+ *
+ * @ingroup terminal
+ */
+typedef struct {
+ /** Selection start point. */
+ GhosttyPoint start;
+
+ /** Selection end point. */
+ GhosttyPoint end;
+
+ /** Whether to treat the selection as a rectangle. */
+ bool rectangle;
+} GhosttyTerminalSelection;
+
+/**
* Callback function type for bell.
*
* Called when the terminal receives a BEL character (0x07).
@@ -974,6 +994,34 @@ GHOSTTY_API GhosttyResult ghostty_terminal_grid_ref(GhosttyTerminal terminal,
GhosttyPoint point,
GhosttyGridRef *out_ref);
+/**
+ * Read terminal text for an arbitrary selection.
+ *
+ * Formats the selected region as plain text using Ghostty's native selection
+ * semantics. Soft-wrapped lines are unwrapped and trailing whitespace is
+ * trimmed on non-blank lines.
+ *
+ * The returned buffer is allocated using the provided allocator, or the
+ * default allocator if NULL is passed. Free it with ghostty_free(), passing
+ * the same allocator pointer and returned length.
+ *
+ * @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
+ * @param selection The selection bounds to read
+ * @param allocator Pointer to allocator, or NULL to use the default allocator
+ * @param[out] out_ptr On success, set to the allocated output buffer
+ * @param[out] out_len On success, set to the output length in bytes
+ * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal
+ * is NULL or the selection points are out of bounds,
+ * GHOSTTY_OUT_OF_MEMORY on allocation failure
+ *
+ * @ingroup terminal
+ */
+GHOSTTY_API GhosttyResult ghostty_terminal_read_text(GhosttyTerminal terminal,
+ GhosttyTerminalSelection selection,
+ const GhosttyAllocator* allocator,
+ uint8_t** out_ptr,
+ size_t* out_len);
+
/** @} */
#ifdef __cplusplus
diff --git a/vendor/libghostty-vt/src/lib_vt.zig b/vendor/libghostty-vt/src/lib_vt.zig
index 9f5ee6c4..9d2442a8 100644
--- a/vendor/libghostty-vt/src/lib_vt.zig
+++ b/vendor/libghostty-vt/src/lib_vt.zig
@@ -214,6 +214,7 @@ comptime {
@export(&c.terminal_mode_set, .{ .name = "ghostty_terminal_mode_set" });
@export(&c.terminal_get, .{ .name = "ghostty_terminal_get" });
@export(&c.terminal_grid_ref, .{ .name = "ghostty_terminal_grid_ref" });
+ @export(&c.terminal_read_text, .{ .name = "ghostty_terminal_read_text" });
@export(&c.grid_ref_cell, .{ .name = "ghostty_grid_ref_cell" });
@export(&c.grid_ref_row, .{ .name = "ghostty_grid_ref_row" });
@export(&c.grid_ref_graphemes, .{ .name = "ghostty_grid_ref_graphemes" });
diff --git a/vendor/libghostty-vt/src/terminal/c/main.zig b/vendor/libghostty-vt/src/terminal/c/main.zig
index 17056779..34d1a8f4 100644
--- a/vendor/libghostty-vt/src/terminal/c/main.zig
+++ b/vendor/libghostty-vt/src/terminal/c/main.zig
@@ -142,6 +142,7 @@ pub const terminal_mode_get = terminal.mode_get;
pub const terminal_mode_set = terminal.mode_set;
pub const terminal_get = terminal.get;
pub const terminal_grid_ref = terminal.grid_ref;
+pub const terminal_read_text = terminal.read_text;
pub const type_json = types.get_json;
diff --git a/vendor/libghostty-vt/src/terminal/c/terminal.zig b/vendor/libghostty-vt/src/terminal/c/terminal.zig
index ec749ffa..fa421f21 100644
--- a/vendor/libghostty-vt/src/terminal/c/terminal.zig
+++ b/vendor/libghostty-vt/src/terminal/c/terminal.zig
@@ -13,6 +13,8 @@ const size = @import("../size.zig");
const device_attributes = @import("../device_attributes.zig");
const device_status = @import("../device_status.zig");
const size_report = @import("../size_report.zig");
+const formatterpkg = @import("../formatter.zig");
+const selectionpkg = @import("../Selection.zig");
const cell_c = @import("cell.zig");
const row_c = @import("row.zig");
const grid_ref_c = @import("grid_ref.zig");
@@ -398,6 +400,21 @@ pub const DeviceAttributes = Effects.CDeviceAttributes;
/// C: GhosttyTerminalScrollViewport
pub const ScrollViewport = ZigTerminal.ScrollViewport.C;
+/// C: GhosttyTerminalSelection
+pub const Selection = extern struct {
+ start: point.Point.C,
+ end: point.Point.C,
+ rectangle: bool,
+
+ fn toZig(self: Selection, terminal: *const ZigTerminal) ?selectionpkg {
+ return selectionpkg.init(
+ pinFromPoint(terminal, self.start) orelse return null,
+ pinFromPoint(terminal, self.end) orelse return null,
+ self.rectangle,
+ );
+ }
+};
+
pub fn scroll_viewport(
terminal_: Terminal,
behavior: ScrollViewport,
@@ -608,24 +625,61 @@ fn getTyped(
return .success;
}
+fn pointToZig(pt: point.Point.C) point.Point {
+ return switch (pt.tag) {
+ .active => .{ .active = pt.value.active },
+ .viewport => .{ .viewport = pt.value.viewport },
+ .screen => .{ .screen = pt.value.screen },
+ .history => .{ .history = pt.value.history },
+ };
+}
+
+fn pinFromPoint(terminal: *const ZigTerminal, pt: point.Point.C) ?PageList.Pin {
+ return terminal.screens.active.pages.pin(pointToZig(pt));
+}
+
pub fn grid_ref(
terminal_: Terminal,
pt: point.Point.C,
out_ref: ?*grid_ref_c.CGridRef,
) callconv(lib.calling_conv) Result {
const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal;
- const zig_pt: point.Point = switch (pt.tag) {
- .active => .{ .active = pt.value.active },
- .viewport => .{ .viewport = pt.value.viewport },
- .screen => .{ .screen = pt.value.screen },
- .history => .{ .history = pt.value.history },
- };
- const p = t.screens.active.pages.pin(zig_pt) orelse
- return .invalid_value;
+ const p = pinFromPoint(t, pt) orelse return .invalid_value;
if (out_ref) |out| out.* = grid_ref_c.CGridRef.fromPin(p);
return .success;
}
+pub fn read_text(
+ terminal_: Terminal,
+ selection: Selection,
+ alloc_: ?*const CAllocator,
+ out_ptr: *?[*]u8,
+ out_len: *usize,
+) callconv(lib.calling_conv) Result {
+ out_ptr.* = null;
+ out_len.* = 0;
+
+ const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal;
+ const alloc = lib.alloc.default(alloc_);
+ const sel = selection.toZig(t) orelse return .invalid_value;
+
+ var formatter: formatterpkg.ScreenFormatter = .init(t.screens.active, .{
+ .emit = .plain,
+ .unwrap = true,
+ .trim = true,
+ });
+ formatter.content = .{ .selection = sel };
+
+ var aw: std.Io.Writer.Allocating = .init(alloc);
+ defer aw.deinit();
+ formatter.format(&aw.writer) catch return .out_of_memory;
+
+ const buf = aw.toOwnedSlice() catch return .out_of_memory;
+ out_ptr.* = buf.ptr;
+ out_len.* = buf.len;
+ return .success;
+}
+
pub fn free(terminal_: Terminal) callconv(lib.calling_conv) void {
const wrapper = terminal_ orelse return;
const t = wrapper.terminal;
diff --git a/vendor/libghostty-vt/src/terminal/c/types.zig b/vendor/libghostty-vt/src/terminal/c/types.zig
index b808bf38..51184b47 100644
--- a/vendor/libghostty-vt/src/terminal/c/types.zig
+++ b/vendor/libghostty-vt/src/terminal/c/types.zig
@@ -41,6 +41,7 @@ pub const structs: std.StaticStringMap(StructInfo) = .initComptime(.{
.{ "GhosttyTerminalOptions", StructInfo.init(terminal.Options) },
.{ "GhosttyTerminalScrollbar", StructInfo.init(terminal.TerminalScrollbar) },
.{ "GhosttyTerminalScrollViewport", StructInfo.init(terminal.ScrollViewport) },
+ .{ "GhosttyTerminalSelection", StructInfo.init(terminal.Selection) },
});
/// The comptime-generated JSON string of all structs.