From f26cf0be705280ff28ca69aaef6cc64e1466ed4e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 18:52:44 -0300 Subject: messages: one session-wide list of live messages replaces each pane's stack of chips (pick-ui SPEC §10.1, walk UI line C item 1): Messages.Said is a kind (echo, notice, warn, error, question, progress), its cause first as a word a Look understands (a file's path, @pN and its command), its text, the places it names and the lines that explain it, the pane it is about, the question it asks, ×n and the core's clock, in a fixed list of 16 (Messages.live); a new message retires every echo and notice, a new error the error before it, the same message right after itself is counted, progress from the same cause runs in place and is never the notice row's; an echo or a notice dims after MessageLinger (now 4000 ms, a warning twice as long: test configs' MessageLinger 0 keep their meaning, nothing dims and nothing wakes), and the core sleeps to that dim (Messages.dimDue in nextWake) instead of a frame-counted linger; no key or click dismisses a message any more: Esc in normal mode takes the warnings and errors first, then goes back as before; every call site says what it said, mapped by its entry: setMessage a notice, setStatus progress, reportError and reportFailure an error, a builtin's announcement an echo (exec.zig), the sent-to-a-REPL arrow an echo; the log ring stays the history and 9P /log (each line now with its kind); gone: Pane.msg, msg_older and their lives, slots, announcement, status and sent flags, dismissMessage, advanceMessages, holdMessages, messageWake, expireUnattended and the detached shells' 100 ms message waits; until the notice row (item 2) a pane's one message chip shows the newest message said of it (Messages.saidBy); an Upgrade's outcome is said once and is every view's; tests: Messages.zig 'an echo or a notice retires those before it, an error the error before it, a repeat counts and progress runs in place', 'Msg says a notice by hand ... and the next message retires it', 'an idle core wants no wake, and a message wakes it once, when it dims', runtime/upgrade.zig 'an upgrade's outcome stays on every view's notice row until something else is said'; core-test 862 pass 162 skip, pane-test and shell-test pass; behaviour-test: the pdf addon's script golden and one PDF Restore test move with the chip over a PDF's body, which item 2 takes away MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/Messages.zig | 695 ++++++++++++++++++++++++++---------------------------- 1 file changed, 335 insertions(+), 360 deletions(-) (limited to 'core/Messages.zig') diff --git a/core/Messages.zig b/core/Messages.zig index 040b8d2c..747e5e2f 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -1,6 +1,9 @@ -//! The message row: messages and statuses a pane shows between its body and -//! its tagline, how each line lingers once dismissed, the notices -//! built from them every frame, and the log of everything said. +//! The session's messages (pick-ui SPEC §10): each a kind, its cause as a +//! word a Look understands, its text, the places it names and the lines +//! that explain it, kept in a fixed list while it lives; the log of +//! everything said behind them (the history, `SPC h m`, and 9P `/log`); +//! and the transient bands a pane shows over its body's top rows (the +//! pending leader chord, a prompt waiting for input). const pardes = @import("root.zig"); const memory = @import("memory.zig"); const std = @import("std"); @@ -16,12 +19,14 @@ const BOX_H = pardes.BOX_H; const Key = pardes.Key; const Rect = layout.Rect; const Pardes = pardes.Pardes; +const Buf = pardes.Kind.Buf; const Messages = @This(); /// Where its ring's next line goes and how many it holds, held to the ring -/// where a dump is read back (walk.zig `.place`). -pub const replica = .{ .place = .{ .{ "head", "lastSlot" }, .{ "len", "size" } } }; +/// where a dump is read back (walk.zig `.place`); the live messages as far +/// as they go. +pub const replica = .{ .place = .{ .{ "head", "lastSlot" }, .{ "len", "size" } }, .len = .{.{ "live", "live_len" }} }; /// The message-row log: a fixed ring, never allocated, never grown. See /// `logMessage` and the `Messages` builtin. @@ -29,6 +34,14 @@ log: [limits.message_log]LoggedMessage = @splat(.{}), /// Next slot to write. `len` saturates at the ring's size. head: usize = 0, len: usize = 0, +/// The messages that live, oldest first: what the notice row, the marks +/// and the ghosts at a cause's line are drawn from each frame. +live: [max_live]Said = undefined, +live_len: u8 = 0, + +/// How many messages live at once: past it the oldest that is no +/// question goes (a burst of distinct ones; a repeat only counts). +pub const max_live = 16; pub fn lastSlot(m: *const Messages) usize { return m.log.len - 1; @@ -38,6 +51,111 @@ pub fn size(m: *const Messages) usize { return m.log.len; } +/// What a message is, which says how long it lives (SPEC §10.1): an echo +/// and a notice until the next message, bright for `MessageLinger`; a +/// warning bright twice that, until Esc; an error until Esc or the next +/// error; a question until it is answered; progress while it runs, at the +/// right end of its tag rather than on the notice row. +pub const Kind = enum(u8) { + echo, + notice, + warn, + @"error", + question, + progress, + + pub fn glyph(k: Kind) []const u8 { + return switch (k) { + .echo => "\u{2713}", + .notice => "\u{b7}", + .warn => "!", + .@"error" => "\u{2717}", + .question => "?", + .progress => "\u{22ef}", + }; + } + + /// Which wins the notice row and a mark: error, question, warning, + /// notice, echo; progress never. + pub fn rank(k: Kind) u8 { + return switch (k) { + .progress => 0, + .echo => 1, + .notice => 2, + .warn => 3, + .question => 4, + .@"error" => 5, + }; + } +}; + +/// A place a message names: a file's line and column, and what it says +/// there (a compiler's `error: ...`), which the body shows at that line. +pub const Place = struct { + path: Buf(256) = .{}, + line: u32 = 1, + col: u32 = 1, + text: Buf(128) = .{}, +}; + +/// The questions the session asks in the background, and their answers. +pub const Ask = enum(u8) { + none, + /// A file changed on disk under unsaved text. + disk, +}; + +/// One live message. +pub const Said = struct { + pub const replica = .{ .len = .{ .{ "places", "places_len" }, .{ "detail", "detail_len" } } }; + pub const max_places = 4; + pub const max_detail = 4; + kind: Kind = .notice, + /// Its cause, first, as a word a Look understands: a file's path, a + /// pane's `@pN` (and its command), a server's name. + src: Buf(96) = .{}, + text: Buf(256) = .{}, + places: [max_places]Place = undefined, + places_len: u8 = 0, + /// The lines that explain it, shown under its first place's line while + /// the caret is on it. + detail: [max_detail]Buf(128) = undefined, + detail_len: u8 = 0, + /// The serial of the pane it is about, 0 for none in particular. + pane: u32 = 0, + asks: Ask = .none, + /// 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`). + t: u64 = 0, + + /// Its words without the clock a host stamped on them (Message.stamp). + pub fn words(s: *const Said) []const u8 { + return Message.body(s.text.slice()); + } + + /// Whether it has dimmed: an echo or a notice past `linger_ms`, a + /// warning past twice that. + pub fn faded(s: *const Said, now_ns: u64, linger_ms: u16) bool { + const bright: u64 = switch (s.kind) { + .echo, .notice => linger_ms, + .warn => 2 * @as(u64, linger_ms), + else => return false, + }; + return now_ns -| s.t >= bright * std.time.ns_per_ms; + } + + /// Whether it is about `pane`: said of it, or naming its file (as its + /// cause or among its places). + pub fn about(s: *const Said, pane: *const Pane) bool { + if (s.pane != 0 and s.pane == pane.serial) return true; + const f = pane.file orelse return false; + if (std.mem.eql(u8, s.src.slice(), f.path)) return true; + for (s.places[0..s.places_len]) |*place| if (std.mem.eql(u8, place.path.slice(), f.path)) return true; + return false; + } +}; + pub const LoggedMessage = struct { pub const cap = 256; pub const replica = .{ .len = .{.{ "text", "len" }} }; @@ -50,6 +168,7 @@ pub const LoggedMessage = struct { /// How many times in a row. Saturating: a key held down against the same /// refusal is one event, not four hundred. repeats: u16 = 0, + kind: Kind = .notice, pub fn slice(m: *const LoggedMessage) []const u8 { return m.text[0..m.len]; @@ -137,195 +256,168 @@ pub const Message = struct { } }; -/// A status updates the message line in place (a progress count does not -/// pile up); a message is an event, and stacks. + +/// A status updates its line in place (a progress count does not pile up), +/// and is not logged; a message is an event. pub const setStatus = pardes.tape.taped(setStatusInner); fn setStatusInner(p: *Pardes, id: Pane.Id, text: []const u8) void { - showMessage(p, id.index(), text, false); + say(p, .progress, id.index(), text); } pub const setMessage = pardes.tape.taped(setMessageInner); fn setMessageInner(p: *Pardes, id: Pane.Id, text: []const u8) void { - logMessage(p, id.index(), text); - showMessage(p, id.index(), text, true); + say(p, .notice, id.index(), text); +} + +/// Say `text`, a message of `kind` about pane `id` (MAX_PANES and up: +/// none in particular), its cause first: logged (progress is not), and +/// live (`keep`). +pub fn say(p: *Pardes, kind: Kind, id: usize, text: []const u8) void { + if (kind != .progress) logMessage(p, id, kind, text); + const pane: ?*Pane = if (id < MAX_PANES and !p.fs.session_write) p.panes[id] else null; + var src: Buf(96) = .{}; + if (pane) |cause| { + if (cause.file) |f| src.set(f.path) else if (cause.command) |line| { + const first = line[0 .. std.mem.indexOfAny(u8, line, "\r\n") orelse line.len]; + var cut = @min(first.len, 64); + while (cut < first.len and cut > 0 and first[cut] & 0xC0 == 0x80) cut -= 1; + _ = src.print(config.pane_addr ++ "{d} {s}", .{ cause.serial, first[0..cut] }); + } else _ = src.print(config.pane_addr ++ "{d}", .{cause.serial}); + } + _ = keep(p, kind, if (pane) |cause| cause.serial else 0, src.slice(), text); } -fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { - if (id >= MAX_PANES) return; - const pane = p.panes[id] orelse return; +/// Make a message live, and return it (for its places and its detail): +/// a new message retires every echo and notice, a new error the error +/// before it; the same message again right after itself is counted. The +/// list is in the order things were said. +pub fn keep(p: *Pardes, kind: Kind, serial: u32, src: []const u8, text: []const u8) *Said { + const m = &p.messages; + p.needs_frame = true; // One row of printable text: a language server's multi-line report // reads as one line, and the cut never leaves half a glyph behind. - var n: usize = @min(text.len, pane.msg.len); + var row: [256]u8 = undefined; + var n: usize = @min(text.len, row.len); if (n < text.len) { while (n > 0 and text[n] & 0xC0 == 0x80) n -= 1; } - const same = pane.msg_len == n and for (text[0..n], pane.msg[0..n]) |c, have| { - if ((if (c < 0x20 or c == 0x7f) ' ' else c) != have) break false; - } else true; - // A new event moves the line on screen up the stack, where it keeps - // its own timing, and falls in below it. - // Only an event stacks on an event: an announcement or a status - // stands in for whatever comes next. - if (stack and pane.msg_len > 0 and !same and !pane.msg_announcement and !pane.msg_status) - pane.pushOlderMessage(); - if (pane.msg_len == 0) pane.msg_slot = pane.freeMessageSlot(); - pane.msg_announcement = false; - pane.msg_sent = false; - pane.msg_status = !stack; - for (text[0..n], pane.msg[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c; - pane.msg_len = @intCast(n); - // The clock when it was said (the session clock is the last pump's, - // and a 9P write after an idle hour would say it an hour ago). - pane.msg_said_ms = p.tape.value(.clock, nowMs, .{}); - p.needs_frame = true; - // New, the same line again or a status update: fresh, so it stays. - pane.msg_life = .{}; -} - -/// With no frontend attached no input dismisses a message, and a detached -/// session's /screen and messages kept what was said an hour ago: each -/// pane's lines go once its newest has been up `message_linger_ms`, on the -/// clock, checked as the core is stepped (each event, and each pump after -/// its wait, which a message wakes) -- not as a 9P request is served, since -/// a read is answered while a step is out and must change nothing, nor by -/// the host, whose call is no input a replay makes. -/// ponytail: a pane's older lines go with its newest, not each on its own -/// clock; the render pipeline's clock will give every line its own. -fn nowMs() i64 { - var ts: std.c.timespec = undefined; - if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); -} - -pub fn expireUnattended(p: *Pardes) void { - if (!p.unattended) return; - // The clock as the session that recorded read it (core/tape.zig). - const now = p.tape.value(.clock, nowMs, .{}); - for (p.panes) |slot| { - const pane = slot orelse continue; - if (pane.msg_len == 0 and pane.msg_older_len == 0) continue; - if (awaited(p, pane)) continue; - if (now - pane.msg_said_ms < p.settings.message_linger_ms) continue; - pane.msg_len = 0; - pane.msg_life = .{}; - pane.msg_older_len = 0; - p.needs_frame = true; - } -} - -/// A message waits for someone to see it, and a view kept for its client's -/// return (views.away: an Upgrade's clients, a divergence's) is someone -/// on the way: what its keyboard's pane says (an Upgrade's outcome, -/// upgrade.zig `tellEveryView`) is that client's until it is back for its -/// view (Pardes.rejoinView: its join's dump has it) or the view's kept -/// time is up; no other view's input and no unattended clock take it. A -/// pair's one client is such a client; the session's own frontend is -/// none (its view is never away). A session no build could load starts -/// afresh with no view kept: a pair's is held attended for its client -/// instead (tui.zig `Own.start`). -fn awaited(p: *const Pardes, pane: *const Pane) bool { - if (p.views_away == 0) return false; - for (p.views, 0..) |slot, k| if (slot) |v| { - if (pardes.views.isAway(p, @intCast(k)) and p.panes[v.active] == pane and - p.clock.now_ns -| v.away_ns < pardes.views.away_kept_ns) return true; + for (text[0..n], row[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c; + const line = row[0..n]; + const now = p.clock.now_ns; + // Progress from the same cause is updated, and is the newest said. + if (kind == .progress) for (m.live[0..m.live_len], 0..) |*s, i| { + if (s.kind != .progress or s.pane != serial or !std.mem.eql(u8, s.src.slice(), src)) continue; + const kept = s.*; + m.remove(i); + const last = &m.live[m.live_len]; + m.live_len += 1; + last.* = kept; + last.text.set(line); + last.t = now; + return last; }; - return false; -} - -/// The key or click that used to clear the message row now starts each -/// line's exit: it stays `message_linger_ms`, then goes. A line already -/// on its way out keeps its own timing. -pub fn dismissMessage(p: *Pardes, pane: *Pane) void { - if (awaited(p, pane)) return; - if (pane.msg_len > 0 and dismissLine(p, &pane.msg_life)) { - pane.msg_len = 0; - pane.msg_life = .{}; + if (m.live_len > 0) { + // The NEWEST wording wins, so it carries the latest clock. + const last = &m.live[m.live_len - 1]; + if (last.kind == kind and last.pane == serial and std.mem.eql(u8, last.src.slice(), src) and + std.mem.eql(u8, Message.body(last.text.slice()), Message.body(line))) + { + last.n +|= 1; + last.t = now; + last.text.set(line); + return last; + } } - var i: usize = 0; - while (i < pane.msg_older_len) { - if (dismissLine(p, &pane.msg_older[i].life)) pane.removeOlderMessage(i) else i += 1; + if (kind != .progress) { + var i: usize = 0; + while (i < m.live_len) { + const old = m.live[i].kind; + if (old == .echo or old == .notice or (kind == .@"error" and old == .@"error")) m.remove(i) else i += 1; + } + } + if (m.live_len == max_live) { + var oldest: usize = 0; + while (oldest + 1 < m.live_len and m.live[oldest].kind == .question) oldest += 1; + m.remove(oldest); } + const s = &m.live[m.live_len]; + m.live_len += 1; + s.* = .{ .kind = kind, .pane = serial, .t = now }; + s.src.set(src); + s.text.set(line); + return s; } -/// Start one line's exit; true when it is gone at once (no linger). -fn dismissLine(p: *const Pardes, life: *Pane.MessageLife) bool { - if (life.phase == .lingering) return false; - if (p.settings.message_linger_ms == 0) return true; - life.* = .{ .phase = .lingering }; - return false; +fn remove(m: *Messages, i: usize) void { + std.mem.copyForwards(Said, m.live[i .. m.live_len - 1], m.live[i + 1 .. m.live_len]); + m.live_len -= 1; } -/// One frame of every lingering message line. The one that goes owes a -/// frame: a tick asks for none itself. -pub fn advanceMessages(p: *Pardes) void { - for (p.panes) |slot| { - const pane = slot orelse continue; - if (pane.msg_len > 0 and pane.msg_life.phase != .shown) { - p.needs_frame = true; - if (advanceLine(p, &pane.msg_life)) { - pane.msg_len = 0; - pane.msg_life = .{}; - } - } - var i: usize = 0; - while (i < pane.msg_older_len) { - if (pane.msg_older[i].life.phase != .shown) p.needs_frame = true; - if (advanceLine(p, &pane.msg_older[i].life)) pane.removeOlderMessage(i) else i += 1; - } +/// Retire every live message of `kinds` (Esc: the warnings and errors); +/// how many went. +pub fn retire(m: *Messages, kinds: []const Kind) usize { + var gone: usize = 0; + var i: usize = 0; + while (i < m.live_len) { + if (std.mem.indexOfScalar(Kind, kinds, m.live[i].kind) != null) { + m.remove(i); + gone += 1; + } else i += 1; } + return gone; } -/// One frame of one line's life; true once it has gone. -fn advanceLine(p: *const Pardes, life: *Pane.MessageLife) bool { - if (life.phase == .shown) return false; - life.frame +|= 1; - return life.frame >= messageFrames(p.settings.message_linger_ms); +/// The message the notice row leads with: the highest-ranked, the newest +/// within a rank; progress never. +pub fn headline(m: *const Messages) ?*const Said { + var best: ?*const Said = null; + for (m.live[0..m.live_len]) |*s| { + if (s.kind == .progress) continue; + if (best == null or s.kind.rank() >= best.?.kind.rank()) best = s; + } + return best; } -/// Move every lingering message line `frames` along its linger at once: -/// how `advance` jumps a wait instead of stepping it frame by frame. -pub fn holdMessages(p: *Pardes, frames: u64) void { - const n: u16 = @intCast(@min(frames, std.math.maxInt(u16))); - for (p.panes) |slot| { - const pane = slot orelse continue; - if (pane.msg_len > 0 and pane.msg_life.phase == .lingering) pane.msg_life.frame +|= n; - for (pane.msg_older[0..pane.msg_older_len]) |*line| { - if (line.life.phase == .lingering) line.life.frame +|= n; - } +/// 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 { + var i = m.live_len; + while (i > 0) { + i -= 1; + if (m.live[i].pane == pane.serial) return m.live[i].text.slice(); } + return ""; } -/// Frames until this pane's message lines next change: the rest of a -/// linger, null when every line just shows. -pub fn messageWake(p: *const Pardes, pane: *const Pane) ?u64 { +/// When the next live message dims, on the core's clock; null for none. +pub fn dimDue(p: *const Pardes) ?u64 { + const m = &p.messages; + if (p.settings.message_linger_ms == 0) return null; var soonest: ?u64 = null; - const linger = messageFrames(p.settings.message_linger_ms); - for (0..@as(usize, pane.msg_older_len) + 1) |i| { - const life = if (i < pane.msg_older_len) pane.msg_older[i].life else if (pane.msg_len > 0) pane.msg_life else continue; - if (life.phase == .shown) continue; - const frames: u64 = @max(1, linger -| life.frame); - soonest = if (soonest) |n| @min(n, frames) else frames; + for (m.live[0..m.live_len]) |*s| { + const bright: u64 = switch (s.kind) { + .echo, .notice => p.settings.message_linger_ms, + .warn => 2 * @as(u64, p.settings.message_linger_ms), + else => continue, + }; + const due = s.t + bright * std.time.ns_per_ms; + if (due <= p.clock.stepped_ns) continue; + soonest = if (soonest) |first| @min(first, due) else due; } return soonest; } -/// Frames a message timing lasts; at least one. -pub fn messageFrames(ms: u16) u16 { - return @intCast(@max(1, (@as(u32, ms) + pardes.frame_ms - 1) / pardes.frame_ms)); -} - /// What notice band `i` says. The grid pass and the band layers both /// read it here, so a terminal and a pixel shell show the same line. pub fn noticeText(p: *const Pardes, pane: *const Pane, i: usize, leader_buf: *[16]u8) []const u8 { return switch (pane.notices.kinds[i]) { - .message => pane.msg[0..pane.msg_len], - .older => pane.msg_older[pane.notices.index[i]].slice(), + .message => p.messages.saidBy(pane), .leader => leaderText(p, leader_buf), .prompt => pane.promptText() orelse "", }; } -fn logMessage(p: *Pardes, id: usize, text: []const u8) void { +fn logMessage(p: *Pardes, id: usize, kind: Kind, text: []const u8) void { if (text.len == 0) return; const serial: u32 = if (p.fs.session_write) 0 else if (id < MAX_PANES) if (p.panes[id]) |pane| pane.serial else 0 else 0; const kept = text[0..@min(text.len, LoggedMessage.cap)]; @@ -349,6 +441,7 @@ fn logMessage(p: *Pardes, id: usize, text: []const u8) void { @memcpy(slot.text[0..slot.len], kept); slot.serial = serial; slot.repeats = 1; + slot.kind = kind; p.messages.head = (p.messages.head + 1) % limits.message_log; p.messages.len = @min(p.messages.len + 1, limits.message_log); } @@ -478,9 +571,9 @@ pub fn reportFailure(p: *Pardes, id: usize, text: []const u8) void { if (failing_write) { p.fs.unlogged = true; defer p.fs.unlogged = false; - return setMessage(p, .at(id), text); + return say(p, .@"error", id, text); } - setMessage(p, .at(id), text); + say(p, .@"error", id, text); } /// The one place that decides which transient lines a pane is showing and @@ -510,10 +603,8 @@ pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { pub fn bands(p: *const Pardes, v: *const Pardes.View, pane: *const Pane, tag_rows: u16, r: Rect, id: usize) Pane.Notices { var n: Pane.Notices = .{}; if (r.w <= config.GUTTER or r.h <= tag_rows) return n; - // Oldest message line on top, so lines already up do not move when a - // new one lands under them; when rows run out the oldest go first. - for (pane.msg_older[0..pane.msg_older_len], 0..) |line, i| n.pushAt(.older, @intCast(i), line.slot); - if (pane.msg_len > 0) n.pushAt(.message, 0, pane.msg_slot); + // The newest message said of it on top. + if (p.messages.saidBy(pane).len > 0) n.push(.message); if (id == v.active and v.prefix == .leader) n.push(.leader); // Del's question shows once its delay is up: answered before that, it // was never there. @@ -525,28 +616,18 @@ pub fn bands(p: *const Pardes, v: *const Pardes.View, pane: *const Pane, tag_row // into blind. A two-row pane spends its only body row on one notice, // which is what the single shared row used to do anyway. const room: u8 = @intCast(@min(@as(u16, Pane.Notices.max), r.h - tag_rows)); - if (n.span() <= room) return n; - // Too short for the gaps: close them up in screen order, and if even - // that does not fit, the top bands go. - var i: usize = 1; - while (i < n.len) : (i += 1) { - var j = i; - while (j > 0 and n.row[j - 1] > n.row[j]) : (j -= 1) { - std.mem.swap(Pane.Notices.Kind, &n.kinds[j - 1], &n.kinds[j]); - std.mem.swap(u8, &n.index[j - 1], &n.index[j]); - std.mem.swap(u8, &n.row[j - 1], &n.row[j]); - } - } - const drop = n.len -| room; - for (0..n.len - drop) |k| { + if (n.len <= room) return n; + // Too short for them all: the top bands go. + const drop = n.len - room; + for (0..room) |k| { n.kinds[k] = n.kinds[k + drop]; - n.index[k] = n.index[k + drop]; n.row[k] = @intCast(k); } - n.len -= @intCast(drop); + n.len = room; return n; } + test "errors are said in words, and a ZON failure names its line" { var buf: [128]u8 = undefined; try std.testing.expectEqualStrings("file not found", errorWords(error.FileNotFound, &buf)); @@ -608,177 +689,84 @@ test "the message log keeps what the row forgets, and collapses repeats" { try std.testing.expectEqualStrings(want_last, p.messages.at(limits.message_log - 1).?.slice()); } -test "Msg writes the transient row by hand, bare or with text, and input ends it" { +test "Msg says a notice by hand, bare or with text, and the next message retires it" { if (pardes.Term.ops == null) return error.SkipZigTest; // the classic boot's three shells const p = try Pardes.init(std.testing.allocator, .{ .shells = 3, .cols = 100, .rows = 30 }); defer p.deinit(); while (p.nextEffect()) |_| {} - const row = struct { - fn of(pp: *Pardes, i: usize) []const u8 { - const pane = pp.panes[i].?; - return pane.msg[0..pane.msg_len]; - } - }.of; - // the whole tail, spaces and all: the row is a sentence, not a word try std.testing.expect(p.executeBuiltinLine(2, "Msg saved /etc/hosts")); - try std.testing.expectEqualStrings("saved /etc/hosts", row(p, 2)); - // ...on the pane that ran it, and nowhere else - try std.testing.expectEqualStrings("", row(p, 0)); - + try std.testing.expectEqualStrings("saved /etc/hosts", p.messages.saidBy(p.panes[2].?)); + // ...of the pane that ran it, its cause that pane's address + try std.testing.expectEqualStrings("", p.messages.saidBy(p.panes[0].?)); + var want: [16]u8 = undefined; + try std.testing.expectEqualStrings(try std.fmt.bufPrint(&want, "@p{d}", .{p.panes[2].?.serial}), p.messages.headline().?.src.slice()); // Bare, it reports ITSELF through reportError — the `: ` - // every failed save and refused Look arrives in. + // every failed save and refused Look arrives in: an error. try std.testing.expect(p.executeBuiltinLine(2, "Msg")); - try std.testing.expectEqualStrings("Msg: no message", row(p, 2)); - - // Input starts its exit: it lingers, and is gone. - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expectEqualStrings("Msg: no message", row(p, 2)); - for (0..200) |_| p.update(.tick); - try std.testing.expectEqualStrings("", row(p, 2)); - // ...and with no linger it is as old as your last input. - try std.testing.expect(p.executeBuiltinLine(2, "MessageLinger 0")); - try std.testing.expect(p.executeBuiltinLine(2, "Msg gone")); + try std.testing.expectEqualStrings("Msg: no message", p.messages.saidBy(p.panes[2].?)); + try std.testing.expectEqual(Kind.@"error", p.messages.headline().?.kind); + // Input takes nothing away; the next message does what the notice + // was, and the error stays until Esc. p.update(.{ .key = .{ .cp = 'h' } }); - try std.testing.expectEqualStrings("", row(p, 2)); + for (0..200) |_| p.update(.tick); + try std.testing.expectEqualStrings("Msg: no message", p.messages.saidBy(p.panes[2].?)); + try std.testing.expect(p.executeBuiltinLine(2, "Msg next")); + try std.testing.expectEqual(@as(u8, 2), p.messages.live_len); + try std.testing.expectEqual(Kind.@"error", p.messages.headline().?.kind); - // Longer than the row's buffer is CUT, never refused and never overrun: + // Longer than a message's room is CUT, never refused and never overrun: // this word is the only way to reach that boundary without a 300-byte path. var long: [600]u8 = @splat('x'); var line: [608]u8 = undefined; const cmd = try std.fmt.bufPrint(&line, "Msg {s}", .{&long}); try std.testing.expect(p.executeBuiltinLine(2, cmd)); - try std.testing.expectEqual(p.panes[2].?.msg.len, row(p, 2).len); -} - -test "with no frontend attached a message goes once it has lingered, on the clock" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = p.panes[p.view().active].?; - p.setMessage(.at(p.view().active), "said"); - p.setMessage(.at(p.view().active), "said again"); - // Attached: it waits for input, however long. - pane.msg_said_ms -= 60_000; - p.update(.tick); - try std.testing.expect(pane.msg_len > 0); - // Detached with nobody attached: past its linger it is gone, and the - // line above it with it. - p.unattended = true; - // A read served meanwhile changes nothing; the session's step after its - // wait expires, not the host (whose call no replay makes). - _ = p.serveFs(.{ .tag = 1, .op = .read, .node = @backingInt(ctlfs.TopFile.index), .size = 64 }); - try std.testing.expect(pane.msg_len > 0); - try p.pump(.{}); - try std.testing.expectEqual(@as(u16, 0), pane.msg_len); - try std.testing.expectEqual(@as(u8, 0), pane.msg_older_len); - // A fresh one stays until its linger is up. - p.setMessage(.at(p.view().active), "fresh"); - p.update(.tick); - try std.testing.expect(pane.msg_len > 0); + try std.testing.expectEqual(@as(usize, 256), p.messages.saidBy(p.panes[2].?).len); } -test "a message lingers past the input that dismisses it, and goes" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); +test "an echo or a notice retires those before it, an error the error before it, a repeat counts and progress runs in place" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = p.panes[p.view().active].?; - try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 160")); - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg hello")); - // Shown at once, until input; the same line again stays shown. - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - try std.testing.expect(!p.animationActive()); - for (0..100) |_| p.update(.tick); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg hello")); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - // Input (a key that announces nothing): 160 ms of lingering (ten frames), unaffected by more input... - p.update(.{ .key = .{ .cp = 'h' } }); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - for (0..9) |_| { - p.update(.tick); - p.update(.{ .key = .{ .cp = 'h' } }); + _ = try p.setTestFile("text\n"); + const id = p.view().active; + const m = &p.messages; + m.live_len = 0; + say(p, .notice, id, "first"); + say(p, .echo, id, "second"); + try std.testing.expectEqual(@as(u8, 1), m.live_len); + try std.testing.expectEqualStrings("second", m.headline().?.text.slice()); + say(p, .@"error", id, "broke"); + say(p, .warn, id, "careful"); + // The echo went with the error; the warning stands beside it. + try std.testing.expectEqual(@as(u8, 2), m.live_len); + try std.testing.expectEqual(Kind.@"error", m.headline().?.kind); + say(p, .@"error", id, "broke again"); + try std.testing.expectEqual(@as(u8, 2), m.live_len); + try std.testing.expectEqualStrings("broke again", m.headline().?.text.slice()); + // The same again right after itself: one message, counted, its newest + // wording (and clock) kept. + for (0..9) |_| say(p, .@"error", id, "broke again"); + try std.testing.expectEqual(@as(u8, 2), m.live_len); + try std.testing.expectEqual(@as(u16, 10), m.headline().?.n); + // Progress updates its line in place and retires nothing. + say(p, .progress, id, "Changes: jj 1/4"); + say(p, .progress, id, "Changes: jj 3/4"); + try std.testing.expectEqual(@as(u8, 3), m.live_len); + try std.testing.expectEqualStrings("Changes: jj 3/4", m.saidBy(p.panes[id].?)); + try std.testing.expectEqual(Kind.@"error", m.headline().?.kind); + // Esc in normal mode takes the warnings and errors. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(u8, 1), m.live_len); + try std.testing.expectEqual(Kind.progress, m.live[0].kind); + try std.testing.expect(m.headline() == null); + // Past the room, the oldest that is no question goes. + m.live_len = 0; + for (0..max_live + 3) |i| { + var buf: [16]u8 = undefined; + say(p, .warn, id, std.fmt.bufPrint(&buf, "w{d}", .{i}) catch unreachable); } - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - // ...then it is gone, and the tick that clears it owes the frame that - // shows it gone. - p.needs_frame = false; - p.update(.tick); - try std.testing.expectEqual(@as(u16, 0), pane.msg_len); - try std.testing.expect(p.needs_frame); - try std.testing.expect(!p.animationActive()); - // Shown again while lingering: fresh, so it stays; a status update to a - // line on screen swaps its text in place. - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg again")); - p.update(.{ .key = .{ .cp = 'h' } }); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg again")); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - setStatus(p, .at(p.view().active), "progress 50%"); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - try std.testing.expectEqualStrings("progress 50%", pane.msg[0..pane.msg_len]); - try std.testing.expectEqual(@as(u8, 0), pane.msg_older_len); -} - -test "messages stack, oldest on top, each on its own timing" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = p.panes[p.view().active].?; - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Verbose off")); - try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 160")); - // `Verbose off` announced itself before it took effect. - pane.msg_len = 0; - pane.msg_life = .{}; - setMessage(p, .at(p.view().active), "first"); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); - // A second message stacks under the first instead of replacing it. - setMessage(p, .at(p.view().active), "second"); - try std.testing.expectEqual(@as(u8, 1), pane.msg_older_len); - try std.testing.expectEqualStrings("first", pane.msg_older[0].slice()); - try std.testing.expectEqualStrings("second", pane.msg[0..pane.msg_len]); - collectNotices(p, pane, p.view().rects[p.view().active], p.view().active); - try std.testing.expectEqual(Pane.Notices.Kind.older, pane.notices.kinds[0]); - try std.testing.expectEqual(Pane.Notices.Kind.message, pane.notices.kinds[1]); - // One key dismisses both, each on its own clock: a line already - // lingering keeps its timing. - p.update(.{ .key = .{ .cp = 'h' } }); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_older[0].life.phase); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - p.update(.tick); - setMessage(p, .at(p.view().active), "third"); - try std.testing.expectEqual(@as(u8, 2), pane.msg_older_len); - p.update(.{ .key = .{ .cp = 'h' } }); - const linger = messageFrames(p.settings.message_linger_ms); - for (0..linger - 1) |_| p.update(.tick); - // The first two are gone; the third, a frame later, still lingers. - try std.testing.expectEqual(@as(u8, 0), pane.msg_older_len); - try std.testing.expectEqualStrings("third", pane.msg[0..pane.msg_len]); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - p.update(.tick); - try std.testing.expectEqual(@as(u16, 0), pane.msg_len); - try std.testing.expect(!p.animationActive()); - // A status ticking in place does not pile up. - setStatus(p, .at(p.view().active), "lsp 10%"); - setStatus(p, .at(p.view().active), "lsp 20%"); - try std.testing.expectEqual(@as(u8, 0), pane.msg_older_len); - // Lines keep their rows: when the top one leaves, the one under it does - // not move up, and the next message fills the gap instead. - setMessage(p, .at(p.view().active), "top"); - setMessage(p, .at(p.view().active), "under"); - try std.testing.expectEqual(@as(u8, 0), pane.msg_older[0].slot); - try std.testing.expectEqual(@as(u8, 1), pane.msg_slot); - pane.removeOlderMessage(0); - collectNotices(p, pane, p.view().rects[p.view().active], p.view().active); - try std.testing.expectEqual(@as(u8, 1), pane.notices.row[0]); - try std.testing.expectEqual(@as(u8, 2), pane.notices.span()); - setMessage(p, .at(p.view().active), "fills"); - try std.testing.expectEqual(@as(u8, 0), pane.msg_slot); - try std.testing.expectEqual(@as(u8, 1), pane.msg_older[0].slot); - // The stack keeps the newest few. - for ([_][]const u8{ "a", "b", "c", "d", "e" }) |text| setMessage(p, .at(p.view().active), text); - try std.testing.expectEqual(@as(u8, Pane.message_stack), pane.msg_older_len); - try std.testing.expectEqualStrings("b", pane.msg_older[0].slice()); - try std.testing.expectEqualStrings("e", pane.msg[0..pane.msg_len]); + try std.testing.expectEqual(@as(u8, max_live), m.live_len); + try std.testing.expectEqualStrings("w3", m.live[0].text.slice()); } test "a builtin announces itself on the message row, and Verbose silences it" { @@ -792,11 +780,11 @@ test "a builtin announces itself on the message row, and Verbose silences it" { // Wrap, not Collapse: a builtin that folds its own pane away leaves no row // for its announcement, which is correct and not what this is testing. try std.testing.expect(p.executeBuiltinLine(p.view().active, "Wrap")); - try std.testing.expectEqualStrings("Wrap", pane.msg[0..pane.msg_len]); + try std.testing.expectEqualStrings("Wrap", p.messages.saidBy(pane)); // A value it does not take is said, in place of the announcement. const wrapped = p.settings.wrap; try std.testing.expect(p.executeBuiltinLine(p.view().active, "Wrap maybe")); - try std.testing.expectEqualStrings("Wrap: takes on, off", pane.msg[0..pane.msg_len]); + try std.testing.expectEqualStrings("Wrap: takes on, off", p.messages.saidBy(pane)); try std.testing.expectEqual(wrapped, p.settings.wrap); // The notice list is what the paint pass reads, so the announcement has to @@ -825,12 +813,12 @@ test "a builtin announces itself on the message row, and Verbose silences it" { // Msg owns the row itself, so it does not announce over its own text. try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg hello")); - try std.testing.expectEqualStrings("hello", pane.msg[0..pane.msg_len]); + try std.testing.expectEqualStrings("hello", p.messages.saidBy(pane)); - pane.msg_len = 0; + p.messages.live_len = 0; p.settings.verbose = false; try std.testing.expect(p.executeBuiltinLine(p.view().active, "Wrap")); - try std.testing.expectEqual(@as(u16, 0), pane.msg_len); + try std.testing.expectEqual(@as(usize, 0), p.messages.saidBy(pane).len); } test "notices stack over the body's top rows without moving them" { @@ -874,47 +862,34 @@ test "notices stack over the body's top rows without moving them" { // Gone with the notices: no stale chip left behind. p.view().prefix = .none; - pane.msg_len = 0; + p.messages.live_len = 0; _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); for (p.surface.tagLayers()) |*layer| try std.testing.expect(layer.rows == 0 or layer.kind != .notice); } -test "an idle core wants no wake and a lingering message sleeps to its linger's end" { +test "an idle core wants no wake, and a message wakes it once, when it dims" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); while (p.nextEffect()) |_| {} - const frame = pardes.frame_ns; p.advance(1_000_000); + p.messages.live_len = 0; try std.testing.expectEqual(@as(?u64, null), p.nextWake()); - const pane = p.panes[p.view().active].?; - // Shown, a message asks for no wake. + try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 160")); try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg hello")); - try std.testing.expectEqual(Pane.MessagePhase.shown, pane.msg_life.phase); + const said = p.messages.headline().?; + try std.testing.expect(!said.faded(p.clock.now_ns, p.settings.message_linger_ms)); + // The core sleeps to its dimming, not a frame at a time. + const frame = pardes.frame_ns; + const due = said.t + 160 * std.time.ns_per_ms; + const woken = p.nextWake().?; + try std.testing.expect(woken >= due and woken < due + frame); + p.advance(woken); + try std.testing.expect(said.faded(p.clock.now_ns, p.settings.message_linger_ms)); try std.testing.expectEqual(@as(?u64, null), p.nextWake()); - // A key starts the 800 ms linger: the core sleeps to its end, not a - // frame at a time, and the frames it jumps draw nothing. - p.update(.{ .key = .{ .cp = 'x' } }); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - const linger = messageFrames(p.settings.message_linger_ms); - const due = p.clock.stepped_ns + linger * frame; - try std.testing.expectEqual(@as(?u64, due), p.nextWake()); - p.needs_frame = false; - p.advance(due - frame); - try std.testing.expectEqual(@as(?u64, due), p.nextWake()); - try std.testing.expectEqual(Pane.MessagePhase.lingering, pane.msg_life.phase); - p.advance(due); - try std.testing.expectEqual(@as(usize, 0), pane.msg_len); + // A warning is bright twice as long; with no linger nothing wakes. + try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 0")); + say(p, .warn, p.view().active, "careful"); try std.testing.expectEqual(@as(?u64, null), p.nextWake()); - - // A long linger ends when it says, jumped in one step, not stretched by - // the cap on catching up with moving frames. - try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 5000")); - try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg again")); - p.update(.{ .key = .{ .cp = 'x' } }); - const long_due = p.clock.stepped_ns + messageFrames(5000) * frame; - try std.testing.expectEqual(@as(?u64, long_due), p.nextWake()); - p.advance(long_due); - try std.testing.expectEqual(@as(usize, 0), pane.msg_len); } -- cgit v1.3