summaryrefslogtreecommitdiff
path: root/src/terminal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/terminal.zig')
-rw-r--r--src/terminal.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/terminal.zig b/src/terminal.zig
index bcb70110..6c3eeda7 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -158,6 +158,9 @@ pub const State = struct {
mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {},
/// The kitty graphics its program placed, as drawn (TerminalImages.zig).
images: TerminalImages = if (images_enabled) .{} else {},
+ /// 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,
/// 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
@@ -169,6 +172,15 @@ pub const State = struct {
/// on (docs/next-daemon.md).
pub fn digest(s: *const State, h: *pardes.walk.Hasher) void {
if (comptime !enabled) return;
+ // Under screen-state terminals the emulator is the authority's, and
+ // a replica's is what the last delta made it: compared as screens
+ // (test/replicas.zig), not step by step.
+ if (pardes.tape.screenState()) return;
+ s.digestScreen(h);
+ }
+
+ pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher) void {
+ if (comptime !enabled) return;
const screen = s.vt.screens.active;
const sb = screen.pages.scrollbar();
const cur = screen.cursor;
@@ -531,6 +543,41 @@ pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 {
return @intCast(@min(pane.rows - shown, (at + 1) -| shown));
}
+/// What pardes made of a terminal's stream that its emulator does not hold
+/// (the OSC 133 cycle, MarkedHandler), for a replica's delta: phase u8,
+/// started, prompts, declined u32, continuation and lost u8, a finished
+/// command's seq u32 and status i32 (all ones: none; i32 min: no status).
+// ponytail: the output pins stay the authority's (its 9P pty/run reads them).
+pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4;
+
+pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
+ if (comptime !enabled) return &.{};
+ const m = &state.stream.handler;
+ var w: std.Io.Writer = .fixed(buf);
+ w.writeByte(@intFromEnum(m.phase)) catch unreachable;
+ inline for (.{ m.started, m.prompts, m.declined }) |n| w.writeInt(u32, n, .little) catch unreachable;
+ w.writeAll(&.{ @intFromBool(m.continuation), @intFromBool(m.lost) }) catch unreachable;
+ w.writeInt(u32, if (m.finished) |f| f.seq else std.math.maxInt(u32), .little) catch unreachable;
+ w.writeInt(i32, if (m.finished) |f| f.status orelse std.math.minInt(i32) else 0, .little) catch unreachable;
+ return w.buffered();
+}
+
+/// A replica's terminal takes its authority's marks (`marksOf`).
+pub fn takeMarks(state: *State, bytes: []const u8) void {
+ if (comptime !enabled) return;
+ if (bytes.len != marks_len) return;
+ const m = &state.stream.handler;
+ m.phase = @enumFromInt(bytes[0]);
+ m.started = std.mem.readInt(u32, bytes[1..5], .little);
+ m.prompts = std.mem.readInt(u32, bytes[5..9], .little);
+ m.declined = std.mem.readInt(u32, bytes[9..13], .little);
+ m.continuation = bytes[13] != 0;
+ m.lost = bytes[14] != 0;
+ const seq = std.mem.readInt(u32, bytes[15..19], .little);
+ const status = std.mem.readInt(i32, bytes[19..23], .little);
+ m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status };
+}
+
/// A replica's speculation leaves an emulator be: it is the authority's
/// (docs/next-daemon.md, phase 5), so the input waits for its order.
fn authorityOnly() bool {
@@ -545,6 +592,7 @@ pub fn scrollGrid(pane: *Pane, delta: i32) void {
if (authorityOnly()) return;
const state = pane.terminal orelse return;
state.vt.screens.active.scroll(.{ .delta_row = delta });
+ state.changes +%= 1;
}
/// Is the viewport already showing the last row of output?
@@ -559,7 +607,9 @@ pub fn followOutput(pane: *Pane) void {
if (comptime !enabled) return;
if (authorityOnly()) return;
const state = pane.terminal orelse return;
+ const moved = state.vt.screens.active.pages.viewport != .active;
state.vt.screens.active.scroll(.active);
+ state.changes +%= @intFromBool(moved);
}
/// Reflow the grid. A failed reflow keeps the grid it had rather than dropping
@@ -572,6 +622,9 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi
pane.clearPointerSelection(button);
pane.sel[button].state = .none;
};
+ // A replica's emulator is sized by its authority: the next delta says.
+ if (pardes.tape.screenReplica()) return;
+ state.changes +%= 1;
state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {};
}
@@ -801,6 +854,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;
// 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;
@@ -856,6 +910,7 @@ pub fn reviveShell(pane: *Pane) void {
/// then apply different views.
fn ingest(pane: *Pane, bytes: []const u8) void {
pane.terminal.?.stream.nextSlice(bytes);
+ pane.terminal.?.changes +%= 1;
}
test "replay ownership is terminal-only and construction rolls back on allocation failure" {
@@ -1017,6 +1072,8 @@ 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;