summaryrefslogtreecommitdiff
path: root/src/terminal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/terminal.zig')
-rw-r--r--src/terminal.zig125
1 files changed, 88 insertions, 37 deletions
diff --git a/src/terminal.zig b/src/terminal.zig
index 6c3eeda7..fcb284a0 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -161,6 +161,10 @@ pub const State = struct {
/// Counts the emulator's writes (output, a resize, its view scrolled):
/// a new count is a new state for its replicas (screen_state.Track).
changes: u32 = 0,
+ /// A step read its rows (its history too): a replica replaying that
+ /// step must hold them, so its authority sends the ones it does not
+ /// before the step's unit (replica.Screens.fill).
+ read: bool = false,
/// The emulator as its program has made it, for the replay digest
/// (src/walk.zig): the active screen's text, how much history is above
@@ -855,6 +859,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa });
state.reply_len = 0;
state.changes = 0;
+ state.read = false;
// Every field its default (create leaves them what the heap had, which
// a digest then reads: two replicas must agree, src/replica.zig).
state.reply_dropped = 0;
@@ -1072,11 +1077,15 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void {
if (comptime !enabled) return;
if (pane.terminal == null) return;
if (authorityOnly()) return;
- // A replica's emulator is what the authority's deltas make it.
- if (pardes.tape.screenReplica()) return;
const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or
pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or
pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0;
+ // 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);
+ return;
+ }
// Sticky bottom, as every other terminal does it: output drags the
// viewport down only when the viewport was already on the last row, so
// a reader scrolled back into the scrollback stays where they scrolled.
@@ -1095,7 +1104,9 @@ const PositionPin = struct {
edit_row: ?i32 = null,
};
-fn trackRow(pages: *ghostty_vt.PageList, row: i32, failed: *bool) RowPin {
+fn trackRow(maybe_pages: ?*ghostty_vt.PageList, row: i32, failed: *bool) RowPin {
+ // A screen-state replica's: told by the tape, not its emulator.
+ const pages = maybe_pages orelse return .{ .offset = row };
if (row < 0) return .{ .offset = row };
const bounded: usize = @min(@as(usize, @intCast(row)), pages.total_rows - 1);
var left = pages.total_rows - 1 - bounded;
@@ -1125,7 +1136,7 @@ fn pinnedRow(pages: ?*ghostty_vt.PageList, row: RowPin) i32 {
return 0;
}
-fn trackPosition(pages: *ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin {
+fn trackPosition(pages: ?*ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin {
var grid = target.*;
if (overlay) |edit| {
const lines: i32 = @intCast(modal.lineCount(edit.text));
@@ -1144,11 +1155,24 @@ fn pointerRowCount(pane: *const Pane) usize {
return count;
}
+/// What an ingest told the positions over it (`ingestWithPositions`): each
+/// tracked row where it is after it (the spans' starts and ends, the
+/// positions, the pointer rows, in that order) and whether the program
+/// erased it, whether the screen was cleared, whether its pages lived and
+/// whether a pin could not be made. Taped: a screen-state replica, which
+/// ingests nothing, is told the authority's.
+const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool };
+
+fn told(t: Told) Told {
+ return t;
+}
+
fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
const screens = &pane.terminal.?.vt.screens;
const key = screens.active_key;
const generation = screens.generation(key);
- const pages = &screens.active.pages;
+ const replica = pardes.tape.screenReplica();
+ const pages: ?*ghostty_vt.PageList = if (replica) null else &screens.active.pages;
const Group = struct { overlay: ?*EditBuffer, cursor: *i32, selection: *panes.Text.CharSel };
var groups: [history_max * 2 + 1]Group = undefined;
groups[0] = .{
@@ -1230,31 +1254,61 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
}
};
} else failed = true;
- // Whether the program cleared the screen (ED 2, a switch of screens);
- // pardes reads the flag nowhere else.
- pane.terminal.?.vt.flags.dirty.clear = false;
- ingest(pane, bytes);
- const cleared = pane.terminal.?.vt.flags.dirty.clear;
- // RIS can destroy the alternate screen and every pin it owned.
- const alive = if (screens.generation(key) == generation) pages else null;
- var moved = false;
- var pointer_rows_lost = alive == null or pointer_positions == null;
- if (pointer_positions) |pins| for (pins) |position| {
- if (alive != null) if (position.row.pin) |pin| {
- if (pin.garbage) pointer_rows_lost = true;
+ var cleared = false;
+ var alive: ?*ghostty_vt.PageList = null;
+ if (!replica) {
+ // Whether the program cleared the screen (ED 2, a switch of
+ // screens); pardes reads the flag nowhere else.
+ pane.terminal.?.vt.flags.dirty.clear = false;
+ ingest(pane, bytes);
+ cleared = pane.terminal.?.vt.flags.dirty.clear;
+ // RIS can destroy the alternate screen and every pin it owned.
+ alive = if (screens.generation(key) == generation) pages else null;
+ }
+ const pointers: []PositionPin = pointer_positions orelse &.{};
+ const n = 2 * nspans + npositions + pointers.len;
+ // On the stack, as the local path never allocated here but for the
+ // 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;
+ 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;
+ defer if (n > erased_buf.len) pane.gpa.free(erased_after);
+ @memset(rows_after, 0);
+ @memset(erased_after, false);
+ if (!replica) {
+ var k: usize = 0;
+ for (spans[0..nspans]) |span| for ([_]RowPin{ span.start, span.end }) |rp| {
+ rows_after[k] = pinnedRow(alive, rp);
+ erased_after[k] = alive != null and rp.pin != null and rp.pin.?.garbage;
+ k += 1;
+ };
+ for ([_][]PositionPin{ positions[0..npositions], pointers }) |batch| for (batch) |position| {
+ rows_after[k] = pinnedRow(alive, position.row);
+ erased_after[k] = alive != null and position.row.pin != null and position.row.pin.?.garbage;
+ k += 1;
};
+ }
+ 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)
+ failed = after.failed;
+ const at_position = 2 * nspans;
+ const at_pointer = at_position + npositions;
+ var moved = false;
+ var pointer_rows_lost = !after.alive or pointer_positions == null;
+ for (0..pointers.len) |i| if (after.erased[at_pointer + i]) {
+ pointer_rows_lost = true;
};
// The program cleared the screen, or the rows the live copy stood on are
// gone (pruned, a reset): the copy goes with them, as what the program
// drew next. Kept, ghostty would move its rows to the top of what is
// left, over the program's new text.
- var copy_lost = cleared or alive == null;
- for (spans[0..nspans]) |span| {
- if (span.overlay == overlay) if (span.start.pin) |pin| if (alive != null and pin.garbage) {
- copy_lost = true;
- };
- const start = pinnedRow(alive, span.start);
- const rows = @max(1, pinnedRow(alive, span.end) -| start +| 1);
+ var copy_lost = after.cleared or !after.alive;
+ for (spans[0..nspans], 0..) |span, i| {
+ if (span.overlay == overlay and after.erased[2 * i]) copy_lost = true;
+ const start = after.rows[2 * i];
+ const rows = @max(1, after.rows[2 * i + 1] -| start +| 1);
moved = moved or start != span.overlay.row or rows != span.overlay.rows;
span.overlay.row = start;
span.overlay.rows = rows;
@@ -1266,23 +1320,16 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
// The rule above for the copy holds for the selections: one whose row
// is gone (ED 3 erased its history row, which ghostty flags `garbage`)
// goes too, rather than being clamped to the top.
- const erased = struct {
- fn at(owner: ?*ghostty_vt.PageList, all: []const PositionPin, index: usize) bool {
- if (owner == null) return false;
- const pin = all[index].row.pin orelse return false;
- return pin.garbage;
- }
- }.at;
+ const erased = after.erased[at_position..];
const vsel_lost = if (vsel_at) |i|
- erased(alive, positions[0..npositions], i) or (copy_lost and positions[i].edit_row != null)
+ erased[i] or (copy_lost and positions[i].edit_row != null)
else
false;
- const msel_lost = if (msel_at) |i| erased(alive, positions[0..npositions], i) or erased(alive, positions[0..npositions], i + 1) else false;
+ const msel_lost = if (msel_at) |i| erased[i] or erased[i + 1] else false;
var sels_keep: [panes.Text.max_selections]bool = undefined;
- for (0..pane.body.nsel) |i| sels_keep[i] = !(erased(alive, positions[0..npositions], sels_at + 2 * i) or
- erased(alive, positions[0..npositions], sels_at + 2 * i + 1));
- for ([_][]PositionPin{ positions[0..npositions], pointer_positions orelse &.{} }) |batch| for (batch) |position| {
- var row = pinnedRow(alive, position.row);
+ for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]);
+ for ([_][]PositionPin{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |position, k| {
+ var row = after.rows[k];
if (position.overlay) |edit| {
if (position.edit_row) |relative| {
row = edit.row +| relative;
@@ -1887,7 +1934,11 @@ pub fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void {
}
+/// Set while a frame is drawn: what it reads is no step's.
+pub var drawing = false;
+
fn buildRows(alloc: std.mem.Allocator, p: *Pardes, pane: *Pane) !Rows {
+ if (!drawing) pane.terminal.?.read = true;
const full = try pane.terminal.?.vt.screens.active.dumpStringAlloc(p.scratch.allocator(), .{ .screen = .{} });
var pit = pane.terminal.?.vt.screens.active.pages.rowIterator(.right_down, .{ .screen = .{} }, null);
// split yields one more item than delimiters; the extra final slot is the