diff options
Diffstat (limited to 'core/Messages.zig')
| -rw-r--r-- | core/Messages.zig | 81 |
1 files changed, 55 insertions, 26 deletions
diff --git a/core/Messages.zig b/core/Messages.zig index 3fef1842..8ecf2919 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -3,8 +3,8 @@ //! 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`); //! the notice row at the window's foot that indexes them; and the -//! transient bands a pane shows over its body's top rows (the pending -//! leader chord, a prompt waiting for input). +//! transient band a pane shows over its body's top rows (a prompt waiting +//! for input). const pardes = @import("root.zig"); const memory = @import("memory.zig"); const std = @import("std"); @@ -190,16 +190,6 @@ pub const LoggedMessage = struct { } }; -/// The room the leader band's line takes (TagState.lastUsed): a leaf's -/// key, word and summary (builtins.summary_max), and "(last used here)". -pub const leader_room = 192; - -/// The leaf the current which-key level ran last, and what it does: the -/// line under a rolled level (walk UI SPEC §3.7). -fn leaderText(p: *const Pardes, buf: *[leader_room]u8) []const u8 { - return pardes.TagState.lastUsed(p, p.view(), buf) orelse ""; -} - pub const Message = struct { const libc = std.c; const Tm = extern struct { @@ -683,6 +673,8 @@ pub const Row = struct { left: usize = 0, right: usize = 0, text: []const u8 = "", + /// The message the left says, when it says one. + lead: ?*const Said = null, }; /// Room for the notice row's line: the widest window's at the tagline's @@ -691,6 +683,15 @@ pub const notice_row_cap = 1024; pub fn noticeRow(p: *const Pardes, width: usize, out: *[notice_row_cap]u8) Row { const m = &p.messages; + const tab = p.settings.tab; + // A rolled level's last leaf, while the level shows: what it does, the + // whole row (SPEC §3.7's first mockup). + if (pardes.TagState.lastUsed(p, p.view(), out)) |used| { + var end = @min(used.len, panes.File.rawAtDisplay(tab, used, width)); + while (end > 0 and panes.File.displayWidth(tab, used[0..end]) > width) end -= 1; + while (end > 0 and end < used.len and used[end] & 0xC0 == 0x80) end -= 1; + return .{ .left = end, .right = end, .text = used[0..end] }; + } var w: std.Io.Writer = .fixed(out); var live: usize = 0; var questions: usize = 0; @@ -723,7 +724,6 @@ pub fn noticeRow(p: *const Pardes, width: usize, out: *[notice_row_cap]u8) Row { r.print("{d} message{s} \u{b7} SPC h m", .{ m.len, if (m.len == 1) "" else "s" }) catch {}; const tail = r.buffered(); // The left cut to make room for the right, a blank between them. - const tab = p.settings.tab; const tail_w = panes.File.displayWidth(tab, tail); const room = width -| (tail_w + 1); var cut = left_end; @@ -739,14 +739,13 @@ pub fn noticeRow(p: *const Pardes, width: usize, out: *[notice_row_cap]u8) Row { for (0..gap) |_| w.writeByte(' ') catch {}; const right_at = w.end; w.writeAll(tail) catch {}; - return .{ .left = cut, .right = right_at, .text = w.buffered() }; + return .{ .left = cut, .right = right_at, .text = w.buffered(), .lead = lead }; } /// 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: *[leader_room]u8) []const u8 { +pub fn noticeText(pane: *const Pane, i: usize) []const u8 { return switch (pane.notices.kinds[i]) { - .leader => leaderText(p, leader_buf), .prompt => pane.promptText() orelse "", }; } @@ -915,8 +914,8 @@ pub fn reportFailure(p: *Pardes, id: usize, text: []const u8) void { /// in what order. Everything downstream reads the answer: the body layer /// so it does not paint under them, and the paint pass so it can stack /// them instead of letting the last one win the row. -pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { - pane.notices = bands(p, p.view(), pane, pane.tag_rows, r, id); +pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect) void { + pane.notices = bands(p, pane, pane.tag_rows, r); // Each band's chip at the right edge, as wide as its own text and a // blank cell either side, the pane's width at most: a click finds it // whether or not a frame has drawn it. An overlay over the body's top @@ -925,21 +924,18 @@ pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { // draws, and a chip narrower than its text there would clip it. const tx = r.x + config.GUTTER; const tw = r.w -| config.GUTTER; - var leader_buf: [leader_room]u8 = undefined; for (0..pane.notices.len) |i| { - const chip = panes.File.displayWidth(p.settings.tab, noticeText(p, pane, i, &leader_buf)) + 2; + const chip = panes.File.displayWidth(p.settings.tab, noticeText(pane, i)) + 2; pane.notices.left[i] = if (tw == 0) std.math.maxInt(u16) else tx + tw - @as(u16, @intCast(@min(chip, tw))); } } -/// The bands pane `id` shows in view `v`, over `r` below its `tag_rows`: -/// the live view's each step, and any view's for its kind's facts -/// (Kind.viewOf), made from what a dump keeps (the bands are derived). -pub fn bands(p: *const Pardes, v: *const Pardes.View, pane: *const Pane, tag_rows: u16, r: Rect, id: usize) Pane.Notices { +/// The bands a pane shows over `r` below its `tag_rows`: the live view's +/// each step, and any view's for its kind's facts (Kind.viewOf), made from +/// what a dump keeps (the bands are derived). +pub fn bands(p: *const Pardes, pane: *const Pane, tag_rows: u16, r: Rect) Pane.Notices { var n: Pane.Notices = .{}; if (r.w <= config.GUTTER or r.h <= tag_rows) return n; - var used_buf: [leader_room]u8 = undefined; - if (id == v.active and pardes.TagState.lastUsed(p, v, &used_buf) != null) n.push(.leader); // Del's question shows once its delay is up: answered before that, it // was never there. const del_waiting = pane.prompt == .del_side and pane.question_due_ns > p.clock.now_ns; @@ -1195,6 +1191,39 @@ test "the notice row leads with the most important live message, its count and p try std.testing.expect(std.mem.endsWith(u8, row.text, "+2 \u{b7} 1 question \u{b7} SPC e")); } +test "a rolled level's last leaf is said on the notice row, the whole row, never in a chip over the body; at rest the row is the messages' again" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + p.messages.live_len = 0; + p.clock.now_ns = 5 * std.time.ns_per_s; + p.clock.stepped_ns = p.clock.now_ns; + // SPC t w: Wrap, run from SPC t; then SPC t again, shown. + for (" tw t") |c| p.update(.{ .key = .{ .cp = c } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + p.sync(); + _ = try p.render(p.scratch.allocator()); + var buf: [notice_row_cap]u8 = undefined; + var row = noticeRow(p, 110, &buf); + try std.testing.expect(std.mem.startsWith(u8, row.text, "w Wrap: ")); + try std.testing.expect(std.mem.endsWith(u8, row.text, " (last used here)")); + try std.testing.expectEqual(row.text.len, row.right); + try std.testing.expect(row.lead == null); + try std.testing.expectEqual(@as(u8, 0), pane.notices.len); + // Narrower than the line: cut to the width, never past it. + row = noticeRow(p, 12, &buf); + try std.testing.expectEqualStrings("w Wrap: Long", row.text); + // Esc: at rest, the messages' (Wrap's echo), and with none where the + // caret is. + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + row = noticeRow(p, 110, &buf); + try std.testing.expect(row.lead != null); + p.messages.live_len = 0; + row = noticeRow(p, 110, &buf); + try std.testing.expect(std.mem.startsWith(u8, row.text, "/test.txt:1")); +} + 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(); |
