summaryrefslogtreecommitdiff
path: root/core/Messages.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 21:01:37 -0300
committerGabriel Schneider <[email protected]>2026-11-24 21:01:54 -0300
commite91261ff4f4602afec14d2fb958fd3e51a4dc62d (patch)
treed9a50458be65477a0679159373d2e05eb4c1787f /core/Messages.zig
parent1ebc8974489e46aee26409d54d7f1de567dbbb4a (diff)
downloadpardes-e91261ff4f4602afec14d2fb958fd3e51a4dc62d.tar.gz
pardes-e91261ff4f4602afec14d2fb958fd3e51a4dc62d.zip
messages: a background question waits in its pane's tag and never takes the keyboard; SPC e gives it the keyboard, Esc leaves it waiting (pick-ui SPEC §10.2, §10.4, §3.2 asking, walk UI line C item 4): a file that changes on disk under unsaved text is now a question of its pane (one at a time: File.changed), its answers r Reload, k Keep mine and d Diff (Messages.Ask.answers); while the tag is at rest the question is drawn after the path as computed prefix text, 'changed on disk ▸ r Reload k Keep mine d Diff · SPC e to answer' (Messages.askingText; the tag's own words stay after it until the head of ruling R1 takes their place), its mark ? in the box and the notice row's '1 question · SPC e'; the keys stay the body's: View.answering (a pane's serial, the view's own keyboard state, ruling R4) is set by SPC e (item 5) and makes the answer's key answer it in handleKey's first slot, Esc leaving it waiting; the own-command questions (Del's side, a REPL's choice) keep that slot as they were; B2 on an answer's word in its tag answers it (exec.execute: Messages.answerWord); r reads the file again with the consent Get would ask for, k keeps the text and says so, d reads the disk's text (the host's read, taped) and shows where it parts from the buffer as ghost rows under that line, the disk's + lines first (Messages.diskDiff, Said.open: shown whatever the caret), d again hides them; an answer is logged ('... · answered Reload'); Messages.waitingAnswers is what a : lists first (for agent E); 9P /log says 'msg N changed on disk' (monkey9p's checker reads that); tests: Messages.zig 'a file changed on disk under unsaved text asks in its tag and never takes the keyboard: SPC e's keyboard answers, d shows the disk's lines as ghosts, Esc leaves it waiting, B2 on Reload reads it again', ninep/ctl.zig's disk-change test now finds the question; tui: core-test, shell-test, pane-test, behaviour-test pass; process-test: filewatch's golden takes the question (its tag row and its notice row), Restore's root-ctl case failed once under load and passes alone
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Messages.zig')
-rw-r--r--core/Messages.zig192
1 files changed, 192 insertions, 0 deletions
diff --git a/core/Messages.zig b/core/Messages.zig
index 282482ea..e421a32c 100644
--- a/core/Messages.zig
+++ b/core/Messages.zig
@@ -104,8 +104,19 @@ pub const Ask = enum(u8) {
none,
/// A file changed on disk under unsaved text.
disk,
+
+ /// Its answers, as its pane's tag offers them: the key that gives one,
+ /// and its word (an Exec of the word's first word gives it too).
+ pub fn answers(a: Ask) []const Answer {
+ return switch (a) {
+ .none => &.{},
+ .disk => &.{ .{ .key = 'r', .word = "Reload" }, .{ .key = 'k', .word = "Keep mine" }, .{ .key = 'd', .word = "Diff" } },
+ };
+ }
};
+pub const Answer = struct { key: u8, word: []const u8 };
+
/// One live message.
pub const Said = struct {
pub const replica = .{ .len = .{ .{ "places", "places_len" }, .{ "detail", "detail_len" } } };
@@ -125,6 +136,9 @@ pub const Said = struct {
/// The serial of the pane it is about, 0 for none in particular.
pane: u32 = 0,
asks: Ask = .none,
+ /// Its detail shown under its first place's line whatever the caret
+ /// (a question's Diff).
+ open: bool = false,
/// How many times in a row it was said.
n: u16 = 1,
/// When it was last said, on the core's clock (`Pardes.clock.now_ns`).
@@ -472,6 +486,135 @@ fn placeOf(row: []const u8) ?struct { path: []const u8, line: u32, col: u32, tex
return null;
}
+/// The question waiting on `pane`, an index into `live`; null for none.
+pub fn waiting(m: *const Messages, pane: *const Pane) ?usize {
+ for (m.live[0..m.live_len], 0..) |*s, i| if (s.kind == .question and s.pane == pane.serial) return i;
+ return null;
+}
+
+/// The answers of the question waiting on `pane`: what a `:` lists first
+/// (pick-ui SPEC §10.4); none without one.
+pub fn waitingAnswers(m: *const Messages, pane: *const Pane) []const Answer {
+ const i = m.waiting(pane) orelse return &.{};
+ return m.live[i].asks.answers();
+}
+
+/// The waiting question's words as its pane's tag asks them, while the tag
+/// is at rest (SPEC §3.2 asking): what it asks, its answers, and how to
+/// give one. `answering`: SPC e gave it the keyboard.
+pub fn askingText(m: *const Messages, pane: *const Pane, answering: bool, out: []u8) []const u8 {
+ const i = m.waiting(pane) orelse return "";
+ const s = &m.live[i];
+ var w: std.Io.Writer = .fixed(out);
+ w.print("{s} \u{25b8}", .{s.words()}) catch {};
+ for (s.asks.answers()) |a| w.print(" {c} {s} ", .{ a.key, a.word }) catch {};
+ w.writeAll(if (answering) "\u{b7} Esc later" else "\u{b7} SPC e to answer") catch {};
+ return w.buffered();
+}
+
+/// Answer live question `i` with `key`: `r` reads the file again over its
+/// unsaved text, `k` keeps the text and lets the disk's be, `d` shows (or
+/// hides again) the disk's lines where they differ, as ghosts in its
+/// body. False for a key that is none of its answers.
+pub fn answer(p: *Pardes, i: usize, key: u8) bool {
+ const m = &p.messages;
+ const s = &m.live[i];
+ const a = for (s.asks.answers()) |a| {
+ if (a.key == key) break a;
+ } else return false;
+ p.needs_frame = true;
+ const id = p.paneBySerial(s.pane) orelse {
+ m.remove(i);
+ return true;
+ };
+ var said: Buf(256) = .{};
+ if (key == 'd') {
+ if (s.open) {
+ s.open = false;
+ return true;
+ }
+ diskDiff(p, s, id);
+ return true;
+ }
+ logMessage(p, id, .echo, said.print("{s} {s} \u{b7} answered {s}", .{ s.src.slice(), s.words(), a.word }));
+ m.remove(i);
+ if (key == 'r') {
+ const pane = p.panes[id].?;
+ // The answer is the consent Get would ask for.
+ if (pane.file) |f| pane.discard_warned = .{ .revision = f.revision, .by = .get };
+ pardes.exec.runBuiltin(p, .Get, id, "", null);
+ } else _ = say(p, .echo, id, "kept yours; the disk's is not read");
+ return true;
+}
+
+/// An Exec of an answer's word in the tag of a pane a question waits on
+/// (B2 on `Reload`): that answer. False for any other word.
+pub fn answerWord(p: *Pardes, id: usize, word: []const u8) bool {
+ if (id >= MAX_PANES) return false;
+ const pane = p.panes[id] orelse return false;
+ const i = p.messages.waiting(pane) orelse return false;
+ const typed = std.mem.trim(u8, word, " \t\r\n");
+ for (p.messages.live[i].asks.answers()) |a| {
+ const first = a.word[0 .. std.mem.indexOfScalar(u8, a.word, ' ') orelse a.word.len];
+ if (std.mem.eql(u8, typed, first)) return answer(p, i, a.key);
+ }
+ return false;
+}
+
+/// A question's Diff: the disk's text read (the host's read, taped) and
+/// set against the buffer's, the lines they share at either end left out;
+/// where they part, its place, and up to `Said.max_detail` of the lines
+/// there, the disk's `+` and then the buffer's `-`, its detail, shown.
+fn diskDiff(p: *Pardes, s: *Said, id: usize) void {
+ const pane = p.panes[id].?;
+ const f = if (pane.file) |*file| file else return;
+ const disk = @import("fs.zig").read(p, f.path) catch |err| return pardes.Messages.reportError(p, .at(id), "Diff", err);
+ defer p.gpa.free(disk);
+ const mine = pardes.panes.File.fileText(f, p.scratch.allocator()) catch return;
+ var a = std.mem.splitScalar(u8, mine, '\n');
+ var b = std.mem.splitScalar(u8, disk, '\n');
+ var line: u32 = 1;
+ var ours: []const u8 = "";
+ var theirs: []const u8 = "";
+ while (true) : (line += 1) {
+ const x = a.next();
+ const y = b.next();
+ if (x == null and y == null) return;
+ if (x != null and y != null and std.mem.eql(u8, x.?, y.?)) continue;
+ ours = if (x) |l| mine[@intFromPtr(l.ptr) - @intFromPtr(mine.ptr) ..] else "";
+ theirs = if (y) |l| disk[@intFromPtr(l.ptr) - @intFromPtr(disk.ptr) ..] else "";
+ break;
+ }
+ // The lines both end with, left out.
+ var ours_end = ours.len;
+ var theirs_end = theirs.len;
+ while (ours_end > 0 and theirs_end > 0) {
+ const ol = std.mem.lastIndexOfScalar(u8, ours[0 .. ours_end - 1], '\n');
+ const tl = std.mem.lastIndexOfScalar(u8, theirs[0 .. theirs_end - 1], '\n');
+ const o_line = ours[if (ol) |nl| nl + 1 else 0..ours_end];
+ const t_line = theirs[if (tl) |nl| nl + 1 else 0..theirs_end];
+ if (!std.mem.eql(u8, o_line, t_line)) break;
+ ours_end = if (ol) |nl| nl + 1 else 0;
+ theirs_end = if (tl) |nl| nl + 1 else 0;
+ }
+ s.places[0] = .{ .line = line, .col = 1 };
+ s.places[0].path.set(f.path);
+ s.places[0].text.set("the disk's differs here");
+ s.places_len = 1;
+ s.detail_len = 0;
+ // The disk's first: it is what the answer shows.
+ for ([_]struct { []const u8, []const u8 }{ .{ "+", theirs[0..theirs_end] }, .{ "-", ours[0..ours_end] } }) |side| {
+ var rows = std.mem.splitScalar(u8, std.mem.trimEnd(u8, side[1], "\n"), '\n');
+ while (rows.next()) |row| {
+ if (side[1].len == 0 or s.detail_len == Said.max_detail) break;
+ s.detail[s.detail_len] = .{};
+ _ = s.detail[s.detail_len].print("{s}{s}", .{ side[0], row });
+ s.detail_len += 1;
+ }
+ }
+ s.open = true;
+}
+
/// The words the newest live message said of `pane` has, "" for none:
/// what its band shows, and what a write that failed on it answers.
pub fn saidBy(m: *const Messages, pane: *const Pane) []const u8 {
@@ -1093,3 +1236,52 @@ test "a burst of the same message is one message, counted, and moves nothing on
const row = noticeRow(p, 78, &buf);
try std.testing.expect(std.mem.endsWith(u8, row.text[0..row.left], "3 files \u{d7}10"));
}
+
+test "a file changed on disk under unsaved text asks in its tag and never takes the keyboard: SPC e's keyboard answers, d shows the disk's lines as ghosts, Esc leaves it waiting, B2 on Reload reads it again" {
+ const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 });
+ defer p.deinit();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.zig", .data = "one\ntwo\nthree\n" });
+ var dir: [4096]u8 = undefined;
+ const path = try std.fmt.allocPrint(std.testing.allocator, "{s}/c.zig", .{dir[0..try tmp.dir.realPath(std.testing.io, &dir)]});
+ defer std.testing.allocator.free(path);
+ const pane = try p.setTestFile("one\ntwo\nthree\n");
+ const id = p.view().active;
+ p.gpa.free(pane.file.?.path);
+ pane.file.?.path = try p.gpa.dupe(u8, path);
+ const f = &pane.file.?;
+ f.saved_hash = panes.File.fileHash(f);
+ // Unsaved: a line typed at the end.
+ for ([_]u21{ 'G', 'o', 'f', 'o', 'u', 'r', Key.escape }) |cp| p.update(.{ .key = .{ .cp = cp } });
+ try std.testing.expect(f.revision != f.saved_revision);
+ const disk = "zero\none\ntwo\nthree\n";
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.zig", .data = disk });
+ p.update(.{ .file_changed = .{ .pane = .at(id), .bytes = disk } });
+ const i = p.messages.waiting(pane) orelse return error.NotAsked;
+ try std.testing.expectEqual(Kind.question, p.messages.live[i].kind);
+ // In its tag at rest, its answers offered; its mark `?`.
+ const prefix = try pardes.tagline.tagPrefix(p, pane);
+ try std.testing.expect(std.mem.indexOf(u8, prefix, "changed on disk \u{25b8} r Reload k Keep mine d Diff \u{b7} SPC e to answer") != null);
+ try std.testing.expectEqualStrings("Reload", p.messages.waitingAnswers(pane)[0].word);
+ // The keyboard is not its: `r` is the body's (helix's replace).
+ p.update(.{ .key = .{ .cp = 'k' } });
+ try std.testing.expect(p.messages.waiting(pane) != null);
+ // Given the keyboard: `d` shows the disk's lines and keeps asking; Esc
+ // leaves it waiting.
+ p.view().answering = pane.serial;
+ p.update(.{ .key = .{ .cp = 'd' } });
+ const asked = &p.messages.live[p.messages.waiting(pane).?];
+ try std.testing.expect(asked.open);
+ try std.testing.expectEqual(@as(u32, 1), asked.places[0].line);
+ try std.testing.expectEqualStrings("+zero", asked.detail[0].slice());
+ try std.testing.expectEqual(pane.serial, p.view().answering);
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqual(@as(u32, 0), p.view().answering);
+ try std.testing.expect(p.messages.waiting(pane) != null);
+ // B2 on `Reload` in its tag: read again, the question gone.
+ try std.testing.expectEqual(@as(?usize, null), pardes.exec.execute(p, id, "Reload"));
+ while (p.nextEffect()) |_| {}
+ try std.testing.expect(p.messages.waiting(pane) == null);
+ try std.testing.expectEqualStrings(disk, p.panes[id].?.file.?.content);
+}