1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
const std = @import("std");
const testing = std.testing;
const lib = @import("../lib.zig");
const CAllocator = lib.alloc.Allocator;
const formatterpkg = @import("../formatter.zig");
const grid_ref = @import("grid_ref.zig");
const point = @import("../point.zig");
const selection_codepoints = @import("../selection_codepoints.zig");
const Selection = @import("../Selection.zig");
const Result = @import("result.zig").Result;
const terminal_c = @import("terminal.zig");
const log = std.log.scoped(.selection_c);
pub const Adjustment = Selection.Adjustment;
pub const Order = Selection.Order;
pub const Format = formatterpkg.Format;
/// C: GhosttySelection
pub const CSelection = extern struct {
size: usize = @sizeOf(CSelection),
start: grid_ref.CGridRef,
end: grid_ref.CGridRef,
rectangle: bool = false,
pub fn toZig(self: CSelection) ?Selection {
const start_pin = self.start.toPin() orelse return null;
const end_pin = self.end.toPin() orelse return null;
return Selection.init(start_pin, end_pin, self.rectangle);
}
pub fn fromZig(sel: Selection) CSelection {
return .{
.start = .fromPin(sel.start()),
.end = .fromPin(sel.end()),
.rectangle = sel.rectangle,
};
}
};
/// C: GhosttySelectionBuffer
///
/// A caller-provided buffer of selections. This follows the same
/// conventions as GhosttyBuffer: ptr may be NULL with cap 0 to query
/// the required capacity, and len is set to the entries written on
/// success or to the required capacity on GHOSTTY_OUT_OF_SPACE.
pub const CSelectionBuffer = extern struct {
ptr: ?[*]CSelection = null,
cap: usize = 0,
len: usize = 0,
};
/// C: GhosttyTerminalSelectWordOptions
pub const SelectWordOptions = extern struct {
size: usize = @sizeOf(SelectWordOptions),
ref: grid_ref.CGridRef,
boundary_codepoints: ?[*]const u32 = null,
boundary_codepoints_len: usize = 0,
};
/// C: GhosttyTerminalSelectWordBetweenOptions
pub const SelectWordBetweenOptions = extern struct {
size: usize = @sizeOf(SelectWordBetweenOptions),
start: grid_ref.CGridRef,
end: grid_ref.CGridRef,
boundary_codepoints: ?[*]const u32 = null,
boundary_codepoints_len: usize = 0,
};
/// C: GhosttyTerminalSelectLineOptions
pub const SelectLineOptions = extern struct {
size: usize = @sizeOf(SelectLineOptions),
ref: grid_ref.CGridRef,
whitespace: ?[*]const u32 = null,
whitespace_len: usize = 0,
semantic_prompt_boundary: bool = false,
};
/// C: GhosttyTerminalSelectionFormatOptions
pub const FormatOptions = extern struct {
size: usize = @sizeOf(FormatOptions),
emit: Format,
unwrap: bool,
trim: bool,
selection: ?*const CSelection = null,
};
pub fn word(
terminal: terminal_c.Terminal,
options: ?*const SelectWordOptions,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const opts = options orelse return .invalid_value;
if (opts.size < @sizeOf(SelectWordOptions)) return .invalid_value;
const out = out_selection orelse return .invalid_value;
const boundary_codepoints = codepointSlice(
opts.boundary_codepoints,
opts.boundary_codepoints_len,
) catch return .invalid_value;
const screen = t.screens.active;
const pin = opts.ref.toPin() orelse return .invalid_value;
out.* = .fromZig(screen.selectWord(
pin,
boundary_codepoints orelse &selection_codepoints.default_word_boundaries,
) orelse
return .no_value);
return .success;
}
pub fn word_between(
terminal: terminal_c.Terminal,
options: ?*const SelectWordBetweenOptions,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const opts = options orelse return .invalid_value;
if (opts.size < @sizeOf(SelectWordBetweenOptions)) return .invalid_value;
const out = out_selection orelse return .invalid_value;
const boundary_codepoints = codepointSlice(
opts.boundary_codepoints,
opts.boundary_codepoints_len,
) catch return .invalid_value;
const screen = t.screens.active;
const start = opts.start.toPin() orelse return .invalid_value;
const end = opts.end.toPin() orelse return .invalid_value;
out.* = .fromZig(screen.selectWordBetween(
start,
end,
boundary_codepoints orelse &selection_codepoints.default_word_boundaries,
) orelse
return .no_value);
return .success;
}
pub fn line(
terminal: terminal_c.Terminal,
options: ?*const SelectLineOptions,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const opts = options orelse return .invalid_value;
if (opts.size < @sizeOf(SelectLineOptions)) return .invalid_value;
const out = out_selection orelse return .invalid_value;
const whitespace = codepointSlice(
opts.whitespace,
opts.whitespace_len,
) catch return .invalid_value;
const screen = t.screens.active;
const pin = opts.ref.toPin() orelse return .invalid_value;
out.* = .fromZig(screen.selectLine(.{
.pin = pin,
.whitespace = whitespace orelse &selection_codepoints.default_line_whitespace,
.semantic_prompt_boundary = opts.semantic_prompt_boundary,
}) orelse return .no_value);
return .success;
}
pub fn all(
terminal: terminal_c.Terminal,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const out = out_selection orelse return .invalid_value;
out.* = .fromZig(t.screens.active.selectAll() orelse return .no_value);
return .success;
}
pub fn output(
terminal: terminal_c.Terminal,
ref: grid_ref.CGridRef,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const out = out_selection orelse return .invalid_value;
const screen = t.screens.active;
const pin = ref.toPin() orelse return .invalid_value;
out.* = .fromZig(screen.selectOutput(pin) orelse return .no_value);
return .success;
}
pub fn format_buf(
terminal: terminal_c.Terminal,
opts: FormatOptions,
out_: ?[*]u8,
out_len: usize,
out_written: *usize,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
if (out_ == null) {
var discarding: std.Io.Writer.Discarding = .init(&.{});
formatSelection(t, opts, &discarding.writer) catch |err| return switch (err) {
error.InvalidValue => .invalid_value,
error.NoValue => .no_value,
error.WriteFailed => unreachable,
};
out_written.* = @intCast(discarding.count);
return .out_of_space;
}
var writer: std.Io.Writer = .fixed(out_.?[0..out_len]);
formatSelection(t, opts, &writer) catch |err| switch (err) {
error.InvalidValue => return .invalid_value,
error.NoValue => return .no_value,
error.WriteFailed => {
var discarding: std.Io.Writer.Discarding = .init(&.{});
formatSelection(t, opts, &discarding.writer) catch unreachable;
out_written.* = @intCast(discarding.count);
return .out_of_space;
},
};
out_written.* = writer.end;
return .success;
}
pub fn format_alloc(
terminal: terminal_c.Terminal,
alloc_: ?*const CAllocator,
opts: FormatOptions,
out_ptr: *?[*]u8,
out_len: *usize,
) callconv(lib.calling_conv) Result {
out_ptr.* = null;
out_len.* = 0;
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const alloc = lib.alloc.default(alloc_);
var aw: std.Io.Writer.Allocating = .init(alloc);
defer aw.deinit();
formatSelection(t, opts, &aw.writer) catch |err| return switch (err) {
error.InvalidValue => .invalid_value,
error.NoValue => .no_value,
error.WriteFailed => .out_of_memory,
};
const buf = aw.toOwnedSlice() catch return .out_of_memory;
out_ptr.* = buf.ptr;
out_len.* = buf.len;
return .success;
}
fn formatSelection(
t: *terminal_c.ZigTerminal,
opts: FormatOptions,
writer: *std.Io.Writer,
) error{ InvalidValue, NoValue, WriteFailed }!void {
var formatter = selectionFormatter(t, opts) catch |err| return err;
try formatter.format(writer);
}
fn selectionFormatter(
t: *terminal_c.ZigTerminal,
opts: FormatOptions,
) error{ InvalidValue, NoValue }!formatterpkg.TerminalFormatter {
if (opts.size < @sizeOf(FormatOptions)) return error.InvalidValue;
_ = std.enums.fromInt(Format, @intFromEnum(opts.emit)) orelse
return error.InvalidValue;
const sel = if (opts.selection) |sel|
sel.toZig() orelse return error.InvalidValue
else
t.screens.active.selection orelse return error.NoValue;
var formatter: formatterpkg.TerminalFormatter = .init(t, .{
.emit = opts.emit,
.unwrap = opts.unwrap,
.trim = opts.trim,
});
formatter.content = .{ .selection = sel };
return formatter;
}
/// Return the borrowed C array of `uint32_t` codepoints as a `[]const u21`.
///
/// `NULL + len 0` returns null, which callers treat as “use the API default
/// set.” A non-null pointer with `len 0` returns an empty slice, meaning “use an
/// explicitly empty set.” A non-zero length requires a non-null pointer.
///
/// This is intentionally zero-copy. In the C ABI, codepoints are `uint32_t`,
/// but selection internals use Zig's `u21` to represent valid Unicode scalar
/// values. Zig currently stores `u21` in the same size and alignment as `u32`,
/// so we assert that layout relationship and reinterpret the borrowed slice.
/// If Zig ever changes that representation, these comptime assertions fail
/// loudly rather than silently making this cast wrong.
fn codepointSlice(
ptr: ?[*]const u32,
len: usize,
) error{InvalidValue}!?[]const u21 {
comptime {
std.debug.assert(@sizeOf(u21) == @sizeOf(u32));
std.debug.assert(@alignOf(u21) == @alignOf(u32));
}
if (len == 0) {
const p = ptr orelse return null;
_ = p;
return &.{};
}
const p = ptr orelse return error.InvalidValue;
const cps: [*]const u21 = @ptrCast(p);
return cps[0..len];
}
pub fn adjust(
terminal: terminal_c.Terminal,
selection: ?*CSelection,
adjustment: Selection.Adjustment,
) callconv(lib.calling_conv) Result {
if (comptime std.debug.runtime_safety) {
_ = std.enums.fromInt(Selection.Adjustment, @intFromEnum(adjustment)) orelse {
log.warn("terminal_selection_adjust invalid adjustment value={d}", .{@intFromEnum(adjustment)});
return .invalid_value;
};
}
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const sel_ptr = selection orelse return .invalid_value;
var sel = sel_ptr.toZig() orelse return .invalid_value;
sel.adjust(t.screens.active, adjustment);
sel_ptr.* = .fromZig(sel);
return .success;
}
pub fn order(
terminal: terminal_c.Terminal,
selection: ?*const CSelection,
out_order: ?*Selection.Order,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const sel = (selection orelse return .invalid_value).toZig() orelse
return .invalid_value;
const out = out_order orelse return .invalid_value;
out.* = sel.order(t.screens.active);
return .success;
}
pub fn ordered(
terminal: terminal_c.Terminal,
selection: ?*const CSelection,
desired: Selection.Order,
out_selection: ?*CSelection,
) callconv(lib.calling_conv) Result {
if (comptime std.debug.runtime_safety) {
_ = std.enums.fromInt(Selection.Order, @intFromEnum(desired)) orelse {
log.warn("terminal_selection_ordered invalid desired value={d}", .{@intFromEnum(desired)});
return .invalid_value;
};
}
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const sel = (selection orelse return .invalid_value).toZig() orelse
return .invalid_value;
const out = out_selection orelse return .invalid_value;
out.* = .fromZig(sel.ordered(t.screens.active, desired));
return .success;
}
pub fn contains(
terminal: terminal_c.Terminal,
selection: ?*const CSelection,
pt: point.Point.C,
out_contains: ?*bool,
) callconv(lib.calling_conv) Result {
const t = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const sel = (selection orelse return .invalid_value).toZig() orelse
return .invalid_value;
const out = out_contains orelse return .invalid_value;
const screen = t.screens.active;
const pin = screen.pages.pin(.fromC(pt)) orelse return .invalid_value;
out.* = sel.contains(screen, pin);
return .success;
}
pub fn equal(
terminal: terminal_c.Terminal,
a: ?*const CSelection,
b: ?*const CSelection,
out_equal: ?*bool,
) callconv(lib.calling_conv) Result {
_ = terminal_c.zigTerminal(terminal) orelse return .invalid_value;
const sel_a = (a orelse return .invalid_value).toZig() orelse
return .invalid_value;
const sel_b = (b orelse return .invalid_value).toZig() orelse
return .invalid_value;
const out = out_equal orelse return .invalid_value;
out.* = sel_a.eql(sel_b);
return .success;
}
test "selection_format_alloc uses active selection" {
var t: terminal_c.Terminal = null;
try testing.expectEqual(Result.success, terminal_c.new(
&lib.alloc.test_allocator,
&t,
80,
24,
));
defer terminal_c.free(t);
terminal_c.vt_write(t, "Hello World", 11);
var start_ref: grid_ref.CGridRef = .{};
try testing.expectEqual(Result.success, terminal_c.grid_ref(t, .{
.tag = .active,
.value = .{ .active = .{ .x = 6, .y = 0 } },
}, &start_ref));
var end_ref: grid_ref.CGridRef = .{};
try testing.expectEqual(Result.success, terminal_c.grid_ref(t, .{
.tag = .active,
.value = .{ .active = .{ .x = 10, .y = 0 } },
}, &end_ref));
const sel: CSelection = .{
.start = start_ref,
.end = end_ref,
};
try testing.expectEqual(Result.success, terminal_c.set(t, .selection, @ptrCast(&sel)));
const opts: FormatOptions = .{
.emit = .plain,
.unwrap = true,
.trim = true,
};
var required: usize = 0;
try testing.expectEqual(Result.out_of_space, format_buf(
t,
opts,
null,
0,
&required,
));
try testing.expectEqual(@as(usize, 5), required);
var out_ptr: ?[*]u8 = null;
var out_len: usize = 0;
try testing.expectEqual(Result.success, format_alloc(
t,
&lib.alloc.test_allocator,
opts,
&out_ptr,
&out_len,
));
const ptr = out_ptr orelse return error.TestExpectedEqual;
defer lib.alloc.default(&lib.alloc.test_allocator).free(ptr[0..out_len]);
try testing.expectEqualStrings("World", ptr[0..out_len]);
}
test "selection_format_buf uses provided selection" {
var t: terminal_c.Terminal = null;
try testing.expectEqual(Result.success, terminal_c.new(
&lib.alloc.test_allocator,
&t,
80,
24,
));
defer terminal_c.free(t);
terminal_c.vt_write(t, "Hello World", 11);
var start_ref: grid_ref.CGridRef = .{};
try testing.expectEqual(Result.success, terminal_c.grid_ref(t, .{
.tag = .active,
.value = .{ .active = .{ .x = 0, .y = 0 } },
}, &start_ref));
var end_ref: grid_ref.CGridRef = .{};
try testing.expectEqual(Result.success, terminal_c.grid_ref(t, .{
.tag = .active,
.value = .{ .active = .{ .x = 4, .y = 0 } },
}, &end_ref));
const sel: CSelection = .{
.start = start_ref,
.end = end_ref,
};
const opts: FormatOptions = .{
.emit = .plain,
.unwrap = true,
.trim = true,
.selection = &sel,
};
var small: [2]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.out_of_space, format_buf(
t,
opts,
&small,
small.len,
&written,
));
try testing.expectEqual(@as(usize, 5), written);
var buf: [32]u8 = undefined;
try testing.expectEqual(Result.success, format_buf(
t,
opts,
&buf,
buf.len,
&written,
));
try testing.expectEqualStrings("Hello", buf[0..written]);
}
test "selection_format_alloc returns no_value without active selection" {
var t: terminal_c.Terminal = null;
try testing.expectEqual(Result.success, terminal_c.new(
&lib.alloc.test_allocator,
&t,
80,
24,
));
defer terminal_c.free(t);
var out_ptr: ?[*]u8 = @ptrFromInt(1);
var out_len: usize = 123;
try testing.expectEqual(Result.no_value, format_alloc(
t,
&lib.alloc.test_allocator,
.{ .emit = .plain, .unwrap = true, .trim = true },
&out_ptr,
&out_len,
));
try testing.expect(out_ptr == null);
try testing.expectEqual(@as(usize, 0), out_len);
}
|