diff options
Diffstat (limited to 'src/echo.zig')
| -rw-r--r-- | src/echo.zig | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/echo.zig b/src/echo.zig index 3d2ee060..6a18f5dd 100644 --- a/src/echo.zig +++ b/src/echo.zig @@ -29,7 +29,7 @@ //! there is ever shown. const std = @import("std"); const pardes = @import("pardes.zig"); -const terminal = pardes.panes.terminal; +const Term = pardes.Term; const Pardes = pardes.Pardes; const Pane = pardes.Pane; const Surface = pardes.Surface; @@ -235,12 +235,11 @@ pub const Echo = struct { /// Whether a key `kind` into terminal `pane` was predicted. fn predict(e: *Echo, pane: *Pane, kind: Kind, seq: u32) bool { const state = pane.terminal orelse return false; - const vt = &state.vt; - if (vt.screens.active_key != .primary or !terminal.atBottom(pane) or !vt.modes.get(.cursor_visible)) return false; + const g = Term.grid(state); + if (Term.modes(state).alternate or g.offset + g.len < g.total or !g.cursor_visible) return false; const top = e.tops[e.pane]; - const screen = vt.screens.active; - const cur: Cursor = e.cursor orelse .{ .x = @intCast(screen.cursor.x), .row = top + screen.cursor.y, .seq = 0 }; - if (cur.row < top or cur.row - top >= vt.rows or e.n == max) return false; + const cur: Cursor = e.cursor orelse .{ .x = g.cursor_x, .row = top + g.cursor_y, .seq = 0 }; + if (cur.row < top or cur.row - top >= g.len or e.n == max) return false; var next: Cursor = .{ .x = cur.x, .row = cur.row, .seq = seq }; var cell: ?Cell = null; switch (kind) { @@ -250,7 +249,7 @@ pub const Echo = struct { // or a suggestion (fish's) being left: the shell redraws // what is right of it. ponytail: mosh's shift of the rest // would cover bash's middle, and mispredict fish's. - if (cur.x + 1 >= vt.cols) return false; + if (cur.x + 1 >= g.cols) return false; const under = e.cpAt(state, cur.x, cur.row); if (!blank(under) and under != cp) return false; cell = .{ .x = cur.x, .row = cur.row, .cp = cp, .was = under, .seq = seq }; @@ -270,7 +269,7 @@ pub const Echo = struct { // Over typed text only: past its end, fish takes the right // arrow for its suggestion (another colour). .right => { - if (cur.x + 1 >= vt.cols or !e.isText(state, cur.x, cur.row)) return false; + if (cur.x + 1 >= g.cols or !e.isText(state, cur.x, cur.row)) return false; next.x += 1; }, } @@ -297,40 +296,40 @@ pub const Echo = struct { } /// What the screen shows at a cell as predicted so far. - fn cpAt(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + fn cpAt(e: *const Echo, state: *Term.State, x: u16, row: u64) u21 { if (e.mine(x, row)) |have| return have.cp; return e.screenCp(state, x, row); } /// What the screen itself shows there; off its active area, a /// codepoint no key types. - fn screenCp(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { - const ci = e.screenCell(state, x, row) orelse return 0x10ffff; - return ci.cell.codepoint(); + fn screenCp(e: *const Echo, state: *Term.State, x: u16, row: u64) u21 { + const c = e.screenCell(state, x, row) orelse return 0x10ffff; + return c.cp; } - fn screenCell(e: *const Echo, state: *terminal.State, x: u16, row: u64) ?terminal.PageCell { + fn screenCell(e: *const Echo, state: *Term.State, x: u16, row: u64) ?Term.EchoCell { const top = e.tops[e.pane]; - if (row < top or row - top >= state.vt.rows) return null; - return state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = @intCast(row - top) } }); + if (row < top or row - top >= Term.grid(state).len) return null; + return Term.echoCell(state, x, @intCast(row - top)); } /// A prompt's input there, or a character predicted. - fn isInput(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + fn isInput(e: *const Echo, state: *Term.State, x: u16, row: u64) bool { if (e.mine(x, row)) |have| return !blank(have.cp); - const ci = e.screenCell(state, x, row) orelse return false; - return ci.cell.semantic_content == .input; + const c = e.screenCell(state, x, row) orelse return false; + return c.input; } /// Typed text there: a character predicted, or a prompt's input in the /// colour of the cell before it. - fn isText(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + fn isText(e: *const Echo, state: *Term.State, x: u16, row: u64) bool { if (e.mine(x, row)) |have| return !blank(have.cp); - const ci = e.screenCell(state, x, row) orelse return false; - if (ci.cell.semantic_content != .input or blank(ci.cell.codepoint())) return false; + const c = e.screenCell(state, x, row) orelse return false; + if (!c.input or blank(c.cp)) return false; if (x == 0) return true; const before = e.screenCell(state, x - 1, row) orelse return false; - return before.cell.style_id == ci.cell.style_id; + return before.style == c.style; } /// The authority confirmed its client's input `seq`. @@ -383,7 +382,7 @@ pub const Echo = struct { const pane = c.panes[e.pane] orelse return e.drop(); if (pane.serial != e.serial) return e.drop(); const state = pane.terminal orelse return e.drop(); - if (state.vt.screens.active_key != .primary) return e.drop(); + if (Term.modes(state).alternate) return e.drop(); const shown = e.visible(c); var i: usize = 0; while (i < e.n) { @@ -399,8 +398,8 @@ pub const Echo = struct { } e.stats.mispredicted += 1; if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { - const cur = state.vt.screens.active.cursor; - std.debug.print("echo: key {d}'s {u} at {d},{d} not shown ({u} there) {d} ms past due; top {d}, the screen's cursor {d},{d}, acked {d}\n", .{ cell.seq, cell.cp, cell.x, cell.row, e.screenCp(state, cell.x, cell.row), (now - cell.due) / ms, e.tops[e.pane], cur.x, cur.y, e.acked }); + const cur = Term.grid(state); + std.debug.print("echo: key {d}'s {u} at {d},{d} not shown ({u} there) {d} ms past due; top {d}, the screen's cursor {d},{d}, acked {d}\n", .{ cell.seq, cell.cp, cell.x, cell.row, e.screenCp(state, cell.x, cell.row), (now - cell.due) / ms, e.tops[e.pane], cur.cursor_x, cur.cursor_y, e.acked }); } if (shown) e.doubt = true; e.trusted = false; @@ -423,8 +422,8 @@ pub const Echo = struct { } const cur = e.cursor orelse return; if (now < cur.due) return; - const screen = state.vt.screens.active; - if (screen.cursor.x == cur.x and e.tops[e.pane] + screen.cursor.y == cur.row) { + const g = Term.grid(state); + if (g.cursor_x == cur.x and e.tops[e.pane] + g.cursor_y == cur.row) { e.cursor = null; e.changed = true; } else if (now >= cur.due + slack) { @@ -448,7 +447,7 @@ pub const Echo = struct { if (id != e.pane or pane.serial != e.serial or p.live != e.view or pane.body.mode != .tty) return; if (!e.visible(p)) return; const state = pane.terminal orelse return; - const sb = state.vt.screens.active.pages.scrollbar(); + const sb = Term.grid(state); const top = e.tops[id]; // Off a prompt, the trust is inferred: underlined, as mosh does. const flag = !atPrompt(pane); @@ -532,7 +531,8 @@ fn blank(cp: u21) bool { /// pardes's marks is trusted only once its echo is confirmed. fn atPrompt(pane: *const Pane) bool { const state = pane.terminal orelse return false; - return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.promptInputReady(pane); + const at = Term.prompt(state); + return !Term.modes(state).alternate and at.phase == .input and at.input_ready; } /// The terminal view `view`'s keys go to, in tty mode: none when they go @@ -642,7 +642,7 @@ const Rig = struct { const s = try c.render(arena.allocator()); const pane = c.panes[g.shell].?; const body = s.region(.body, g.shell).?.rect; - const y = body.y + terminal.gridCursor(pane).y; + const y = body.y + pardes.Term.grid(pane.terminal).cursor_y; const cell = s.at(body.x + x, y); return .{ .cp = cell.text[0], .ul = cell.style.ul != .off, .cursor = if (s.cursor) |cur| if (cur.y == y) cur.x - body.x else null else null }; } @@ -651,6 +651,7 @@ const Rig = struct { const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; test "keys typed at a shell's prompt are drawn at once, and the authority's echo takes their place" { + if (pardes.Term.ops == null) return error.SkipZigTest; // it reads the emulator var g: Rig = undefined; try g.init(); defer g.deinit(); @@ -698,13 +699,14 @@ test "keys typed at a shell's prompt are drawn at once, and the authority's echo } test "a password prompt shows nothing typed, and a full-screen program is not predicted" { + if (pardes.Term.ops == null) return error.SkipZigTest; // it reads the emulator var g: Rig = undefined; try g.init(); defer g.deinit(); try g.prints(prompt ++ "sudo ls\r\n\x1b]133;C;aid=pardes\x07[sudo] password: "); var arena: std.heap.ArenaAllocator = .init(testing.allocator); defer arena.deinit(); - const x = terminal.gridCursor(g.r.core().panes[g.shell].?).x; + const x = pardes.Term.grid(g.r.core().panes[g.shell].?.terminal).cursor_x; const k = try g.key(.{ .cp = 'h', .text = "h" }); // Predicted, and not drawn: nothing has confirmed an echo here. try testing.expectEqual(@as(u32, 1), g.r.echo.stats.predicted); @@ -729,6 +731,7 @@ test "a password prompt shows nothing typed, and a full-screen program is not pr } test "keys still on their way when their predictions go: the next waits for their echo, never predicted where it is about to land" { + if (pardes.Term.ops == null) return error.SkipZigTest; // it reads the emulator var g: Rig = undefined; try g.init(); defer g.deinit(); @@ -766,6 +769,7 @@ test "keys still on their way when their predictions go: the next waits for thei } test "a misprediction takes every prediction back and shows none until an echo is confirmed; after Tab nothing is predicted until its echo" { + if (pardes.Term.ops == null) return error.SkipZigTest; // it reads the emulator var g: Rig = undefined; try g.init(); defer g.deinit(); |
