summaryrefslogtreecommitdiff
path: root/core/term/terminal.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 16:50:34 -0300
committerGabriel Schneider <[email protected]>2026-11-22 16:53:19 -0300
commit9863a63b614d200852853825031189364dcacfdc (patch)
treed235648a2ac14b3e311746ced3433ed6865e2061 /core/term/terminal.zig
parent6544f73a6b50e5ba68564beea7784593bb54348e (diff)
parentbad506bd45846e012246a33b5d3097b3e92f02bc (diff)
downloadpardes-9863a63b614d200852853825031189364dcacfdc.tar.gz
pardes-9863a63b614d200852853825031189364dcacfdc.zip
integration: the old road deleted and the emulator's boundary on the gathered line: pardes-merge56 (`qvwrwxtu`: the installed tree and the source view's changes to their last slice) and pardes-term-join-9 (`twssqzqw`, fourteen changes on pardes-merge55: every client's terminals by the pty's bytes and nothing else: the delta protocol, its flow control and core/term/screen_state.zig deleted (-3,605 +504 lines), the wire at 30, one soak; S1: abi/term.zig's Ops in replicated and local, the terminal's digest covering what a step reads, every call of the core's into a terminal listed with its rule as a test; a client's own join again judged after its goodbye; a pty's size one fact the core hands its host for every fork, adopt and resize; a closed view sizes no terminal); no textual conflict; one semantic one, resolved here: core/jj.zig's Churn test (the layers' last round) called Authority.init and Replica.init with the road's flag the deletion took away: the argument removed; core-test 916/1073 (157 skipped), shell-test 1115/1118; its gate: .scratch/sketch/reorg/merge57, the numbers in PARDES.md (2026-11-22)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/term/terminal.zig')
-rw-r--r--core/term/terminal.zig230
1 files changed, 63 insertions, 167 deletions
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index 187ed144..44038a18 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -5,7 +5,6 @@ const std = @import("std");
const plain = @import("plain");
const Key = plain.Key;
const Term = plain.term;
-const screen_state = @import("screen_state.zig");
const ghostty_vt = @import("ghostty-vt");
const Ink = plain.term.Ink;
@@ -183,18 +182,6 @@ pub const State = struct {
reply_dropped: u32 = 0,
/// 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,
- /// 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 size a step gave it, on an authority with screen-state replicas:
- /// made after the step's unit (`resizeDeferred`), so that the rest of
- /// the step reads the grid it had, as its replicas' replays do, and the
- /// rows sent them for it (Screens.fill) are of that grid's numbering.
- resize_to: ?struct { cols: u16, rows: u16 } = null,
/// The stream over the emulator, its answers to the program and its
/// size reports routed here, PNG decoded: what `create` and a dump's
@@ -224,9 +211,22 @@ pub fn digest(s: *const State, h: *std.hash.Wyhash, view: bool) void {
const sb = screen.pages.scrollbar();
const cur = screen.cursor;
const offset = if (view) sb.offset else 0;
- const facts = .{ sb.total, offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped };
+ // Everything a step reads of it (abi/term.zig `replicated`): its
+ // size, the view and the cursor, its modes (the alternate screen,
+ // the mouse's, focus reports, bracketed paste: `modes`), whether it
+ // holds a kitty image (the tag says so: tagline.zig), the reply
+ // buffer, the OSC 133 cycle (`prompt`), the active area's text. Not
+ // the history's text, only its length (a step's read of it: not
+ // covered; core/Term.zig `calls`).
+ const Modes = @TypeOf(s.vt.modes.values);
+ const mode_bits: @Int(.unsigned, @bitSizeOf(Modes)) = @bitCast(s.vt.modes.values);
+ const modes_word: @Int(.unsigned, (@bitSizeOf(Modes) + 7) / 8 * 8) = mode_bits;
+ const mouse_flags: [2]u8 = .{ @intCast(@intFromEnum(s.vt.flags.mouse_event)), @intCast(@intFromEnum(s.vt.flags.mouse_format)) };
+ const facts = .{ s.vt.cols, s.vt.rows, sb.total, offset, sb.len, cur.x, cur.y, modes_word, s.vt.screens.active_key, mouse_flags, s.reply_dropped, hasImages(s) };
inline for (facts) |fact| h.update(std.mem.asBytes(&fact));
h.update(s.reply[0..s.reply_len]);
+ var marks: [marks_len]u8 = undefined;
+ h.update(marksOf(s, &marks));
var buf: [4096]u8 = undefined;
var text: std.Io.Writer.Hashing(std.hash.Wyhash) = .initHasher(h.*, &buf);
if (screen.pages.getBottomRight(.active)) |br|
@@ -338,11 +338,10 @@ pub fn reload(s: *State) !u64 {
var reply: [@typeInfo(@FieldType(State, "reply")).array.len]u8 = undefined;
@memcpy(reply[0..saved.reply.len], saved.reply);
saved.reply = reply[0..saved.reply.len];
- const kept = .{ s.changes, s.read, s.resize_to };
if (comptime images_enabled) s.images.deinit(gpa);
s.stream.deinit();
s.vt.deinit(gpa);
- s.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined, .changes = kept[0] +% 1, .read = kept[1], .resize_to = kept[2] };
+ s.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined };
// Short of room for its sequence in flight it goes on without it (lost
// at the next cut), as ghostty's stream does: not worth a terminal.
_ = install(s, decoded.continuation, &saved);
@@ -470,13 +469,12 @@ pub fn grid(state: ?*const State) Term.Grid {
}
/// 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;
+/// (the OSC 133 cycle, MarkedHandler), as bytes its digest holds: 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).
+const marks_len = 1 + 4 * 3 + 2 + 4 + 4;
-pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
+fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
const m = &state.stream.handler;
var w: std.Io.Writer = .fixed(buf);
w.writeByte(@backingInt(m.phase)) catch unreachable;
@@ -487,121 +485,43 @@ pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
return w.buffered();
}
-/// A replica's terminal takes its authority's marks (`marksOf`).
-pub fn takeMarks(state: *State, bytes: []const u8) void {
- if (bytes.len != marks_len) return;
- const m = &state.stream.handler;
- m.phase = @fromBackingInt(@intCast(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 };
-}
-
-// ---- its replicas: the screen state they are sent (screen_state.zig) ----
-
-/// What `peer` is owed of it, a delta against what it was sent (with the
-/// marks of its prompt cycle), onto `out`; false when it has it already.
-pub fn encode(state: *State, enc: *screen_state.Encoder, tr: *screen_state.Track, peer: *screen_state.Peer, out: *std.Io.Writer) !bool {
- var marks: [marks_len]u8 = undefined;
- return screen_state.encode(enc, tr, peer, &state.vt, state.changes, marksOf(state, &marks), out);
-}
-
-/// Its rows `lo` to `hi` (absolute), as a replica holding them as holes
-/// is sent them.
-pub fn encodeRows(state: *State, enc: *screen_state.Encoder, tr: *screen_state.Track, lo: u64, hi: u64, out: *std.Io.Writer) !void {
- return screen_state.encodeRows(enc, tr, &state.vt, lo, hi, out);
-}
-
-/// What a session's authority keeps to encode its terminals for its
-/// peers, and what it keeps of one terminal for them: made and freed.
-pub fn encoder(gpa: std.mem.Allocator) !*screen_state.Encoder {
- const e = try gpa.create(screen_state.Encoder);
- e.* = .init(gpa);
- return e;
-}
-
-pub fn encoderFree(e: *screen_state.Encoder) void {
- const gpa = e.gpa;
- e.deinit();
- gpa.destroy(e);
-}
-
-pub fn track(gpa: std.mem.Allocator) !*screen_state.Track {
- const t = try gpa.create(screen_state.Track);
- t.* = .init(gpa);
- return t;
-}
-
-/// A replica's side of one terminal: made, freed, where its active area
-/// begins, and the history rows `lo..hi` it does not hold.
-pub fn replica(gpa: std.mem.Allocator) !*screen_state.Replica {
- const r = try gpa.create(screen_state.Replica);
- r.* = .{};
- return r;
-}
-
-pub fn replicaFree(r: *screen_state.Replica, gpa: std.mem.Allocator) void {
- r.deinit(gpa);
- gpa.destroy(r);
-}
-
-pub fn replicaTop(r: *const screen_state.Replica) u64 {
- return r.top;
-}
-
-pub fn missing(r: *const screen_state.Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void {
- return r.missing(lo, hi, out, gpa);
-}
-
-/// What `track` kept of its versions, freed; `state` null when its pane is
-/// gone with the emulator.
-/// A peer's track of it let go, and freed (`state` null: it is gone).
-pub fn untrack(t: *screen_state.Track, gpa: std.mem.Allocator, state: ?*State) void {
- t.deinit(gpa, if (state) |s| &s.vt else null);
- gpa.destroy(t);
-}
-
-/// Whether a step read its rows since `clearRead` (`markRead`).
-pub fn wasRead(state: *const State) bool {
- return state.read;
-}
-
-pub fn clearRead(state: *State) void {
- state.read = false;
-}
-
-/// A replica's terminal takes its authority's delta (and the marks riding
-/// it); error.BaseMismatch when it is against a state not held here.
-pub fn apply(state: *State, rep: *screen_state.Replica, bytes: []const u8) !Term.Applied {
- const applied = try rep.apply(state.gpa, terminalIo(), &state.vt, bytes);
- takeMarks(state, applied.extra);
- return applied;
-}
-
-/// ...and rows it asked for.
-pub fn applyRows(state: *State, rep: *screen_state.Replica, bytes: []const u8) !void {
- return rep.applyRows(state.gpa, &state.vt, bytes);
-}
-
-/// The absolute rows its view shows on a replica.
-pub fn shown(state: *State, rep: *const screen_state.Replica) [2]u64 {
- return rep.shown(&state.vt);
-}
-
-/// Its screen as text, every row's cells and flags (the replica tests
-/// compare an authority's and a replica's).
-pub fn dumpScreen(state: *State, w: *std.Io.Writer) !void {
- return screen_state.dumpScreen(&state.vt, w);
+/// What a row shows, written out value by value: its flags, then each
+/// cell's word without its page's ids, its style, its grapheme and its
+/// hyperlink's URI.
+fn dumpRow(page: *ghostty_vt.page.Page, row: *ghostty_vt.page.Row, cols: u16, w: *std.Io.Writer) !void {
+ try w.print("{d}:", .{@as(u8, @intFromBool(row.wrap)) | @as(u8, @intFromBool(row.wrap_continuation)) << 1 | @as(u8, @backingInt(row.semantic_prompt)) << 2});
+ const all = page.getCells(row);
+ for (all[0..@min(cols, all.len)]) |*c| {
+ var word = c.*;
+ word.style_id = 0;
+ try w.print(" {x}", .{@as(u64, @bitCast(word))});
+ if (c.style_id != 0) try ghostty_vt.snapshot.style.encode(page.styles.get(page.memory, c.style_id).*, w);
+ if (c.hasGrapheme()) for (page.lookupGrapheme(c).?) |cp| try w.print("+{x}", .{cp});
+ if (c.hyperlink) if (page.lookupHyperlink(c)) |id| try w.print("@{s}", .{page.hyperlink_set.get(page.memory, id).uri.slice(page.memory)});
+ }
+ try w.writeByte('\n');
}
-/// Rows `lo` to `hi` shown blank on a replica until they come.
-pub fn blank(state: *State, rep: *const screen_state.Replica, lo: u64, hi: u64) void {
- rep.blank(&state.vt, lo, hi);
+/// What an emulator shows, cell for cell: the terminal-wide state, the
+/// cursor, the history's extent and the viewport, the active screen's rows
+/// (test/replicas.zig compares a client's terminal with its session's by it).
+pub fn dumpScreen(t: *ghostty_vt.Terminal, w: *std.Io.Writer) !void {
+ const screen = t.screens.active;
+ const sb = t.screens.get(.primary).?.pages.scrollbar();
+ try w.print("{d}x{d} {s} cursor {d},{d} {s} {} {s} title {s} pwd {s} scrollbar {d} {d} {d}\n", .{
+ t.cols, t.rows, @tagName(t.screens.active_key),
+ screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style),
+ screen.cursor.pending_wrap, @tagName(screen.cursor.semantic_content), t.getTitle() orelse "",
+ t.getPwd() orelse "", sb.total, sb.offset,
+ sb.len,
+ });
+ try w.writeAll(std.mem.asBytes(&t.modes.values));
+ try w.writeAll(std.mem.asBytes(&screen.kitty_keyboard));
+ try w.print(" mouse {s} {s} {s}\n", .{ @tagName(t.flags.mouse_event), @tagName(t.flags.mouse_format), @tagName(t.mouse_shape) });
+ for (t.colors.palette.current) |c| try w.print("{x}{x}{x}", .{ c.r, c.g, c.b });
+ try w.writeByte('\n');
+ var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null);
+ while (it.next()) |p| try dumpRow(p.node.page(), p.rowAndCell().row, t.cols, w);
}
/// Its view to row `at` of its history (from the top), or onto its output.
@@ -612,43 +532,26 @@ pub fn viewTo(state: *State, at: ?usize) void {
/// Its view moved by `delta` rows (negative scrolls back).
pub fn scroll(state: *State, delta: i32) void {
state.vt.screens.active.scroll(.{ .delta_row = delta });
- state.changes +%= 1;
}
/// Its view back onto the output.
pub fn follow(state: *State) void {
- const moved = state.vt.screens.active.pages.viewport != .active;
state.vt.screens.active.scroll(.active);
- state.changes +%= @intFromBool(moved);
}
-/// Its grid reflowed to `cols` by `rows`, now or, `later`, when
-/// `resizeDeferred` is called. A failed reflow keeps the grid it had
-/// rather than dropping a scrollback; the next resize retries with the
-/// same numbers.
-pub fn resize(state: *State, cols: u16, rows: u16, later: bool) void {
- if (later) {
- state.resize_to = .{ .cols = cols, .rows = rows };
- return;
- }
+/// Its grid reflowed to `cols` by `rows`. A failed reflow keeps the grid
+/// it had rather than dropping a scrollback; the next resize retries with
+/// the same numbers.
+pub fn resize(state: *State, cols: u16, rows: u16) void {
reflow(state, cols, rows);
}
-/// The size `resize` left for later, made; whether there was one.
-pub fn resizeDeferred(state: *State) bool {
- const size = state.resize_to orelse return false;
- state.resize_to = null;
- reflow(state, size.cols, size.rows);
- return true;
-}
-
/// Its grid reflowed, and its view a row of its history again, as `viewTo`
/// and `grid` have it: ghostty's view follows a cell, which a widening
/// can leave mid-row (a wrapped line's second row joined to its first) and
/// the next narrowing carries a row down, where a view put back at its row
/// (a replica's rollback, core/replica.zig `putViews`) stays.
fn reflow(state: *State, cols: u16, rows: u16) void {
- state.changes +%= 1;
state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {};
inline for (.{ .primary, .alternate }) |which| if (state.vt.screens.get(which)) |screen| {
if (screen.pages.viewport == .pin) screen.pages.viewport_pin.x = 0;
@@ -891,7 +794,6 @@ pub fn revive(state: *State) void {
/// then apply different views.
fn ingest(state: *State, bytes: []const u8) void {
state.stream.nextSlice(bytes);
- state.changes +%= 1;
}
const RowPin = struct { pin: ?*ghostty_vt.Pin = null, offset: i32 = 0 };
@@ -1227,12 +1129,6 @@ pub fn editRows(state: *State, arena: std.mem.Allocator, first: usize, count: us
return shell;
}
-/// A step read its rows (its history too): a replica replaying the step
-/// must hold them (`State.read`).
-pub fn markRead(state: *State) void {
- state.read = true;
-}
-
/// Its rows from the top of its history as text, a prompt's own cells cut
/// off the rows it starts (what typed text stays), joined by '\n' in
/// `text` with `rows` slices into it and one empty row after them, the
@@ -1616,10 +1512,10 @@ test "a view put back at the row it reports is the view it was, through a reflow
// Lines two rows long at 20 columns, one at 40.
for (0..30) |_| ingest(s, "abcdefghijklmnopqrstuvwxyz0123\r\n");
viewTo(s, 1);
- resize(s, 40, 2, false);
+ resize(s, 40, 2);
}
viewTo(put, grid(put).view);
- for ([_]*State{ left, put }) |s| resize(s, 20, 2, false);
+ for ([_]*State{ left, put }) |s| resize(s, 20, 2);
try std.testing.expectEqual(grid(left), grid(put));
}
@@ -1751,8 +1647,8 @@ fn feedReflowing(s: *State, bytes: []const u8, from: usize, to: usize, stops: [2
const rows = s.vt.rows;
var at = from;
while (at < to) {
- if (at == stops[0]) resize(s, cols / 2 + 1, rows + 3, false);
- if (at == stops[1]) resize(s, cols * 2 - 2, rows - 3, false);
+ if (at == stops[0]) resize(s, cols / 2 + 1, rows + 3);
+ if (at == stops[1]) resize(s, cols * 2 - 2, rows - 3);
var end = @min(to, at + 4096);
for (stops) |stop| if (at < stop and stop < end) {
end = stop;