summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/term/terminal.zig130
1 files changed, 79 insertions, 51 deletions
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index b1beff15..45c01841 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -1586,60 +1586,88 @@ test "a view's place at the history's cap: it keeps showing its line while outpu
}
test "a view's place is carried through output, the history's cap and reflows as ghostty carries a view of its own, a row's first cell each time (a fuzz)" {
- // The oracle: an emulator scrolled by ghostty itself to the same row,
- // fed and resized alike, its view put at its row's first cell after a
- // reflow (a widening can leave ghostty's mid-row: pardes-term-join-6),
- // and at the output when it reaches the active area.
- const gpa = std.testing.allocator;
var seed: u64 = 0;
- while (seed < 300) : (seed += 1) {
- var prng: std.Random.DefaultPrng = .init(seed);
- const rnd = prng.random();
- var cols: u16 = rnd.intRangeAtMost(u16, 20, 60);
- var rows: u16 = rnd.intRangeAtMost(u16, 2, 6);
- const ours = try open(gpa, cols, rows);
- defer close(ours);
- const oracle = try open(gpa, cols, rows);
- defer close(oracle);
- const capped = rnd.boolean();
- for ([_]*State{ ours, oracle }) |s| if (capped) s.vt.screens.active.pages.setMaxBytes(1);
- var places = [_]?usize{null};
- var line: [200]u8 = undefined;
- for (0..80) |_| {
- switch (rnd.uintLessThan(u8, 4)) {
- 0, 1 => for (0..rnd.intRangeAtMost(usize, 1, 400)) |i| {
- const len = rnd.intRangeAtMost(usize, 0, 150);
- for (line[0..len], 0..) |*c, j| c.* = 'a' + @as(u8, @intCast((i + j) % 26));
- _ = feed(ours, line[0..len], &.{}, &.{}, &.{}, &places);
- _ = feed(ours, "\r\n", &.{}, &.{}, &.{}, &places);
- ingest(oracle, line[0..len]);
- ingest(oracle, "\r\n");
- },
- 2 => {
- if (rnd.boolean()) cols = rnd.intRangeAtMost(u16, 20, 60);
- rows = rnd.intRangeAtMost(u16, 2, 6);
- resize(ours, cols, rows, &places);
- oracle.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {};
- const pages = &oracle.vt.screens.active.pages;
- if (pages.viewport == .pin) pages.viewport_pin.x = 0;
- },
- else => {
- const sb = ours.vt.screens.active.pages.scrollbar();
- const to = rnd.uintLessThan(usize, sb.total);
- places[0] = if (to >= sb.total - sb.len) null else to;
- oracle.vt.screens.active.scroll(.{ .row = to });
- },
- }
- const pages = &oracle.vt.screens.active.pages;
- const sb = pages.scrollbar();
- if (sb.offset >= sb.total - sb.len) pages.scroll(.active);
- const want: ?usize = if (pages.viewport == .active) null else pages.scrollbar().offset;
- if (want != places[0]) {
- std.debug.print("seed {d}: carried {?d}, ghostty's own {?d}\n", .{ seed, places[0], want });
- return error.TestExpectedEqual;
- }
+ while (seed < 300) : (seed += 1) _ = try carryFuzz(std.testing.allocator, seed);
+}
+
+test "a scrolled-back view stays on its line through a narrowing that grows the rows, where ghostty's own went to the output in the middle of the resize (the carry's fuzz, seed 1956)" {
+ // 44x3, the view on row 23 (the active area 24-26), resized to 21x6:
+ // ghostty grows the rows first (the active area 21-26 takes the view's
+ // row: to the output), then reflows (the line on rows 46-47 of 55, the
+ // active area 49-54). Its history is all there: the view stays on it.
+ try std.testing.expectEqual(@as(usize, 1), try carryFuzz(std.testing.allocator, 1956));
+}
+
+/// One run of the carry's fuzz: 80 random reads of output (lines 0-150
+/// long, wrapped), resizes and scrolls of two emulators alike, a view's
+/// place carried in one (`feed`, `resize`) and ghostty's own view in the
+/// other (the oracle): its view put at its row's first cell after a reflow
+/// (a widening can leave ghostty's mid-row: pardes-term-join-6), and at the
+/// output when it reaches the active area. Where ghostty's view went to the
+/// output only in the MIDDLE of a resize and its row is in the history
+/// again after it (ghostty grows the rows before a narrowing's reflow,
+/// PageList.resizeWithoutReflow, and checks before a widening's growth,
+/// PageList.resizeCols: an order of its own, nothing under the view gone),
+/// the view stays on its line (pardes-term-join-11): the oracle is put back
+/// on the row a pin of its own was carried to through the resize, and the
+/// run counts it. How many it put back.
+fn carryFuzz(gpa: std.mem.Allocator, seed: u64) !usize {
+ var prng: std.Random.DefaultPrng = .init(seed);
+ const rnd = prng.random();
+ var cols: u16 = rnd.intRangeAtMost(u16, 20, 60);
+ var rows: u16 = rnd.intRangeAtMost(u16, 2, 6);
+ const ours = try open(gpa, cols, rows);
+ defer close(ours);
+ const oracle = try open(gpa, cols, rows);
+ defer close(oracle);
+ const capped = rnd.boolean();
+ for ([_]*State{ ours, oracle }) |s| if (capped) s.vt.screens.active.pages.setMaxBytes(1);
+ const pages = &oracle.vt.screens.active.pages;
+ var places = [_]?usize{null};
+ var line: [200]u8 = undefined;
+ var kept: usize = 0;
+ for (0..80) |_| {
+ switch (rnd.uintLessThan(u8, 4)) {
+ 0, 1 => for (0..rnd.intRangeAtMost(usize, 1, 400)) |i| {
+ const len = rnd.intRangeAtMost(usize, 0, 150);
+ for (line[0..len], 0..) |*c, j| c.* = 'a' + @as(u8, @intCast((i + j) % 26));
+ _ = feed(ours, line[0..len], &.{}, &.{}, &.{}, &places);
+ _ = feed(ours, "\r\n", &.{}, &.{}, &.{}, &places);
+ ingest(oracle, line[0..len]);
+ ingest(oracle, "\r\n");
+ },
+ 2 => {
+ if (rnd.boolean()) cols = rnd.intRangeAtMost(u16, 20, 60);
+ rows = rnd.intRangeAtMost(u16, 2, 6);
+ resize(ours, cols, rows, &places);
+ const held: ?*ghostty_vt.Pin = if (pages.viewport == .pin) pages.trackPin(pages.viewport_pin.*) catch null else null;
+ oracle.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {};
+ if (pages.viewport == .pin) pages.viewport_pin.x = 0;
+ if (held) |pin| {
+ defer pages.untrackPin(pin);
+ const row: usize = @intCast(pinnedRow(pages, .{ .pin = pin }));
+ if (pages.viewport == .active and !pin.garbage and row < pages.total_rows - pages.rows) {
+ pages.scroll(.{ .row = row });
+ kept += 1;
+ }
+ }
+ },
+ else => {
+ const sb = ours.vt.screens.active.pages.scrollbar();
+ const to = rnd.uintLessThan(usize, sb.total);
+ places[0] = if (to >= sb.total - sb.len) null else to;
+ oracle.vt.screens.active.scroll(.{ .row = to });
+ },
+ }
+ const sb = pages.scrollbar();
+ if (sb.offset >= sb.total - sb.len) pages.scroll(.active);
+ const want: ?usize = if (pages.viewport == .active) null else pages.scrollbar().offset;
+ if (want != places[0]) {
+ std.debug.print("seed {d}: carried {?d}, ghostty's own {?d}\n", .{ seed, places[0], want });
+ return error.TestExpectedEqual;
}
}
+ return kept;
}
test "the luminance table answers exactly what ghostty computes" {