summaryrefslogtreecommitdiff
path: root/core/term/terminal.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 03:03:55 -0300
committerGabriel Schneider <[email protected]>2026-11-22 10:31:04 -0300
commit9158de5d50831e115e14e2ed502300ee8c1d9930 (patch)
tree2b5e60569307e02d82b19bd419279392a6da257b /core/term/terminal.zig
parent70c9e3fbf9cdc7139b4da0142f3a44741a7e1491 (diff)
downloadpardes-9158de5d50831e115e14e2ed502300ee8c1d9930.tar.gz
pardes-9158de5d50831e115e14e2ed502300ee8c1d9930.zip
term: screen_state.zig deleted, and the terminal module's side of the delta road (the old road's deletion, piece 5 of 8): core/term/screen_state.zig (1,213 lines: the encoder, tracks, peers' flights, a replica's apply and holes, its own soak and four tests) goes whole; core/term/terminal.zig loses `encode`/`encodeRows`/`encoder`/`encoderFree`/`track`/`untrack`/`replica`/`replicaFree`/`replicaTop`/`missing`/`apply`/`applyRows`/`shown`/`blank`, `takeMarks` (`marksOf` stays, private: the tests compare two terminals by it), the State's `changes` (a version's counter for the tracks), `read` with `markRead`/`wasRead`/`clearRead` (the rows a step read, for the fills), and `resize_to` with `resizeDeferred` and `resize`'s `later` (screen state's authority sized its terminals after a step's unit); abi/term.zig loses the `Encoder`/`Track`/`Replica` handles, `flight_bound`, `Peer`, `Applied` and those entries of `Ops` (and `dumpScreen`, which nothing in the core called); core/Term.zig its wrappers; core/term/term.zig its ops, its handle map and its `screen_state` export; core/panes.zig its `resizeDeferred` and resizes now (its later screen-state lines go in piece 6); core/edit.zig its `markRead`; the cell-for-cell dump the soak compares a client's terminal with its session's by moves to terminal.zig (`dumpScreen`, over the emulator), the soak's three streams (a shell's, a full-screen program's, `yes`) into test/replicas.zig, their one user; test/terminal.zig loses `a replica's terminal pane draws as its authority's does` (a delta's) and `a terminal made fresh has no size waiting` (`resize_to`'s); core-test 911/1068 (157 skipped), shell-test 1110/1113, term-test 15/15, pane-test 181/182 (again after the machines reboot, on a wiped cache), the soaks small run and `--echo` rc 0
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/term/terminal.zig')
-rw-r--r--core/term/terminal.zig216
1 files changed, 50 insertions, 166 deletions
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index 187ed144..1c54f2ac 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
@@ -338,11 +325,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 +456,13 @@ 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 two terminals' compare by
+/// (`expectSame`): 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 +473,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 +520,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 +782,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 +1117,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 +1500,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 +1635,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;