diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:01:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:01:28 -0300 |
| commit | c62ddeed7b3b176f9a777e3670017542da898f17 (patch) | |
| tree | b9a7feb97a1673e9b45f10dd8153e0cf022121cb /src/terminal.zig | |
| parent | 2622b04dcf021ff36862a45b6e9c930e5671b9e2 (diff) | |
| download | pardes-c62ddeed7b3b176f9a777e3670017542da898f17.tar.gz pardes-c62ddeed7b3b176f9a777e3670017542da898f17.zip | |
terminal: a position that cannot follow a terminal's output for want of memory is said through setMessage, logged, animated and taped as every other message is, where it was written straight into the pane's message row: ingestWithPositions answers whether a position was lost and feedOutput says so; a test provokes it with a failing allocator and fails without the fix
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/terminal.zig')
| -rw-r--r-- | src/terminal.zig | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/src/terminal.zig b/src/terminal.zig index 04e4f233..ca9b50f2 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1197,6 +1197,10 @@ test "a restored terminal keeps its history above a marker and gets a shell wher try std.testing.expect(old_at < mark_at); } +/// What a pane says when a position kept over its terminal's rows could +/// not follow the output (no memory for its pin), so it was reset. +const positions_lost = "terminal edit position reset: out of memory"; + /// Record and parse one live pty read, invalidate its motion surface, and /// follow it only when the body (possibly parked under a tag edit) is raw. pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { @@ -1210,7 +1214,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // A replica's emulator is what the authority's deltas make it; what // pardes keeps over it moves as the authority's ingest told it. if (pardes.tape.screenReplica()) { - if (has_positions) ingestWithPositions(pane, bytes); + if (has_positions and ingestWithPositions(pane, bytes)) if (p.paneIdOf(pane)) |id| p.setMessage(id, positions_lost); return; } // Sticky bottom, as every other terminal does it: output drags the @@ -1218,7 +1222,9 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // a reader scrolled back into the scrollback stays where they scrolled. const at_bottom = atBottom(pane); syncPixels(p, pane); - if (has_positions) ingestWithPositions(pane, bytes) else ingest(pane, bytes); + if (!has_positions) { + ingest(pane, bytes); + } else if (ingestWithPositions(pane, bytes)) if (p.paneIdOf(pane)) |id| p.setMessage(id, positions_lost); p.shell_rows.markStale(pane); if (pane.body.mode == .tty and at_bottom) followOutput(pane); } @@ -1294,7 +1300,10 @@ fn told(t: Told) Told { return t; } -fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { +/// `ingest`, with every position kept over the rows (the edit copy, the +/// cursors and selections, their undo, the pointer's rows) moved as the +/// rows move. True when one could not be kept (no memory for its pin). +fn ingestWithPositions(pane: *Pane, bytes: []const u8) bool { const screens = &pane.terminal.?.vt.screens; const key = screens.active_key; const generation = screens.generation(key); @@ -1398,9 +1407,9 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { // pointer's rows. var rows_buf: [512]i32 = undefined; var erased_buf: [512]bool = undefined; - const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return; + const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return true; defer if (n > rows_buf.len) pane.gpa.free(rows_after); - const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return; + const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return true; defer if (n > erased_buf.len) pane.gpa.free(erased_after); @memset(rows_after, 0); @memset(erased_after, false); @@ -1418,7 +1427,7 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { }; } const after = pardes.tape.value(.positions, told, .{Told{ .rows = rows_after, .erased = erased_after, .cleared = cleared, .alive = alive != null, .failed = failed }}); - if (after.rows.len != n or after.erased.len != n) return; // a replica its authority's states apart (diverged) + if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged) failed = after.failed; const at_position = 2 * nspans; const at_pointer = at_position + npositions; @@ -1500,11 +1509,28 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { moved = true; }; if (moved) pane.nsel_snap = 0; // Saved regex-preview offsets name the old flat text. - if (failed) { - const message = "terminal edit position reset: out of memory"; - @memcpy(pane.msg[0..message.len], message); - pane.msg_len = message.len; - } + return failed; +} + +test "a position that cannot follow the output for want of memory is said on the message row, and logged" { + if (comptime !enabled) return error.SkipZigTest; + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); + const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = p.panes[0].?; + feedOutput(p, pane, "one\r\ntwo\r\n"); + // More positions than the emulator's pins have room for: following them + // through the output needs memory, and there is none. + try std.testing.expect(pane.body.reserveSels()); + for (pane.body.sels[0..8], 0..) |*range, i| range.* = .{ .row = @intCast(i), .col = 0, .arow = @intCast(i), .acol = 1 }; + pane.body.nsel = 8; + failing.fail_index = failing.alloc_index; + feedOutput(p, pane, "three\r\n"); + failing.fail_index = std.math.maxInt(usize); + try std.testing.expect(failing.has_induced_failure); + try std.testing.expectEqualStrings(positions_lost, pane.msg[0..pane.msg_len]); + const logged = p.messages.at(p.messages.len -| 1) orelse return error.NothingLogged; + try std.testing.expectEqualStrings(positions_lost, logged.slice()); } test "raw terminal output needs no position tracking allocations" { |
