summaryrefslogtreecommitdiff
path: root/src/echo.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/echo.zig')
-rw-r--r--src/echo.zig67
1 files changed, 64 insertions, 3 deletions
diff --git a/src/echo.zig b/src/echo.zig
index 4560930c..e25953e0 100644
--- a/src/echo.zig
+++ b/src/echo.zig
@@ -115,6 +115,25 @@ pub const Echo = struct {
e.cursor = null;
}
+ /// Every prediction goes, and the keys that made them may still be on
+ /// their way: the keys typed next wait for the newest of them to show,
+ /// as keys typed behind one not predicted do (`deferred`). Predicted
+ /// at once from the screen's cursor, they would land where those keys'
+ /// echo is about to (found at 207 ms, where three keys are in flight:
+ /// one drop, and every key after it misjudged, drawn nowhere).
+ fn lose(e: *Echo) void {
+ // The cursor after the newest key: its `due` is that key's, never
+ // until the authority has it.
+ const newest = e.cursor;
+ e.drop();
+ const cur = newest orelse return;
+ if (e.waiting() or e.time() >= cur.due) return;
+ e.wait = cur.seq;
+ e.wait_key = true;
+ e.nd = 0;
+ e.wait_due = cur.due;
+ }
+
fn waiting(e: *const Echo) bool {
return e.acked < e.wait or e.time() < e.wait_due;
}
@@ -344,7 +363,7 @@ pub const Echo = struct {
if (id == e.pane and top != e.tops[id] and (e.n > 0 or e.cursor != null)) {
// Scrolled: the rows predicted are others now.
e.tops[id] = top;
- return e.drop();
+ return e.lose();
}
e.tops[id] = top;
if (id == e.pane) e.judge(c, 0);
@@ -381,9 +400,13 @@ pub const Echo = struct {
continue;
}
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 });
+ }
if (shown) e.doubt = true;
e.trusted = false;
- return e.drop();
+ return e.lose();
}
// Shown, or a space (mosh: too easy to show by chance to vouch).
// A key before the last one not predicted vouches for nothing
@@ -408,7 +431,7 @@ pub const Echo = struct {
e.changed = true;
} else if (now >= cur.due + slack) {
e.stats.cursor_missed += 1;
- e.drop();
+ e.lose();
}
}
@@ -711,6 +734,44 @@ test "a password prompt shows nothing typed, and a full-screen program is not pr
try testing.expectEqual(before, g.r.echo.stats.predicted);
}
+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 (comptime !terminal.enabled) return error.SkipZigTest;
+ var g: Rig = undefined;
+ try g.init();
+ defer g.deinit();
+ try g.prints(prompt);
+ // Three keys typed a round trip ahead; the authority has the first,
+ // and its shell echoes something else: every prediction goes.
+ const a = try g.key(.{ .cp = 'a', .text = "a" });
+ const b = try g.key(.{ .cp = 'b', .text = "b" });
+ const c = try g.key(.{ .cp = 'c', .text = "c" });
+ try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted);
+ try g.take(a);
+ g.later(grace_ns);
+ try g.prints("A");
+ try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted);
+ try testing.expectEqual(@as(u8, 0), g.r.echo.n);
+ // The next key is not predicted at the screen's cursor, where b's
+ // echo lands: it waits for b and c.
+ const d = try g.key(.{ .cp = 'd', .text = "d" });
+ try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted);
+ try testing.expectEqual(@as(u8, 0), g.r.echo.n);
+ try g.take(b);
+ try g.take(c);
+ g.later(grace_ns);
+ try g.prints("bc");
+ // Their echo shown: d is predicted after it.
+ try testing.expectEqual(@as(u32, 4), g.r.echo.stats.predicted);
+ try testing.expectEqual(@as(u8, 1), g.r.echo.n);
+ try testing.expectEqual(@as(u16, 5), g.r.echo.cells[0].x);
+ try g.take(d);
+ g.later(grace_ns);
+ try g.prints("d");
+ try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted);
+ try testing.expect(!g.r.echo.doubt);
+ try testing.expectEqual(@as(u8, 0), g.r.echo.n);
+}
+
test "a misprediction takes every prediction back and shows none until an echo is confirmed; after Tab nothing is predicted until its echo" {
if (comptime !terminal.enabled) return error.SkipZigTest;
var g: Rig = undefined;