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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
//! Views: several clients on one session, kakoune's way (docs/next-daemon.md,
//! "Phase 4 notes"). Each view has its own layout, focus, selections and
//! scroll over the panes every view shares: their text, terminals, tags,
//! registers and messages.
//!
//! The core still sees one view, the live one, in the fields it always had:
//! a view's share of the session (`fields`) and of each pane (`PaneView`) is
//! copied out when another view goes live (`park`) and back when it is live
//! again (`unpark`). No line of the core reads a view through an
//! indirection, and one view never parks: today's single client runs
//! exactly as before.
//!
//! A parked view is in no digest (Pardes.replica's `parked`, Pane's `pv`):
//! a slot is written whenever its view goes out, so the live view's slot is
//! stale by design, and a parked view that diverged shows when it is live
//! again. The pump draws the other views by going into each and back
//! (`drawOthers`), which leaves every digested field as it was; a replica
//! that draws nothing skips it.
const std = @import("std");
const pardes = @import("pardes.zig");
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
const Text = pardes.panes.Text;
const File = pardes.panes.File;
const layout = pardes.layout;
const MAX_PANES = pardes.MAX_PANES;
/// Views at once: a window or terminal each.
pub const max = 4;
/// The session's fields that are each view's own: its screen and layout,
/// its focus, its pointer and gestures, its jumps, its key prefixes, and
/// what drawing it keeps (animations, the presentation of its panels).
pub const fields = [_][:0]const u8{
"screen_w", "screen_h", "cell_pixels", "row_metrics",
"ncol", "col_weight", "col_panes", "col_n",
"col_tags", "col_serial", "finder", "rects",
"col_x", "col_w", "presentation", "active",
"jumps", "njumps", "jcur", "jump_force",
"active_column", "lifts", "lift_serials", "cursor_glide",
"cursor_view", "cursor_crossing", "cursor_held", "shell_continuous",
"input_ns", "native_images", "native_image_patches", "windowed",
"drag", "mouse_grab", "window_focused", "focus_told",
"hover_col", "hover_row", "pointer_raw_col", "pointer_raw_row",
"pointer_body_hit", "pointer_tag_hit", "raw_hover_intent", "pointer_inside",
"last_click", "click_as", "chord_hold", "wheel_guard",
"ctrl_w_pending", "register", "leader_on", "window_pick",
"leader_keys", "leader_n", "header_focus", "header_column",
"header_drag", "header_sweep", "header_scroll", "header_top",
"ov_pinch_scale", "ov_touch_scroll_delta", "chord_arg", "clip_pending",
};
/// A pane's fields that are each view's own: where it is in the view's
/// layout, which of its texts has the keyboard, how far it is scrolled, and
/// what the view's step measured of it (its tag's rows, its bands).
const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan" };
/// The body's: its cursor, selections and mode. Its undo stays the pane's.
const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "msel", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" };
pub const View = Part(Pardes, &fields);
/// Named field by field, not taken from Pane's type: a pane holds its own.
pub const PaneView = struct {
focus: Pane.Focus = .body,
vweight: f32 = 1,
collapsed: bool = false,
attached_to: ?u32 = null,
hscroll: i32 = 0,
tag_scroll: u16 = 0,
tag_top: u16 = 0,
tag_rows: u16 = 1,
view: Pane.SessionView = .{},
notices: Pane.Notices = .{},
pan: u16 = 0,
body: Part(Text, &body_fields) = .{},
/// A document's scroll and size; a terminal's size is its pty's, which
/// every view shows (sized by its last typist, `Pardes.sync`).
scroll: usize = 0,
cols: u16 = 1,
rows: u16 = 1,
};
/// A view not live: its share of the session, and the serial of each pane
/// slot when it went out, by which the panes closed since leave its layout.
pub const Parked = struct {
view: View,
serials: [MAX_PANES]u32 = @splat(0),
};
/// The fields of `T` named, as a struct of their own, each of its type and
/// default.
fn Part(comptime T: type, comptime names: []const [:0]const u8) type {
@setEvalBranchQuota(100_000);
const s = @typeInfo(T).@"struct";
var types: [names.len]type = undefined;
var attrs: [names.len]std.lang.Type.Struct.FieldAttributes = undefined;
for (names, 0..) |name, i| {
const at = for (s.field_names, 0..) |n, j| {
if (std.mem.eql(u8, n, name)) break j;
} else @compileError("views: " ++ @typeName(T) ++ " has no field " ++ name);
types[i] = s.field_types[at];
attrs[i] = .{ .default_value_ptr = s.field_attrs[at].default_value_ptr };
}
return @Struct(.auto, null, names, &types, &attrs);
}
fn copy(comptime names: []const [:0]const u8, dst: anytype, src: anytype) void {
inline for (names) |name| @field(dst, name) = @field(src, name);
}
pub fn isOpen(p: *const Pardes, k: u8) bool {
return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0;
}
pub fn count(p: *const Pardes) usize {
return @popCount(p.views_open);
}
/// More than one view: the one test the single client's path pays.
pub fn several(p: *const Pardes) bool {
return p.views_open & (p.views_open -% 1) != 0;
}
/// Pane `pane`'s share of the live view.
fn extract(pane: *const Pane) PaneView {
var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows };
copy(&pane_fields, &v, pane);
copy(&body_fields, &v.body, &pane.body);
if (pane.file) |f| v.scroll = f.scroll;
return v;
}
/// Its share of view `v` put back.
fn apply(pane: *Pane, v: *const PaneView) void {
copy(&pane_fields, pane, v);
copy(&body_fields, &pane.body, &v.body);
if (!pane.isTerminal()) {
pane.cols = v.cols;
pane.rows = v.rows;
}
const f = if (pane.file) |*file| file else return;
// The text may have changed while the view was out: what it held is
// brought back inside it.
const lines = File.nlines(pane.gpa, f);
f.scroll = @min(v.scroll, lines -| 1);
if (pane.body.cur_row >= lines) {
pane.body.vsel.active = false;
pane.body.msel.active = false;
pane.body.nsel = 0;
}
_ = pardes.edit.clampFileCursor(&pane.body);
}
/// A copy of the live view's share for another view, owning nothing of the
/// live one's (its extra selections stay behind).
fn fresh(pane: *const Pane) PaneView {
var v = extract(pane);
v.body.sels = &.{};
v.body.nsel = 0;
return v;
}
/// A new pane is in each other view as it was made, there to be shown.
pub fn installed(p: *Pardes, pane: *Pane) void {
pane.sized_by = p.view;
if (!several(p)) return;
for (0..max) |k| if (k != p.view and isOpen(p, @intCast(k))) {
pane.pv[k] = fresh(pane);
};
pane.pv_known = p.views_open;
}
/// A pane going: the other views' selections of it go too.
pub fn tornDown(p: *Pardes, pane: *Pane) void {
for (0..max) |k| if (k != p.view and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) {
const sels = pane.pv[k].body.sels;
if (sels.len > 0) p.gpa.free(sels);
pane.pv[k].body.sels = &.{};
};
}
fn park(p: *Pardes) void {
const slot = p.parked[p.view].?;
copy(&fields, &slot.view, p);
for (p.panes, 0..) |s, id| {
const pane = s orelse {
slot.serials[id] = 0;
continue;
};
slot.serials[id] = pane.serial;
pane.pv[p.view] = extract(pane);
}
}
fn unpark(p: *Pardes, k: u8) void {
const slot = p.parked[k].?;
copy(&fields, p, &slot.view);
p.view = k;
for (p.panes) |s| if (s) |pane| apply(pane, &pane.pv[k]);
// The panes closed while it was out (a slot empty or another pane's
// now) leave its layout, and its keyboard leaves them.
for (0..p.ncol) |c| {
var n: usize = 0;
for (p.col_panes[c][0..p.col_n[c]]) |id| {
const pane = p.panes[id] orelse continue;
if (pane.serial != slot.serials[id]) continue;
p.col_panes[c][n] = id;
n += 1;
}
p.col_n[c] = n;
}
if (layout.findPane(p, p.active) == null) {
for (0..p.ncol) |c| if (p.col_n[c] > 0) {
p.active = p.col_panes[c][0];
break;
};
}
layout.compute(p);
// Its terminals' cursors moved while it was out: each pan follows.
for (p.panes, 0..) |s, id| if (s) |pane| {
pane.pan = pardes.panes.terminal.panFor(p, pane, p.rects[id]);
};
}
/// The pane with the keyboard is one the live view shows: focused where its
/// layout has it not (a Look at a file another view opened, a jump, a 9P
/// focus), it goes into the view's active column, as kakoune shows a buffer
/// in the window that asked for it.
pub fn show(p: *Pardes) void {
if (p.panes[p.active] == null or layout.findPane(p, p.active) != null) return;
const c = layout.firstColumn(p) orelse layout.columnBySerial(p, p.active_column) orelse p.ncol - 1;
layout.acmePlace(p, c, null, p.active);
}
/// Into view `k`: the live one parked, `k`'s put in its place.
pub fn to(p: *Pardes, k: u8) void {
std.debug.assert(isOpen(p, k));
if (k == p.view) return;
park(p);
unpark(p, k);
}
/// A new view, as the live one is laid out and focused, with its panes
/// where they are scrolled and its cursors where they are; the rest of it
/// (column tags, gestures, animations) as a session starts. Null when there
/// are `max` already.
pub fn open(p: *Pardes) ?u8 {
const k: u8 = for (0..max) |i| {
if (!isOpen(p, @intCast(i))) break @intCast(i);
} else return null;
// Each live view parks too: its slot is made with the first other.
for ([_]u8{ p.view, k }) |i| if (p.parked[i] == null) {
const slot = p.gpa.create(Parked) catch return null;
slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } };
p.parked[i] = slot;
};
const slot = p.parked[k].?;
slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } };
inline for (.{ "screen_w", "screen_h", "cell_pixels", "row_metrics", "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur", "windowed", "native_images", "native_image_patches" }) |name|
@field(slot.view, name) = @field(p, name);
slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa });
for (p.panes, 0..) |s, id| {
const pane = s orelse continue;
slot.serials[id] = pane.serial;
pane.pv[k] = fresh(pane);
pane.pv_known |= @as(u8, 1) << @intCast(k);
}
p.views_open |= @as(u8, 1) << @intCast(k);
return k;
}
/// View `k` gone, and all it held; the live one moves to another first.
/// The last view stays: a session without a client keeps its own.
pub fn close(p: *Pardes, k: u8) void {
if (!isOpen(p, k) or count(p) == 1) return;
if (k == p.view) {
const other: u8 = for (0..max) |i| {
if (i != k and isOpen(p, @intCast(i))) break @intCast(i);
} else unreachable;
park(p);
unpark(p, other);
}
const v = &p.parked[k].?.view;
for (&v.col_tags) |*t| t.deinit(p.gpa);
if (v.chord_arg) |a| p.gpa.free(a);
v.presentation.deinit(p.gpa);
for (p.panes) |s| if (s) |pane| {
const sels = pane.pv[k].body.sels;
if (sels.len > 0) p.gpa.free(sels);
pane.pv[k].body.sels = &.{};
pane.pv_known &= ~(@as(u8, 1) << @intCast(k));
// A name it was drafting goes with it.
if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt();
};
p.views_open &= ~(@as(u8, 1) << @intCast(k));
}
/// The session's end: what each view not live holds.
pub fn deinit(p: *Pardes) void {
for (0..max) |i| {
const k: u8 = @intCast(i);
const slot = p.parked[k] orelse continue;
if (k != p.view and isOpen(p, k)) {
const v = &slot.view;
for (&v.col_tags) |*t| t.deinit(p.gpa);
if (v.chord_arg) |a| p.gpa.free(a);
v.presentation.deinit(p.gpa);
}
p.gpa.destroy(slot);
}
}
/// What drawing a view keeps of its own (its animations, its panels as
/// presented), kept from a frame drawn while it was out.
const drawn_fields = [_][:0]const u8{ "presentation", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous" };
/// Each view but the live one drawn and presented, the live one put back
/// after exactly as it was: no digested field moves, so a replica that
/// draws nothing (and skips this) stays the same. A view drawn keeps only
/// what its drawing made (`drawn_fields`); whatever its unpark brought
/// inside a changed text is not kept, and is done again when it is next
/// live. Before the live one's frame: the surface after the pump is its.
pub fn drawOthers(p: *Pardes, h: pardes.Host) !void {
const live = p.view;
const v = h.vtable;
park(p);
defer {
copy(&fields, p, &p.parked[live].?.view);
p.view = live;
for (p.panes) |s| if (s) |pane| {
const pv = &pane.pv[live];
copy(&pane_fields, pane, pv);
copy(&body_fields, &pane.body, &pv.body);
if (!pane.isTerminal()) {
pane.cols = pv.cols;
pane.rows = pv.rows;
}
if (pane.file) |*f| f.scroll = pv.scroll;
};
}
for (0..max) |i| {
const k: u8 = @intCast(i);
if (k == live or !isOpen(p, k)) continue;
unpark(p, k);
_ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept });
const surface = try p.render(p.frame_arena.allocator());
if (v.present) |f| f(h.ctx, surface);
// No post_present: what a shell acknowledges there moves the
// session's pointer and hover, which are the live view's to move;
// a view's panels are acknowledged when it is live and drawn.
copy(&drawn_fields, &p.parked[k].?.view, p);
}
}
// ---- tests ----
const testing = std.testing;
const Key = pardes.Key;
fn typeKeys(p: *Pardes, text: []const u8) void {
for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
}
fn escape(p: *Pardes) void {
p.update(.{ .key = .{ .cp = Key.escape } });
}
fn numbered(gpa: std.mem.Allocator, n: usize) ![]u8 {
var out: std.ArrayList(u8) = .empty;
for (1..n + 1) |i| try out.print(gpa, "line {d}\n", .{i});
return out.toOwnedSlice(gpa);
}
/// The cursor put on `row`, as a jump puts it, and the step after.
fn cursorTo(p: *Pardes, pane: *Pane, row: i32) void {
pane.body.vsel.active = false;
pane.body.nsel = 0;
pane.body.cur_row = row;
pane.body.cur_col = 0;
pane.body.cur_pinned = true;
pane.ensureCursorVisible();
p.sync();
}
test "each view has its own cursor and scroll, and an edit in one is in the other" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const text = try numbered(gpa, 200);
defer gpa.free(text);
const pane = try p.setTestFile(text);
try testing.expectEqual(@as(?u8, 1), p.openView());
cursorTo(p, pane, 119);
const scrolled = pane.file.?.scroll;
try testing.expect(scrolled > 0);
// The other view is where the session was when it came.
p.toView(1);
try testing.expectEqual(@as(i32, 0), pane.body.cur_row);
try testing.expectEqual(@as(usize, 0), pane.file.?.scroll);
typeKeys(p, "ix");
escape(p);
try testing.expectEqual(Text.Mode.normal, pane.body.mode);
p.toView(0);
try testing.expectEqual(@as(i32, 119), pane.body.cur_row);
try testing.expectEqual(scrolled, pane.file.?.scroll);
try testing.expect(std.mem.startsWith(u8, pane.file.?.content, "xline 1\n"));
// Its mode is its own: insert here leaves the other in normal.
typeKeys(p, "i");
try testing.expectEqual(Text.Mode.insert, pane.body.mode);
p.toView(1);
try testing.expectEqual(Text.Mode.normal, pane.body.mode);
}
test "each view has its own layout and focus over the same panes" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
_ = try p.setTestFile("one\ntwo\n");
const k = p.openView().?;
const ncol = p.ncol;
p.toView(k);
p.update(.{ .command = "Newcol" });
p.update(.{ .command = "New" });
const made = p.active;
try testing.expect(made != 0);
try testing.expectEqual(ncol + 1, p.ncol);
p.toView(0);
try testing.expectEqual(ncol, p.ncol);
try testing.expectEqual(@as(usize, 0), p.active);
try testing.expect(layout.findPane(p, made) == null);
try testing.expectEqual(pardes.Rect{}, p.rects[made]);
// The pane is the session's: focused here, this view shows it too.
p.active = made;
p.sync();
try testing.expect(layout.findPane(p, made) != null);
try testing.expect(p.rects[made].h > 0);
// Closed here, it leaves the other view's layout, and its keyboard.
try p.removePane(made, null);
p.sync();
p.toView(k);
try testing.expect(layout.findPane(p, made) == null);
try testing.expect(p.panes[p.active] != null);
try testing.expect(layout.findPane(p, p.active) != null);
}
test "registers are the session's: yanked in one view, put in another" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const pane = try p.setTestFile("alpha\nbeta\n");
const k = p.openView().?;
typeKeys(p, "xy");
p.toView(k);
cursorTo(p, pane, 1);
typeKeys(p, "p");
try testing.expectEqualStrings("alpha\nbeta\nalpha\n", pane.file.?.content);
}
test "a terminal shown at two sizes takes the size of the view that typed into it last" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const term = p.panes[p.active].?;
try testing.expect(term.isTerminal());
const small = .{ term.cols, term.rows };
const k = p.openView().?;
p.toView(k);
p.update(.{ .resize = .{ .cols = 120, .rows = 40 } });
// Shown bigger here, it keeps the size of the view that typed last.
try testing.expect(p.rects[p.active].w > 80);
try testing.expectEqual(small, .{ term.cols, term.rows });
p.update(.{ .key = .{ .cp = Key.right } });
try testing.expect(term.cols > small[0] and term.rows > small[1]);
const big = .{ term.cols, term.rows };
p.toView(0);
p.sync();
try testing.expectEqual(big, .{ term.cols, term.rows });
p.update(.{ .key = .{ .cp = Key.right } });
try testing.expectEqual(small, .{ term.cols, term.rows });
// That view gone, the one left sizes it.
p.toView(k);
p.closeView(0);
try testing.expectEqual(k, p.view);
try testing.expectEqual(big, .{ term.cols, term.rows });
}
test "a view closing leaves the others as they were" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
const text = try numbered(gpa, 100);
defer gpa.free(text);
const pane = try p.setTestFile(text);
const a = p.openView().?;
const b = p.openView().?;
try testing.expectEqual(@as(usize, 3), count(p));
p.toView(b);
p.update(.{ .command = "Newcol" });
cursorTo(p, pane, 40);
typeKeys(p, "xy");
p.toView(a);
cursorTo(p, pane, 70);
p.toView(0);
cursorTo(p, pane, 10);
const ncol = p.ncol;
// The live one closing: the session goes into another.
p.toView(b);
p.closeView(b);
try testing.expect(p.view != b);
try testing.expectEqual(@as(usize, 2), count(p));
p.toView(a);
try testing.expectEqual(@as(i32, 70), pane.body.cur_row);
p.closeView(0);
try testing.expectEqual(a, p.view);
try testing.expectEqual(ncol, p.ncol);
// The last stays.
p.closeView(a);
try testing.expectEqual(@as(usize, 1), count(p));
try testing.expectEqual(@as(i32, 70), pane.body.cur_row);
}
test "drawing the other views leaves the live one as it was, to the digest" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const text = try numbered(gpa, 100);
defer gpa.free(text);
const pane = try p.setTestFile(text);
const k = p.openView().?;
p.toView(k);
cursorTo(p, pane, 60);
typeKeys(p, "dd");
p.toView(0);
cursorTo(p, pane, 95);
const before = pardes.walk.digest(Pardes, p);
try drawOthers(p, .{});
try testing.expectEqual(@as(u8, 0), p.view);
try testing.expectEqual(before, pardes.walk.digest(Pardes, p));
p.toView(k);
try testing.expectEqual(@as(i32, 60), pane.body.cur_row);
}
test "a view not live keeps its place in a text another view edits" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
const text = try numbered(gpa, 200);
defer gpa.free(text);
const pane = try p.setTestFile(text);
const k = p.openView().?;
p.toView(k);
cursorTo(p, pane, 100);
const scrolled = pane.file.?.scroll;
p.toView(0);
// Three lines put above it, one taken below it.
typeKeys(p, "xyppp");
cursorTo(p, pane, 150);
typeKeys(p, "xd");
p.toView(k);
try testing.expectEqual(@as(i32, 103), pane.body.cur_row);
try testing.expectEqual(scrolled + 3, pane.file.?.scroll);
try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101"));
}
test "a terminal shown in a view shorter than it keeps its cursor in sight" {
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 20 });
defer p.deinit();
const id = p.active;
const term = p.panes[id].?;
const k = p.openView().?;
p.toView(k);
p.update(.{ .resize = .{ .cols = 100, .rows = 50 } });
p.update(.{ .key = .{ .cp = Key.right } });
const rows = term.rows;
try testing.expect(rows > 30);
var out: std.ArrayList(u8) = .empty;
defer out.deinit(gpa);
for (1..rows) |i| try out.print(gpa, "row-{d}\r\n", .{i});
try out.appendSlice(gpa, "LAST$ ");
p.update(.{ .output = .{ .pane = @intCast(id), .bytes = out.items } });
// Sized for the tall view, it fits there whole: nothing is passed over.
try testing.expectEqual(@as(u16, 0), term.pan);
p.toView(0);
p.sync();
try testing.expectEqual(rows, term.rows);
try testing.expect(term.pan > 0);
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const s = try p.render(arena.allocator());
const r = p.rects[id];
// The cursor's row, the last, is on screen in the short view, inside
// the pane, and so is the line it is on.
const cursor = s.cursor.?;
try testing.expect(cursor.y >= r.y and cursor.y < r.y + r.h);
var row_text: std.ArrayList(u8) = .empty;
defer row_text.deinit(gpa);
for (0..s.cols) |x| try row_text.appendSlice(gpa, s.cells[@as(usize, cursor.y) * s.cols + x].grapheme());
try testing.expect(std.mem.indexOf(u8, row_text.items, "LAST$") != null);
// Back in the tall view, its top is where it was.
p.toView(k);
try testing.expectEqual(@as(u16, 0), term.pan);
}
|