summaryrefslogtreecommitdiff
path: root/core/Messages.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Messages.zig')
-rw-r--r--core/Messages.zig208
1 files changed, 134 insertions, 74 deletions
diff --git a/core/Messages.zig b/core/Messages.zig
index 747e5e2f..b4950b4f 100644
--- a/core/Messages.zig
+++ b/core/Messages.zig
@@ -2,8 +2,9 @@
//! 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).
+//! 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).
const pardes = @import("root.zig");
const memory = @import("memory.zig");
const std = @import("std");
@@ -277,7 +278,7 @@ pub fn say(p: *Pardes, kind: Kind, id: usize, text: []const u8) void {
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| {
+ if (cause.file) |f| src.set(f.path) else if (if (cause.addon) |k| k.path.len > 0 else false) src.set(cause.addon.?.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;
@@ -407,11 +408,80 @@ pub fn dimDue(p: *const Pardes) ?u64 {
return soonest;
}
+/// The notice row's line (SPEC §10.3), `width` columns: on the left the
+/// message the row leads with (`headline`), its glyph, its cause, its
+/// words, `×n` and the places it names; on the right how many more live,
+/// the questions waiting, or the way to the history. With nothing live the
+/// left says where the focused pane's caret is. Built each frame from the
+/// state, in `out`.
+pub const Row = struct {
+ /// The left part's end and the right part's start, in bytes of `text`.
+ left: usize = 0,
+ right: usize = 0,
+ text: []const u8 = "",
+};
+
+/// Room for the notice row's line: the widest window's at the tagline's
+/// pitch (Layer.capacity) is cut to it.
+pub const notice_row_cap = 1024;
+
+pub fn noticeRow(p: *const Pardes, width: usize, out: *[notice_row_cap]u8) Row {
+ const m = &p.messages;
+ var w: std.Io.Writer = .fixed(out);
+ var live: usize = 0;
+ var questions: usize = 0;
+ for (m.live[0..m.live_len]) |*s| {
+ if (s.kind == .progress) continue;
+ live += 1;
+ if (s.kind == .question) questions += 1;
+ }
+ const lead = m.headline();
+ if (lead) |s| {
+ w.print("{s} ", .{s.kind.glyph()}) catch {};
+ // Its cause first, unless its words name it already.
+ const words = s.words();
+ if (s.src.len > 0 and std.mem.indexOf(u8, words, s.src.slice()) == null) w.print("{s} ", .{s.src.slice()}) catch {};
+ w.writeAll(words) catch {};
+ if (s.n > 1) w.print(" \u{d7}{d}", .{s.n}) catch {};
+ for (s.places[0..s.places_len], 0..) |*place, i| w.print("{s} {s}:{d}:{d}", .{ if (i > 0) " \u{b7}" else "", place.path.slice(), place.line, place.col }) catch {};
+ } else if (p.panes[p.view().active]) |pane| {
+ if (pane.file) |f| w.print("{s}:{d}", .{ f.path, pane.body.cur_row + 1 }) catch {} else w.writeAll(pane.cwdSlice()) catch {};
+ }
+ const left_end = w.end;
+ var right: [64]u8 = undefined;
+ var r: std.Io.Writer = .fixed(&right);
+ if (live > 1) r.print("+{d} \u{b7} ", .{live - 1}) catch {};
+ if (questions > 0)
+ r.print("{d} question{s} \u{b7} SPC e", .{ questions, if (questions == 1) "" else "s" }) catch {}
+ else if (lead != null and lead.?.kind == .@"error" and lead.?.places_len > 0)
+ r.writeAll("SPC e walk") catch {}
+ else if (m.len > 0)
+ 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;
+ if (panes.File.displayWidth(tab, out[0..cut]) > room) {
+ cut = panes.File.rawAtDisplay(tab, out[0..left_end], room);
+ while (cut > 0 and panes.File.displayWidth(tab, out[0..cut]) > room) {
+ cut -= 1;
+ while (cut > 0 and out[cut] & 0xC0 == 0x80) cut -= 1;
+ }
+ }
+ w.end = cut;
+ const gap = width -| (panes.File.displayWidth(tab, out[0..cut]) + tail_w);
+ for (0..gap) |_| w.writeByte(' ') catch {};
+ const right_at = w.end;
+ w.writeAll(tail) catch {};
+ return .{ .left = cut, .right = right_at, .text = w.buffered() };
+}
+
/// 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 => p.messages.saidBy(pane),
.leader => leaderText(p, leader_buf),
.prompt => pane.promptText() orelse "",
};
@@ -603,8 +673,6 @@ 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;
- // 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.
@@ -769,7 +837,7 @@ test "an echo or a notice retires those before it, an error the error before it,
try std.testing.expectEqualStrings("w3", m.live[0].text.slice());
}
-test "a builtin announces itself on the message row, and Verbose silences it" {
+test "a builtin announces itself on the notice row, an echo, and Verbose silences it" {
if (pardes.Term.ops == null) return error.SkipZigTest; // a terminal's tag row
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
@@ -777,39 +845,35 @@ test "a builtin announces itself on the message row, and Verbose silences it" {
p.sync();
try std.testing.expect(p.settings.verbose);
- // 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.
+ // Wrap, not Collapse: a builtin that folds its own pane away is not
+ // what this is testing.
try std.testing.expect(p.executeBuiltinLine(p.view().active, "Wrap"));
try std.testing.expectEqualStrings("Wrap", p.messages.saidBy(pane));
- // A value it does not take is said, in place of the announcement.
+ try std.testing.expectEqual(Kind.echo, p.messages.headline().?.kind);
+ // A value it does not take is said, an error, and leads the row.
const wrapped = p.settings.wrap;
try std.testing.expect(p.executeBuiltinLine(p.view().active, "Wrap maybe"));
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
- // reach it, not just the buffer.
- collectNotices(p, pane, p.view().rects[p.view().active], p.view().active);
- try std.testing.expect(pane.notices.len > 0);
- try std.testing.expectEqual(Pane.Notices.Kind.message, pane.notices.kinds[0]);
-
- // ...and it is carried as a tagline band, not a body row: that is what
- // gives it the tagline's height, its small font and its border.
+ // ...on the window's foot row, a tagline-shaped layer, not on a chip
+ // over the pane's body.
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
- _ = try p.render(frame.allocator());
- const r = p.view().rects[p.view().active];
- const band = for (p.surface.tagLayers()) |*layer| {
- if (layer.rows != 0 and layer.kind == .notice) break layer;
- } else return error.NoNoticeBand;
- try std.testing.expectEqual(@as(u16, 1), band.viewport.h);
- try std.testing.expectEqual(r.y + BOX_H, band.viewport.y);
- // Right aligned: the text ends at the band's right edge.
- const line = band.cells[0..band.cols];
- var last: usize = line.len;
- while (last > 0 and line[last - 1].grapheme()[0] == ' ') last -= 1;
- try std.testing.expect(last > 0);
- try std.testing.expect(line[0].grapheme()[0] == ' ');
+ const s = try p.render(frame.allocator());
+ try std.testing.expectEqual(@as(u8, 0), pane.notices.len);
+ const row = for (s.tagLayers()) |*layer| {
+ if (layer.rows != 0 and layer.kind == .notice_row) break layer;
+ } else return error.NoNoticeRow;
+ try std.testing.expectEqual(@as(u16, 1), row.viewport.h);
+ try std.testing.expectEqual(p.view().screen_h - 1, row.viewport.y);
+ var want: [64]u8 = undefined;
+ const said = try std.fmt.bufPrint(&want, "\u{2717} @p{d} Wrap: takes on, off", .{pane.serial});
+ const grid = s.cells[@as(usize, row.viewport.y) * s.cols ..][0..s.cols];
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(std.testing.allocator);
+ for (grid) |cell| try text.appendSlice(std.testing.allocator, cell.grapheme());
+ try std.testing.expect(std.mem.indexOf(u8, text.items, said) != null);
// 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"));
@@ -821,52 +885,48 @@ test "a builtin announces itself on the message row, and Verbose silences it" {
try std.testing.expectEqual(@as(usize, 0), p.messages.saidBy(pane).len);
}
-test "notices stack over the body's top rows without moving them" {
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+test "the notice row leads with the most important live message, its count and places, and says how many more; with none, where the caret is" {
+ const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 });
defer p.deinit();
+ const pane = try p.setTestFile("one\ntwo\nthree\n");
const id = p.view().active;
- const pane = p.panes[id].?;
- p.sync();
- var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer frame.deinit();
-
- const r = p.view().rects[id];
- const top = p.bodyTop(pane, r);
-
- // A message and a leader prefix at once: two chips, in the order
- // collectNotices chose, each one row tall and each one row further down.
- try std.testing.expect(p.executeBuiltinLine(id, "Msg first"));
- p.view().prefix = .leader;
- p.view().leader_n = 0;
- _ = frame.reset(.retain_capacity);
- _ = try p.render(frame.allocator());
- try std.testing.expectEqual(@as(u8, 2), pane.notices.len);
- // An overlay, not a reservation: the body still starts where it did.
- try std.testing.expectEqual(top, p.bodyTop(pane, r));
-
- var seen: usize = 0;
- for (p.surface.tagLayers()) |*layer| {
- if (layer.rows == 0 or layer.kind != .notice) continue;
- try std.testing.expectEqual(@as(u16, 1), layer.viewport.h);
- // Sized to its own message, right up against the pane's right edge --
- // not the width of the pane.
- try std.testing.expectEqual(r.x + r.w, layer.viewport.x + layer.viewport.w);
- try std.testing.expect(layer.viewport.w < r.w - config.GUTTER);
- try std.testing.expect(layer.viewport.y == top or layer.viewport.y == top + 1);
- try std.testing.expectEqual(pane.notices.left[layer.viewport.y - top], layer.viewport.x);
- seen += 1;
- }
- try std.testing.expectEqual(@as(usize, 2), seen);
- // "first" is longer than "SPC", so its chip is wider.
- try std.testing.expect(pane.notices.left[0] < pane.notices.left[1]);
-
- // Gone with the notices: no stale chip left behind.
- p.view().prefix = .none;
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);
+ p.messages.len = 0;
+ pane.body.cur_row = 1;
+ var buf: [notice_row_cap]u8 = undefined;
+ var row = noticeRow(p, 60, &buf);
+ try std.testing.expectEqualStrings("/test.txt:2", row.text[0..row.left]);
+ try std.testing.expectEqual(@as(usize, 60), panes.File.displayWidth(p.settings.tab, row.text));
+ // A notice, then an error and a warning: the error leads, newest first
+ // within a rank; the others are counted.
+ say(p, .notice, id, "indexed");
+ say(p, .warn, id, "careful");
+ say(p, .@"error", id, "broke");
+ row = noticeRow(p, 60, &buf);
+ try std.testing.expectEqualStrings("\u{2717} /test.txt broke", row.text[0..row.left]);
+ try std.testing.expect(std.mem.endsWith(u8, row.text, "+1 \u{b7} 3 messages \u{b7} SPC h m"));
+ // A burst of the same: one message, counted; the row keeps its width.
+ for (0..9) |_| say(p, .@"error", id, "broke");
+ row = noticeRow(p, 60, &buf);
+ try std.testing.expectEqualStrings("\u{2717} /test.txt broke \u{d7}10", row.text[0..row.left]);
+ try std.testing.expectEqual(@as(usize, 60), panes.File.displayWidth(p.settings.tab, row.text));
+ // An error that names places offers the walk, its places as words.
+ const err = keep(p, .@"error", 0, "@p9 zig build", "exit 1");
+ err.places[0] = .{ .line = 25, .col = 12 };
+ err.places[0].path.set("core/pick.zig");
+ err.places_len = 1;
+ row = noticeRow(p, 80, &buf);
+ try std.testing.expectEqualStrings("\u{2717} @p9 zig build exit 1 core/pick.zig:25:12", row.text[0..row.left]);
+ try std.testing.expect(std.mem.endsWith(u8, row.text, "+1 \u{b7} SPC e walk"));
+ // Too narrow: the left gives way, the right stays whole.
+ row = noticeRow(p, 30, &buf);
+ try std.testing.expect(std.mem.endsWith(u8, row.text, "+1 \u{b7} SPC e walk"));
+ try std.testing.expect(panes.File.displayWidth(p.settings.tab, row.text) <= 30);
+ // A question outranks a warning, not an error; it is counted on the right.
+ _ = keep(p, .question, pane.serial, "/test.txt", "changed on disk");
+ row = noticeRow(p, 80, &buf);
+ try std.testing.expect(std.mem.startsWith(u8, row.text, "\u{2717} @p9"));
+ try std.testing.expect(std.mem.endsWith(u8, row.text, "+2 \u{b7} 1 question \u{b7} SPC e"));
}
test "an idle core wants no wake, and a message wakes it once, when it dims" {