diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Band.zig | 193 | ||||
| -rw-r--r-- | core/Command.zig | 34 | ||||
| -rw-r--r-- | core/Core.zig | 8 | ||||
| -rw-r--r-- | core/Messages.zig | 38 | ||||
| -rw-r--r-- | core/Pick.zig | 180 | ||||
| -rw-r--r-- | core/Prompt.zig | 75 | ||||
| -rw-r--r-- | core/TagState.zig | 472 | ||||
| -rw-r--r-- | core/Trail.zig | 260 | ||||
| -rw-r--r-- | core/bindings.zig | 38 | ||||
| -rw-r--r-- | core/draw.zig | 99 | ||||
| -rw-r--r-- | core/dump.zig | 1 | ||||
| -rw-r--r-- | core/layout.zig | 16 | ||||
| -rw-r--r-- | core/lsp/Ask.zig | 2 | ||||
| -rw-r--r-- | core/mouse.zig | 5 | ||||
| -rw-r--r-- | core/tagline.zig | 78 | ||||
| -rw-r--r-- | core/views.zig | 48 |
16 files changed, 1232 insertions, 315 deletions
diff --git a/core/Band.zig b/core/Band.zig index 5ddbe5af..507f1bee 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -82,8 +82,12 @@ pub const max_steps = 4; pub const max_spines = 12; /// The narrowest a step is drawn (SPEC §3.6: box, spines, one 24-cell column). pub const min_width = 24; +/// The widest a list's rows are laid out (UX D5). +const row_cells = 52; /// A doc's rows, the fewest a band with one has. const doc_rows = 4; +/// The body rows a band leaves its pane at least: it gives way first (UX D6). +pub const body_rows = 6; /// A level's rows past which it flows into two columns. const one_column = 9; @@ -124,6 +128,22 @@ pub fn takes(p: *Pardes, id: usize) u16 { return @max(doc_rows, levelRows(n)); } +/// A terminal's and a kind's band is drawn over the top rows of its body +/// (UX D6's borrowing, R3: drawn, never written): a transient state never +/// resizes a pty under a program or a kind's view (a PDF's), whose size +/// only the user's resize changes. Their `band_rows` stay 0. +pub fn over(pane: *const Pane) bool { + return pane.isTerminal() or pane.addon != null; +} + +/// The rows pane `id`'s band is drawn on: those it takes from the layout, +/// or over a terminal's or a kind's body, as many of its body rows as the +/// band wants. +pub fn shownRows(p: *Pardes, pane: *const Pane, id: usize) u16 { + if (pane.band_rows > 0 or !over(pane) or pane.collapsed) return pane.band_rows; + return @min(takes(p, id), p.view().rects[id].h -| pane.tag_rows); +} + fn levelRows(n: u16) u16 { return if (n > one_column) (n + 1) / 2 else n; } @@ -392,26 +412,33 @@ pub fn docStep(p: *Pardes, arena: std.mem.Allocator, root: bindings.Root, path: return .{ .rows = rows.items, .doc = true }; } -/// Where each step goes: right of the spines, a rule's cell between steps; -/// a doc takes what is left, wrapped to it. Narrow, the doc goes first, -/// then the spines (the steps start at the band's left edge). +/// Where each step goes (UX D5): from two cells right of the last spine, +/// a rule's cell between steps; a level as wide as its widest row (two +/// columns past nine), a list of rows `row_cells` at most, a doc the width +/// the others leave, wrapped to it, `min_width` at least. Narrower than +/// the steps (the hints already gone from the head), the band gives way: +/// the doc leaves it, then the last step unrolled (a picker's acts) takes +/// the place of the others; one step left is held to the room there is, +/// its rows cut with `…` (Band.cutRow): the spines stay, nothing is clipped +/// off the right edge. fn place(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !void { const tab = p.settings.tab; try band.findSpines(p, arena, pane, id); - var x: u16 = if (band.nspines > 0) band.spines[band.nspines - 1] + 2 else 0; + const x0: u16 = if (band.nspines > 0) band.spines[band.nspines - 1] + 2 else 0; var need: u16 = 0; + var lists: u8 = 0; for (band.steps[0..band.nsteps]) |*step| { if (step.doc) continue; var widest: usize = 0; for (step.rows) |row| widest = @max(widest, File.displayWidth(tab, row.text) + if (row.meta.len > 0) 2 + File.displayWidth(tab, row.meta) else 0); - const column: u16 = @intCast(@min(widest + 2, std.math.maxInt(u16) / 4, @max(min_width, band.columns -| x))); + const column: u16 = @intCast(@min(widest, row_cells) + 2); step.width = if (step.two) 2 * column + 1 else column; need += step.width + 2; + lists += 1; } - if (x + need > band.columns) { - band.nspines = 0; - x = 0; - } + // The last list alone where they do not all fit. + var skip: u8 = if (x0 + need > band.columns and lists > 1) lists - 1 else 0; + var x = x0; var kept: u8 = 0; for (band.steps[0..band.nsteps]) |step| { var placed = step; @@ -420,6 +447,12 @@ fn place(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usi if (room < min_width) continue; placed.width = room; placed.rows = try wrap(arena, tab, step.rows, room -| 1, band.height); + } else { + if (skip > 0) { + skip -= 1; + continue; + } + placed.width = @min(placed.width, @max(band.columns -| x, @min(min_width, band.columns))); } placed.x = x; x +|= placed.width + 2; @@ -429,20 +462,16 @@ fn place(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usi band.nsteps = kept; } -/// The display columns of the head's key cells, when they are all on the -/// tag's last row (the spines hang from there). +/// The display columns of the head's key cells: a spine hangs under each, +/// through the tag's rows under them and the band's (UX D5). fn findSpines(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !void { const h = (try TagState.head(p, arena, pane)) orelse return; const whole = try tagline.tagText(p, arena, pane); const shown = try tagline.wrapTag(p.settings.tab, arena, whole, tagline.tagWidth(p, id)); const lead = whole.len - h.text.len; - const last = shown.rows() -| 1; for (h.spans[0..h.cells]) |span| { const at = modal.positionAt(shown.text, shown.fromReal(lead + span.lo)); - if (at.row != last or band.nspines == max_spines) { - band.nspines = 0; - return; - } + if (band.nspines == max_spines) return; band.spines[band.nspines] = @intCast(File.rawDisplayCol(p.settings.tab, modal.lineSlice(shown.text, at.row), at.col)); band.nspines += 1; } @@ -519,12 +548,14 @@ pub fn line(band: *const Band, arena: std.mem.Allocator, tab: u16, y: u16) !Line const lead: []const u8 = if (step.doc) "" else if (row.on) "▶ " else if (row.mark) "• " else " "; try text.appendSlice(arena, lead); col += @intCast(File.displayWidth(tab, lead)); - try parts.append(arena, .{ .col = col, .width = per -| @as(u16, @intCast(File.displayWidth(tab, lead))), .row = row }); const room = per -| @as(u16, @intCast(File.displayWidth(tab, lead))); + const fit = try cutRow(arena, tab, row, room); + try parts.append(arena, .{ .col = col, .width = room, .row = fit }); const at = col; - const cut = File.byteAtDisplay(tab, row.text, room); - try text.appendSlice(arena, row.text[0..cut]); - col += @intCast(File.displayWidth(tab, row.text[0..cut])); + try text.appendSlice(arena, fit.text); + col += @intCast(File.displayWidth(tab, fit.text)); + // Its meta at the part's right end, when it fits after the + // whole text: it gives way before the text is cut (UX D5). if (metaShown(tab, row, room)) { const meta: u16 = @intCast(File.displayWidth(tab, row.meta)); while (col < at + room - meta) : (col += 1) try text.append(arena, ' '); @@ -541,6 +572,36 @@ pub fn metaShown(tab: u16, row: Row, room: u16) bool { return row.meta.len > 0 and File.displayWidth(tab, row.text) + 2 + File.displayWidth(tab, row.meta) <= room; } +/// Row `row` held to `room` cells (UX D2): a longer one cut with `…`, a +/// path (its directory faint: Find's rows) from the left +/// (`…core/Store.zig`), anything else from the right; its marks and faint +/// bytes moved with its text. +fn cutRow(arena: std.mem.Allocator, tab: u16, row: Row, room: u16) !Row { + if (File.displayWidth(tab, row.text) <= room) return row; + var fit = row; + var marks: std.ArrayList(u16) = .empty; + if (row.faint > 0 and row.pre == 0 and room > 1) { + var from = row.text.len; + while (from > 0) { + const before = modal.prevGrapheme(row.text, from); + if (File.displayWidth(tab, row.text[before..]) > room - 1) break; + from = before; + } + fit.text = try std.mem.concat(arena, u8, &.{ "…", row.text[from..] }); + for (row.marks) |m| if (m >= from) try marks.append(arena, @intCast(m - from + "…".len)); + fit.faint = @intCast(if (row.faint > from) row.faint - from + "…".len else "…".len); + } else { + const cut = File.byteAtDisplay(tab, row.text, room -| 1); + fit.text = try std.mem.concat(arena, u8, &.{ row.text[0..cut], if (room > 0) "…" else "" }); + for (row.marks) |m| if (m < cut) try marks.append(arena, m); + fit.key = @intCast(@min(row.key, cut)); + fit.pre = @intCast(@min(row.pre, cut)); + fit.faint = @intCast(@min(row.faint, cut -| fit.pre)); + } + fit.marks = marks.items; + return fit; +} + /// The word under column `col` of band row `y` of pane `id`, at the /// tagline's pitch: what B2 executes and B3 looks at. pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, y: u16, col: u16) !?[]const u8 { @@ -565,6 +626,49 @@ fn layerRow(arena: std.mem.Allocator, layer: *const pardes.Layer, row: usize) ![ return out.items; } +test "a narrow band gives way and keeps its spines: the doc goes, the steps start two cells right of the last spine, no row past the band's edge; the spines run on through the tag's rows under the head" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 44, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + const id = p.view().active; + p.sync(); + try std.testing.expect(pane.tag_rows > 1); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + // Help for SPC l: its key cells `␣ l`, its rows, no room for a doc. + for ([_]u21{ ' ', 'l', '?' }) |c| p.update(.{ .key = .{ .cp = c } }); + p.sync(); + const band = (try of(p, a, pane, id)).?; + try std.testing.expectEqual(@as(u8, 2), band.nspines); + try std.testing.expectEqual(@as(u8, 1), band.nsteps); + try std.testing.expectEqual(band.spines[1] + 2, band.steps[0].x); + for (0..band.height) |y| try std.testing.expect(File.displayWidth(p.settings.tab, (try band.line(a, p.settings.tab, @intCast(y))).text) <= band.columns); + // The tag's second row, kept from rest under the head's one: a spine + // under each key cell. + const s = try p.render(a); + const r = p.view().rects[id]; + const tag_y = p.tagTop(pane, r); + for (band.spines[0..band.nspines]) |col| try std.testing.expectEqualStrings("│", s.at(r.x + pardes.TAG_TEXT_INSET + col, tag_y + 1).grapheme()); + p.update(.{ .key = .{ .cp = Key.escape } }); +} + +test "a row longer than its room is cut with `…`: a path from the left, anything else from the right, its marks and faint bytes moved with it" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + const path = try cutRow(a, 8, .{ .text = "core/deep/dir/Store.zig", .faint = 14, .marks = &.{ 0, 14, 15 } }, 12); + try std.testing.expectEqualStrings("…r/Store.zig", path.text); + try std.testing.expectEqual(@as(u16, 5), path.faint); + try std.testing.expectEqualSlices(u16, &.{ 5, 6 }, path.marks); + const other = try cutRow(a, 8, .{ .text = "a.zig:3:1: hello world", .pre = 9, .marks = &.{ 10, 20 } }, 12); + try std.testing.expectEqualStrings("a.zig:3:1: …", other.text); + try std.testing.expectEqualSlices(u16, &.{10}, other.marks); + const fits = try cutRow(a, 8, .{ .text = "short" }, 12); + try std.testing.expectEqualStrings("short", fits.text); +} + test "SPC Down unrolls the level into a band under the tag's text, the body pushed down and never drawn over; Up on its first row rolls it up" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); @@ -602,7 +706,8 @@ test "SPC Down unrolls the level into a band under the tag's text, the body push for (s.cells[@as(usize, layer.viewport.y) * s.cols ..][layer.viewport.x..][0..layer.viewport.w]) |cell| try grid.appendSlice(frame.allocator(), cell.grapheme()); try std.testing.expect(std.mem.indexOf(u8, grid.items, "▶ b buffer…") != null); // The head names the level and says how to roll it up. - try std.testing.expect(std.mem.endsWith(u8, try tagline.tagText(p, frame.allocator(), pane), " ␣ SPC ↑ roll up · ? field · esc")); + const head = try tagline.tagText(p, frame.allocator(), pane); + try std.testing.expect(std.mem.startsWith(u8, head, "␣ SPC ") and std.mem.endsWith(u8, head, " ↑ roll up · ? field · esc")); // Down moves the cursor; Up back to the first row, then rolls up. p.update(.{ .key = .{ .cp = Key.down } }); try std.testing.expectEqual(@as(u8, 'c'), p.view().tag_state.hi); @@ -624,7 +729,35 @@ test "SPC Down unrolls the level into a band under the tag's text, the body push try std.testing.expect(!p.view().tag_state.unrolled); } -test "a pane whose body would drop below four rows under its band borrows them from its column" { +test "a band keeps its pane's body 60 % of the pane, borrowed from its column; where the column has not the rows, the band gives way before the body drops under 6" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + const top = p.view().active; + p.newScratchBelow(top); + const low = p.view().active; + p.newScratchBelow(low); + p.view().active = low; + p.sync(); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + const pane = p.panes[low].?; + const r = p.view().rects[low]; + try std.testing.expect(pane.band_rows >= doc_rows); + try std.testing.expect(@as(u32, r.h -| pane.tag_rows) * 5 >= @as(u32, r.h) * 3); + p.update(.{ .key = .{ .cp = Key.escape } }); + // A window of 12 rows: the band shrinks, the body keeps 6. + p.update(.{ .resize = .{ .cols = 120, .rows = 12 } }); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + p.sync(); + const short = p.view().rects[low]; + try std.testing.expect(short.h -| pane.tag_rows >= 6 or pane.band_rows == 0); + try std.testing.expect(pane.band_rows < Band.takes(p, low)); +} + +test "a pane whose body would drop below six rows under its band borrows them from its column" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 24 }); defer p.deinit(); @@ -641,13 +774,15 @@ test "a pane whose body would drop below four rows under its band borrows them f const pane = p.panes[low].?; const r = p.view().rects[low]; try std.testing.expect(r.h > before); - try std.testing.expect(r.h -| pane.tag_rows >= 4); - // The other panes dim; the one out of rest does not. + // Its body six rows at least (UX D6): a column too short for 60 %. + try std.testing.expect(r.h -| pane.tag_rows >= 6); + // No pane dims out of rest (UX D9): the others look as at rest. var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const s = try p.render(frame.allocator()); const other = p.view().rects[top]; - try std.testing.expect(s.at(other.x + 3, other.y).style.dim); + try std.testing.expect(!s.at(other.x + 3, other.y).style.dim); + try std.testing.expect(!s.at(other.x + 3, other.y + 1).style.dim); try std.testing.expect(!s.at(r.x + 3, r.y).style.dim); p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expectEqual(before, p.view().rects[low].h); @@ -737,7 +872,13 @@ test "a band is drawn in the theme's roles (UX D4): spines in the rule ink, a ch try std.testing.expectEqual(pardes.Color{ .rgb = inks.rule }, cell.style.fg); spines += 1; } - if (std.mem.eql(u8, cell.grapheme(), "▶")) cursor_row = row; + if (std.mem.eql(u8, cell.grapheme(), "▶")) { + cursor_row = row; + // The cursor's `▶` in the key ink on the selection, which + // covers its lead too (UX D4, D5). + try std.testing.expectEqual(pardes.Color{ .rgb = roles.key }, cell.style.fg); + try std.testing.expectEqual(pardes.Color{ .rgb = selected.ground }, cell.style.bg); + } }; try std.testing.expect(spines > 0); const on = layer.cells[cursor_row.? * layer.cols ..][0..layer.cols]; diff --git a/core/Command.zig b/core/Command.zig index 55c336f1..908358de 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -659,8 +659,22 @@ fn tagOf(p: *Pardes) ![]const u8 { return tagline.tagText(p, p.scratch.allocator(), p.panes[p.view().active].?); } +/// Whether `needle` is in `hay`, a run of two spaces or more in either read +/// as two: a right block's padding is its row's (UX D3). fn has(hay: []const u8, needle: []const u8) bool { - return std.mem.indexOf(u8, hay, needle) != null; + var in: [8192]u8 = undefined; + var of: [1024]u8 = undefined; + return std.mem.indexOf(u8, squeezed(hay, &in), squeezed(needle, &of)) != null; +} + +fn squeezed(s: []const u8, buf: []u8) []const u8 { + var n: usize = 0; + for (s, 0..) |c, i| { + if (c == ' ' and i >= 2 and s[i - 1] == ' ' and s[i - 2] == ' ') continue; + buf[n] = c; + n += 1; + } + return buf[0..n]; } /// Band row `y`'s text under the live view's active pane. @@ -685,7 +699,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, try std.testing.expectEqual(Prompt.Op.command, s.prompt.op); try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); // The head: its key cell, the field, the count, the hints. - try std.testing.expect(has(try tagOf(p), " : 16 commands Tab complete · ⏎ run · C-r history · esc")); + try std.testing.expect(has(try tagOf(p), ": 16 commands Tab complete · ⏎ run · C-r history · esc")); // The rows: the tag's words in its order, `in the tag`, the last ruled // off; then the commands, each what it takes. try std.testing.expect(has(try bandRow(p, a, 0), "▶ Save ")); @@ -700,7 +714,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, try std.testing.expect(has(first[7].text, "theme") and has(first[7].text, "<theme>")); // Typing filters both, by the scorer, in their order. typeKeys(p, "th"); - try std.testing.expect(has(try tagOf(p), " : th = Exec th 1 command ")); + try std.testing.expect(has(try tagOf(p), ": th = Exec th 1 command ")); try std.testing.expect(has(try bandRow(p, a, 0), "▶ theme ")); // Tab: the command, a space after it; the field's plain word is its // pardes word once it is whole. @@ -708,7 +722,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, try std.testing.expectEqualStrings("theme ", s.field.text()); typeKeys(p, "acme"); const worn = colors.themes[p.theme_preview.?].name; - try std.testing.expect(has(try tagOf(p), try std.fmt.allocPrint(a, " : theme acme = Exec Theme {s} ", .{worn}))); + try std.testing.expect(has(try tagOf(p), try std.fmt.allocPrint(a, ": theme acme = Exec Theme {s} ", .{worn}))); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(TagState.Kind.rest, s.kind); try std.testing.expectEqualStrings(worn, p.theme().name); @@ -718,7 +732,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, // `:sort`: the selections piped through sort, as `|sort` does. while (p.nextEffect()) |_| {} typeKeys(p, "%:sort"); - try std.testing.expect(has(try tagOf(p), " : sort = Exec Edit |sort 1 command")); + try std.testing.expect(has(try tagOf(p), ": sort = Exec Edit |sort 1 command")); p.update(.{ .key = .{ .cp = Key.enter } }); var asked: ?u32 = null; while (p.nextEffect()) |effect| if (effect == .pipe) { @@ -727,7 +741,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, try std.testing.expectEqualStrings("sort", p.pipe.pipeRequest(asked orelse return error.MissingPipeEffect).?.command); // `:goto 2`: Look `:2`. typeKeys(p, ":goto 2"); - try std.testing.expect(has(try tagOf(p), " : goto 2 = Look :2 <line> ")); + try std.testing.expect(has(try tagOf(p), ": goto 2 = Look :2 <line> ")); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); // S-Tab: the last row; Tab after it, round to the first. A tag word @@ -819,9 +833,9 @@ test ":theme lists the themes and the whole window wears the current one, none d try std.testing.expectEqual(Rows.values, s.prompt.command.rows); try std.testing.expectEqual(@as(u16, 2), s.nhits); try std.testing.expect(has(try bandRow(p, a, 0), "▶ on now")); - try std.testing.expect(has(try tagOf(p), " : set Wrap = Exec Wrap 2 values ")); + try std.testing.expect(has(try tagOf(p), ": set Wrap = Exec Wrap 2 values ")); typeKeys(p, "of"); - try std.testing.expect(has(try tagOf(p), " : set Wrap of = Exec Wrap of 1 value ")); + try std.testing.expect(has(try tagOf(p), ": set Wrap of = Exec Wrap of 1 value ")); p.update(.{ .key = .{ .cp = Key.tab } }); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(!p.settings.wrap); @@ -843,7 +857,7 @@ test ":theme lists the themes and the whole window wears the current one, none d // :open: the line goes to Find, the rest of it the query, the keys // after it typed into Find's field. typeKeys(p, ":o"); - try std.testing.expect(has(try tagOf(p), " : o = Exec Find ")); + try std.testing.expect(has(try tagOf(p), ": o = Exec Find ")); typeKeys(p, " te"); try std.testing.expectEqual(TagState.Kind.pick, s.kind); try std.testing.expectEqual(pardes.Pick.Source.find, pardes.Pick.live(p).?.source); @@ -885,7 +899,7 @@ test ": has a history of its own: Up and Down bring back the lines Enter ran, ne typeKeys(p, ":"); p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("goto 2", s.field.text()); - try std.testing.expect(has(try tagOf(p), " : goto 2 = Look :2 <line> ")); + try std.testing.expect(has(try tagOf(p), ": goto 2 = Look :2 <line> ")); p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("set Wrap off", s.field.text()); try std.testing.expectEqual(Rows.values, s.prompt.command.rows); diff --git a/core/Core.zig b/core/Core.zig index 2c04cc64..5b6eba75 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2010,7 +2010,10 @@ pub fn sync(p: *Pardes) void { var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); const wa = wrap_fba.allocator(); const prefix: ?[]const u8 = tagline.prefixIn(p, wa, pane) catch null; - const lead = if (prefix) |made| made.len else pane.tag_lead; + // The tag's cursor and selections are offsets in its text at rest: + // a head shown over its own text moves none of them (UX D1). + const at_rest = if (pardes.Trail.outOfRest(p, pane)) tagline.restPrefix(p, wa, pane) catch null else prefix; + const lead = if (at_rest) |made| made.len else pane.tag_lead; if (lead != pane.tag_lead) { const t = &pane.tag; const old: i32 = @intCast(pane.tag_lead); @@ -2059,7 +2062,8 @@ pub fn sync(p: *Pardes) void { } else pane.tag_top = 0; // The band unrolled under the tag's text pushes the body down: // the tag's rows, as everything under them counts them. - pane.band_rows = if (pane.collapsed) 0 else @min(pardes.Band.takes(p, id), r.h -| pane.tag_rows); + // A terminal's or a kind's is drawn over its body instead (Band.over). + pane.band_rows = if (pane.collapsed or pardes.Band.over(pane)) 0 else @min(pardes.Band.takes(p, id), r.h -| pane.tag_rows -| pardes.Band.body_rows); pane.tag_rows += pane.band_rows; const cols = @max(1, r.w -| config.GUTTER); // The tag steals the top rows. The pty is what the pane shows diff --git a/core/Messages.zig b/core/Messages.zig index 6231e3d1..2cb196f6 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -732,6 +732,10 @@ pub fn noticeRow(p: *const Pardes, width: usize, out: *[notice_row_cap]u8) Row { 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 {}; + // The keys of a root whose level waits for its delay, at once (UX D12: + // vim's showcmd; the tag stays at rest until the level shows). + var keys: [64]u8 = undefined; + if (pardes.TagState.pending(p, p.view(), &keys)) |typed| r.print("{s}{s}", .{ if (r.end > 0) " " else "", typed }) catch {}; const tail = r.buffered(); // The left cut to make room for the right, a blank between them. const tail_w = panes.File.displayWidth(tab, tail); @@ -1332,6 +1336,34 @@ test "a burst of the same message is one message, counted, and moves nothing on try std.testing.expect(std.mem.endsWith(u8, row.text[0..row.left], "3 files \u{d7}10")); } +test "a question never adds a row to its tag: at every width the asking tag takes the rows the tag takes at rest, its answers always shown, the question cut before them" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + const id = p.view().active; + // A short name: the tag's rows have room for it and the answers. + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/t/c.zig"); + const f = &pane.file.?; + f.saved_hash = panes.File.fileHash(f); + for ([_]u21{ 'G', 'o', 'f', Key.escape }) |cp| p.update(.{ .key = .{ .cp = cp } }); + // The tag's own words cut to one, so the question is the longer. + pane.tag.own = try p.gpa.dupe(u8, " Put"); + p.update(.{ .file_changed = .{ .pane = .at(id), .bytes = "zero\n" } }); + _ = p.messages.waiting(pane) orelse return error.NotAsked; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + for (56..161) |cols| { + p.update(.{ .resize = .{ .cols = @intCast(cols), .rows = 30 } }); + p.sync(); + const rest = pardes.layout.wantedTagRows(p, id); + try std.testing.expect(pane.tag_rows <= @max(rest, 1)); + const shown = try pardes.tagline.tagText(p, frame.allocator(), pane); + for ([_][]const u8{ "r Reload", "k Keep mine", "d Diff" }) |word| try std.testing.expect(std.mem.indexOf(u8, shown, word) != null); + _ = frame.reset(.retain_capacity); + } +} + 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(); @@ -1368,9 +1400,11 @@ test "a file changed on disk under unsaved text asks in its tag and never takes try std.testing.expect(std.mem.indexOf(u8, prefix, "changed on disk") == null); // The tag's own text is no one's to edit meanwhile. try std.testing.expect((try pardes.TagState.head(p, frame.allocator(), pane)) != null); - // Out of rest (SPC: its key cell at once) the level's head, not the - // question; at rest again, the question. + // Out of rest (SPC's level shown) the level's head, not the question; + // at rest again, the question. p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; try std.testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagText(p, frame.allocator(), pane), "changed on disk") == null); p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagText(p, frame.allocator(), pane), "changed on disk") != null); diff --git a/core/Pick.zig b/core/Pick.zig index 41a62201..c5b639cd 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -270,10 +270,9 @@ fn unroll(p: *Pardes, pk: *Pick, query: []const u8) void { s.rows = .{ .at = @intCast(@min(pk.at, std.math.maxInt(u16))) }; pk.unrolled = true; pk.shown = no_row; - // The whole window previews a theme: no pane dimmed (SPEC §3.6). + // The whole window previews a theme. if (pk.source == .themes) s.previewing = .full; if (p.paneBySerial(pk.landing)) |id| { - s.previewing.set(id); const pane = p.panes[id].?; pk.aside_row = pane.body.cur_row; pk.aside_col = pane.body.cur_col; @@ -606,78 +605,80 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { const pk = live(p) orelse return null; const s = &p.view().tag_state; - var text: std.ArrayList(u8) = .empty; - var spans: std.ArrayList(TagState.Span) = .empty; - try text.append(arena, ' '); - var cells: [4][2]u32 = undefined; - var ncells: usize = 0; + var b = try TagState.Builder.of(p, arena, pane); + const text = &b.text; // SPC's cell, then its keys': `␣ f`. - if (pk.keys.len > 0) for (0..pk.keys.len + 1) |k| { - const name = if (k == 0) "␣" else bindings.keyName(pk.keys.slice()[k - 1]); - cells[ncells] = .{ @intCast(text.items.len), @intCast(text.items.len + name.len) }; - ncells += 1; - try text.appendSlice(arena, name); - try text.append(arena, ' '); - }; + if (pk.keys.len > 0) try b.keyCells(.leader, pk.keys.slice()); try text.appendSlice(arena, pk.word.slice()); try text.append(arena, ' '); for (pk.refined[0..pk.nrefined]) |r| try text.print(arena, "{s} | ", .{r.query.slice()}); if (pk.spine == .edit) try text.print(arena, "{s} | Edit ", .{pk.query.slice()}); const caret: u32 = @intCast(text.items.len + s.field.caret); try text.appendSlice(arena, s.field.text()); + // The count at the row's right end (UX D3): `cursor/matches`, `0` with + // none, ` •M` marked. + const counted = text.items.len; const n = count(p, pk); - try text.print(arena, " {d}/{d}", .{ if (n == 0) 0 else s.rows.at + 1, n }); + if (n == 0) try text.append(arena, '0') else try text.print(arena, "{d}/{d}", .{ s.rows.at + 1, n }); if (pk.marks.items.len > 0) try text.print(arena, " •{d}", .{pk.marks.items.len}); if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); // Themes and actions have no acts and no other sources: no second row. if (pk.source == .themes or pk.source == .actions) { - const hint: u32 = @intCast(text.items.len); - try text.appendSlice(arena, if (pk.source == .themes) " ⏎ wear it · esc" else " ⏎ or 1-9 apply it · esc"); - // The key cells first: the band's spines hang from them. - for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); - try spans.append(arena, .{ .lo = hint, .hi = @intCast(text.items.len), .style = .dim }); - return .{ .text = text.items, .spans = spans.items, .cells = @intCast(ncells), .caret = caret }; + const hints = text.items.len; + try b.styled(if (pk.source == .themes) "⏎ wear it · esc" else "⏎ or 1-9 apply it · esc", .dim); + try b.right(counted, hints); + var h = b.done(); + h.caret = caret; + return h; } - // The second row: the spines go on through it from the key cells, - // then the other sources' words, each switching to it. - try text.append(arena, '\n'); - const prefix = try pardes.tagline.prefixIn(p, arena, pane); - const width = pardes.tagline.tagWidth(p, panes.paneIdOf(p, pane) orelse 0); - const lead = File.displayWidth(p.settings.tab, prefix) % @max(1, width); - var col: usize = 0; - for (cells[0..ncells]) |c| { - const at = lead + File.displayWidth(p.settings.tab, text.items[0..c[0]]); - while (col < at) : (col += 1) try text.append(arena, ' '); - try spans.append(arena, .{ .lo = @intCast(text.items.len), .hi = @intCast(text.items.len + "│".len), .style = .rule }); - try text.appendSlice(arena, "│"); - col += 1; - } - const nspines: u8 = @intCast(spans.items.len); - try text.appendSlice(arena, if (ncells == 0) "" else " "); - for (acts) |act| if (offered(pk, act)) try text.print(arena, "{s} ", .{act.word}); - // The other sources' words, and the `|` before them, dim (UX D4). - const sources: u32 = @intCast(text.items.len); - try text.appendSlice(arena, "|"); - var first = false; - inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != .actions) { - if (src != pk.source) { - if (!first) try text.append(arena, ' '); - first = false; - try text.appendSlice(arena, words.get(src).word); - } + try b.right(counted, text.items.len); + // The second row, one row (UX D2): under the spines from the key + // cells, from two cells right of the last, the acts, `|`, the other + // sources' words, each switching to it, the `|` and the sources dim + // (UX D4); what does not fit is dropped from the right (the sources + // first) and the row ends in a dim `…` (M-l and M-h reach them all). + try b.row(if (b.ncols > 0) b.cols[b.ncols - 1] + 2 else 0); + var offer: [acts.len + 1 + std.enums.values(Source).len][]const u8 = undefined; + var nw: usize = 0; + for (acts) |act| if (offered(pk, act)) { + offer[nw] = act.word; + nw += 1; }; - try spans.append(arena, .{ .lo = sources, .hi = @intCast(text.items.len), .style = .dim }); - for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); - return .{ .text = text.items, .spans = spans.items, .cells = nspines, .caret = caret }; + const nacts = nw; + offer[nw] = "|"; + nw += 1; + inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != .actions and src != pk.source) { + offer[nw] = words.get(src).word; + nw += 1; + }; + for (offer[0..nw], 0..) |word, i| { + const gap: usize = @intFromBool(i > 0); + const rest: usize = if (i + 1 < nw) 2 else 0; + if (b.col() + gap + File.displayWidth(p.settings.tab, word) + rest > b.width) { + try b.put(if (gap > 0) " " else ""); + try b.styled("…", .dim); + break; + } + try b.put(if (gap > 0) " " else ""); + if (i >= nacts) try b.styled(word, .dim) else try b.put(word); + } + var h = b.done(); + h.caret = caret; + return h; } -/// The band's rows: as many as the list will show, eleven at most, from -/// what the source has (not what the query keeps: typing never moves the -/// body). +/// The band's rows (UX D6): as many as the list will show, from what the +/// source has (not what the query keeps: typing never moves the body), +/// four at least, eleven at most, and 40 % of the host's column at most +/// (its panes' rows, which a borrow moves between them, never changes). pub fn bandRows(p: *const Pardes) u16 { const pk = live(p) orelse return 0; const have = if (!fuzzy(pk)) Band.Cursor.shown else pk.names.cands.items.len; - const rows: usize = @max(1, @min(Band.Cursor.shown, have)); + const v = p.view(); + const at = @import("layout.zig").findPane(p, TagState.hostOf(p) orelse return 0) orelse return 0; + var column: usize = 0; + for (v.cols[at.col].panes[0..v.cols[at.col].n]) |id| column += v.rects[id].h; + const rows: usize = @max(1, @min(Band.Cursor.shown, @max(4, have), column * 2 / 5)); // Its second step's rows too: the sources, the acts and what they act on. return @intCast(switch (pk.spine) { .sources => @max(rows, spine_rows.len), @@ -1751,7 +1752,10 @@ test "a picker from a held pane unrolls in a reader made for it: its rows in the defer frame.deinit(); const a = frame.allocator(); const tag = try tagOf(p, a, reader); - try std.testing.expect(std.mem.indexOf(u8, tag, " ␣ f Find pick 1/1\n") != null); + // Out of rest no path (UX D1): not the previewed file's, not Keep; + // the head starts the tag's text. + const first = tag[0..std.mem.indexOfScalar(u8, tag, '\n').?]; + try std.testing.expect(std.mem.startsWith(u8, first, "␣ f Find pick ") and std.mem.endsWith(u8, first, " 1/1")); try std.testing.expect(std.mem.indexOf(u8, tag, "│ Walk Look Split Newcol Grep Edit Copy Keep Drop | Grep Recent Panes Symbols All Jumplist") != null); // The body previews the row's file, after a frame of its own. p.update(.tick); @@ -1768,6 +1772,64 @@ test "a picker from a held pane unrolls in a reader made for it: its rows in the try std.testing.expectEqualStrings("pick", pk.query.slice()); } +test "a picker's act row is one row under the spines, two cells right of the last: what does not fit dropped from the right, the sources first, a dim `…` ending it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + p.stream.engine = .walk; + open(p, asker, .find, "", "", ""); + const reader = p.paneBySerial(live(p).?.landing).?; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const tag = try tagOf(p, a, reader); + var rows = std.mem.splitScalar(u8, tag, '\n'); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "␣ f Find ")); + const acts_row = rows.next().?; + try std.testing.expect(rows.next() == null); + try std.testing.expect(std.mem.startsWith(u8, acts_row, "│ │ Walk Look ")); + try std.testing.expect(std.mem.endsWith(u8, acts_row, " …")); + try std.testing.expect(File.displayWidth(p.settings.tab, acts_row) <= pardes.tagline.tagWidth(p, reader)); + const h = (try head(p, a, p.panes[reader].?)).?; + try std.testing.expect(h.spans[h.spans.len - 1].style == .dim); +} + +test "a picker's band is sized from its list at open, 4 rows at least, and typing never moves it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + // In the test's directory: its two files listed. + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ std.Io.Dir.path.dirname(a_path).?, "held.txt" }); + p.stream.engine = .walk; + open(p, asker, .find, "", "", ""); + settle(p); + const reader = p.paneBySerial(live(p).?.landing).?; + p.sync(); + const rows = p.panes[reader].?.band_rows; + // One or two files listed: four rows, ground under them. + try std.testing.expectEqual(@as(u16, 4), rows); + for ("zzq") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + p.sync(); + try std.testing.expectEqual(@as(usize, 0), count(p, live(p).?)); + try std.testing.expectEqual(rows, p.panes[reader].?.band_rows); +} + test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and Backspace undoes it, M-h switches source keeping the query" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); defer p.deinit(); @@ -1793,7 +1855,8 @@ test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and try std.testing.expectEqual(@as(u16, 2), s.rows.at); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); - try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs 3/4 •1\n") != null); + const refs = try tagOf(p, frame.allocator(), id); + try std.testing.expect(std.mem.indexOf(u8, refs, " Refs ") != null and std.mem.indexOf(u8, refs, " 3/4 •1\n") != null); p.update(.{ .key = .{ .cp = 'a', .alt = true } }); try std.testing.expectEqual(@as(usize, 4), pk.marks.items.len); p.update(.{ .key = .{ .cp = 'a', .alt = true } }); @@ -1811,7 +1874,8 @@ test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and settle(p); try std.testing.expectEqual(@as(usize, 1), count(p, pk)); _ = frame.reset(.retain_capacity); - try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs alpha | w 1/1\n") != null); + const refined = try tagOf(p, frame.allocator(), id); + try std.testing.expect(std.mem.indexOf(u8, refined, " Refs alpha | w ") != null and std.mem.indexOf(u8, refined, " 1/1\n") != null); // Backspace twice: the `w` goes, then the refinement, its query back. p.update(.{ .key = .{ .cp = Key.backspace } }); p.update(.{ .key = .{ .cp = Key.backspace } }); diff --git a/core/Prompt.zig b/core/Prompt.zig index 4db7e424..9dfe9ec2 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -809,10 +809,11 @@ pub fn execWord(p: *Pardes, id: usize) void { /// The span of pane `pane`'s tag its search word takes while its places no /// longer light it (the first Esc cleared them): drawn dimmed (draw.zig). pub fn unlit(p: *const Pardes, pane: *Pane) ?[2]usize { + if (Trail.outOfRest(p, pane)) return null; const t = &pane.trail; - const whole = Trail.folded(p, pane); + const whole = pardes.tagline.folded(p, pane); for (t.steps[0..t.cur], 0..) |*step, i| if (step.* == .search) { - if (lights(pane, &step.search) or t.cur - i > whole) return null; + if (lights(pane, &step.search) or t.isFolded(i, whole)) return null; return t.spanOf(i, whole); }; return null; @@ -1059,15 +1060,15 @@ fn strike(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, line: i32, lo_: } } -/// The head of the prompt's tag: ` s self▌ = Exec Edit x/self/ 4 +/// The head of the prompt's tag: `s self▌ = Exec Edit x/self/ 4 /// selections in 6 lines ⏎ select · M-⏎ as a list · C-r history · esc`. pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { const s = &p.view().tag_state; const pr = &s.prompt; - var text: std.ArrayList(u8) = .empty; - var spans: std.ArrayList(TagState.Span) = .empty; - try text.append(arena, ' '); - try styled(arena, &text, &spans, info.get(pr.op).cell, .key); + var b = try TagState.Builder.of(p, arena, pane); + const text = &b.text; + const spans = &b.spans; + try styled(arena, text, spans, info.get(pr.op).cell, .key); try text.append(arena, ' '); const caret: u32 = @intCast(text.items.len + s.field.caret); const line = s.field.text(); @@ -1077,23 +1078,29 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { var w: std.Io.Writer = .fixed(&buf); const said = if (pr.op == .command) Command.plainWord(p, &w, line) else plainWord(&w, pr.op, line) catch null; if (said) |verb| { - try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); + try styled(arena, text, spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); try text.print(arena, " {s}", .{w.buffered()}); } + } + // The count and the hints at the row's right end (UX D3). + const at = text.items.len; + if (line.len > 0) { // A pattern that is no regex: why, in the err ink, in place of the - // hints (UX D14), what is shown meanwhile dim. + // hints (UX D14), what is shown meanwhile dim; in the count's place, + // which never drops. if (pr.bad) { - try text.appendSlice(arena, " "); - try styled(arena, &text, &spans, regexMessage(line), .err); - try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1}), .dim); + try styled(arena, text, spans, regexMessage(line), .err); + try styled(arena, text, spans, try std.fmt.allocPrint(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1}), .dim); + try b.right(at, text.items.len); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } else if (behavior(pr.op) != null) { - try pipeCount(p, arena, &text, &spans, pane, pr, line); - } else try count(arena, &text, pane, pr); + try pipeCount(p, arena, text, spans, pane, pr, line); + } else try count(arena, text, pane, pr); } - if (pr.op == .command) try Command.count(p, arena, &text); - try text.appendSlice(arena, " "); - try styled(arena, &text, &spans, info.get(pr.op).hint, .dim); + if (pr.op == .command) try Command.count(p, arena, text); + const hints = text.items.len; + try styled(arena, text, spans, info.get(pr.op).hint, .dim); + try b.right(at, hints); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } @@ -1121,7 +1128,7 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c // Round the end from where it began. const c = at orelse return; const origin = set.origin(pane); - if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " · wrapped"); + if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") @@ -1261,8 +1268,22 @@ fn tagOf(p: *Pardes) ![]const u8 { return tagline.tagText(p, p.scratch.allocator(), p.panes[p.view().active].?); } +/// Whether `needle` is in `hay`, a run of two spaces or more in either read +/// as two: a right block's padding is its row's (UX D3). fn has(hay: []const u8, needle: []const u8) bool { - return std.mem.indexOf(u8, hay, needle) != null; + var in: [8192]u8 = undefined; + var of: [1024]u8 = undefined; + return std.mem.indexOf(u8, squeezed(hay, &in), squeezed(needle, &of)) != null; +} + +fn squeezed(s: []const u8, buf: []u8) []const u8 { + var n: usize = 0; + for (s, 0..) |c, i| { + if (c == ' ' and i >= 2 and s[i - 1] == ' ' and s[i - 2] == ' ') continue; + buf[n] = c; + n += 1; + } + return buf[0..n]; } test "s asks in the tag's head: its key cell, the field, the plain word and the count, the selections live in the body; no match is 0, a pattern that is no regex keeps the last result dimmed and says why, Esc puts them back" { @@ -1275,9 +1296,9 @@ test "s asks in the tag's head: its key cell, the field, the plain word and the try std.testing.expectEqual(TagState.Kind.prompt, p.view().tag_state.kind); // No chip on the notice band: the prompt is the tag's. try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); - try std.testing.expect(std.mem.endsWith(u8, try tagOf(p), " s ⏎ select · M-⏎ as a list · C-r history · esc")); + try std.testing.expect(has(try tagOf(p), "s ⏎ select · M-⏎ as a list · C-r history · esc")); typeKeys(p, "self"); - try std.testing.expect(has(try tagOf(p), " s self = Exec Edit x/self/ 2 selections in 3 lines ⏎ select")); + try std.testing.expect(has(try tagOf(p), "s self = Exec Edit x/self/ 2 selections in 3 lines ⏎ select")); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(selecting(p, pane).?.dim == false); // No match: 0, and the selection it began with. @@ -1317,10 +1338,10 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba typeKeys(p, "K"); p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("self..", p.view().tag_state.field.text()); - try std.testing.expect(has(try tagOf(p), " K self.. = Exec Edit g/self../ keeps 2 of 2")); + try std.testing.expect(has(try tagOf(p), "K self.. = Exec Edit g/self../ keeps 2 of 2")); p.update(.{ .key = .{ .cp = Key.down } }); typeKeys(p, "b"); - try std.testing.expect(has(try tagOf(p), " K b = Exec Edit g/b/ keeps 1 of 2 ⏎ keep")); + try std.testing.expect(has(try tagOf(p), "K b = Exec Edit g/b/ keeps 1 of 2 ⏎ keep")); // Ctrl-r: the history under the tag, a row a line. p.update(.{ .key = .{ .cp = 'r', .ctrl = true } }); try std.testing.expectEqual(@as(u16, 1), Band.takes(p, p.view().active)); @@ -1332,7 +1353,7 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba typeKeys(p, "|"); try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); + try std.testing.expect(has(try tagOf(p), "| tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqualStrings("tr a-z A-Z", p.registers.value('|', 0).?); var asked: ?u32 = null; @@ -1369,7 +1390,7 @@ test "/ searches the body as it is typed: the current place selected, `▸` in t pane.body.cur_row = 2; pane.body.cur_col = 6; typeKeys(p, "/dirty"); - try std.testing.expect(has(try tagOf(p), " / dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); + try std.testing.expect(has(try tagOf(p), "/ dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); try std.testing.expectEqual(@as(?usize, 2), pane.places.current); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 0)); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 1)); @@ -1426,7 +1447,7 @@ test "? searches back from the caret; no match is 0; a pattern that is no regex pane.body.cur_col = 6; const was = pane.body.primaryRange(pane.file.?.content, 0); typeKeys(p, "?dirty"); - try std.testing.expect(has(try tagOf(p), " ? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); + try std.testing.expect(has(try tagOf(p), "? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); typeKeys(p, "x"); try std.testing.expect(has(try tagOf(p), "= Look ?dirtyx? 0 ⏎ land")); try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); @@ -1514,7 +1535,7 @@ test "a pipe prompt runs a known filter live, a run a selection: the outputs gho p.update(.{ .key = .{ .cp = Key.enter } }); typeKeys(p, "|"); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); + try std.testing.expect(has(try tagOf(p), "| tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); const first = lastPipe(p) orelse return error.MissingLiveRun; const asked = p.pipe.pipeRequest(first).?; try std.testing.expectEqual(@as(usize, 2), asked.inputs.len); diff --git a/core/TagState.zig b/core/TagState.zig index 7a34ed77..5ae2d0b6 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -26,6 +26,7 @@ const Band = @import("Band.zig"); const Names = @import("Names.zig"); const Prompt = @import("Prompt.zig"); const Ask = @import("lsp/Ask.zig"); +const Messages = @import("Messages.zig"); /// The tag's states besides a root's level (which `kind` rest with a root /// current is): `help`, a field over every leaf (SPEC §4.5); `describe`, @@ -79,9 +80,9 @@ miss: u8 = 0, /// The leaf each group ran last, by its key (bindings.groups): the cursor /// starts there and the rolled level underlines it (SPEC §4.3). used: [bindings.ngroups]u8 = @splat(0), -/// The panes showing a preview of what the state would do (a picker's -/// landing pane, a pane an action draws ghosts in), by slot: not dimmed -/// while the others are (SPEC §3.6). Set by the state that previews. +/// `.full` while the whole window previews a theme (a themes picker's +/// row, `:theme `'s): its own again at rest. No pane dims out of rest +/// (UX D9), so no pane's own bit is read. previewing: std.StaticBitSet(pardes.MAX_PANES) = .empty, /// describe-key (SPEC §4.5), in the tag of pane `id`: the next keys are @@ -192,8 +193,7 @@ pub fn hostOf(p: *const Pardes) ?usize { if (v.tag_state.kind != .rest) return for (p.panes, 0..) |slot, i| { if (slot) |pane| if (pane.serial == v.tag_state.host) break i; } else null; - const cur = current(p, v) orelse return null; - if (!bindings.info.get(cur.root).at_once and !shown(p, v)) return null; + if (current(p, v) == null or !shown(p, v)) return null; return if (p.panes[v.active] != null) v.active else null; } @@ -296,8 +296,23 @@ fn asking(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { const i = p.messages.waiting(pane) orelse return null; const said = &p.messages.live[i]; var b: Builder = .{ .arena = arena }; + // In the rows the tag takes at rest (UX D13: a question never adds a + // row): the hint goes first, then the question is cut, 12 cells at + // least, with `…` (the whole of it is on the notice row); its answers + // always show. + const tab = p.settings.tab; + const id = panes.paneIdOf(p, pane) orelse return null; + const room = @as(usize, @import("layout.zig").wantedTagRows(p, id)) * tagline.tagWidth(p, id) -| panes.File.displayWidth(tab, try tagline.restPrefix(p, arena, pane)); + const hint = if (p.view().answering == pane.serial) "· Esc later" else "· SPC e to answer"; + var answers: usize = 2; // ` ▸`, then each ` k word ` + for (said.asks.answers()) |a| answers += 3 + panes.File.displayWidth(tab, a.word) + 1; + const question = said.words(); + const wanted = 1 + panes.File.displayWidth(tab, question) + answers; try b.put(" "); - try b.put(said.words()); + if (wanted <= room) try b.put(question) else { + try b.put(question[0..panes.File.byteAtDisplay(tab, question, @max(12, room -| (1 + answers)) - 1)]); + try b.put("…"); + } try b.put(" "); try b.styled("▸", .key); for (said.asks.answers()) |a| { @@ -307,10 +322,19 @@ fn asking(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { try b.put(a.word); try b.put(" "); } - try b.styled(if (p.view().answering == pane.serial) "· Esc later" else "· SPC e to answer", .dim); + if (wanted + panes.File.displayWidth(tab, hint) <= room) try b.styled(hint, .dim); return b.done(); } +/// The keys of the root current in view `v` while its level waits for its +/// delay (UX D12: the tag is at rest until then), as the notice row's right +/// end echoes them at once: `SPC f`. +pub fn pending(p: *const Pardes, v: *const View, buf: []u8) ?[]const u8 { + if (v.tag_state.kind != .rest or shown(p, v)) return null; + const cur = current(p, v) orelse return null; + return bindings.chord(cur.root, cur.path, buf); +} + /// The last leaf run in the current level, as the line under it says it /// (Messages' leader band): `k Hover: its summary (last used here)`, while /// the level is shown rolled. @@ -457,31 +481,139 @@ pub const Head = struct { caret: ?u32 = null, }; -const Builder = struct { +/// A head as it is built: its text and spans, laid out against the tag's +/// rows (UX D2, D3). +pub const Builder = struct { arena: std.mem.Allocator, text: std.ArrayList(u8) = .empty, spans: std.ArrayList(Span) = .empty, cells: u8 = 0, + /// The cells of a row of the tag, and those before the head on its + /// first row (the owner's and the newest step's words). + width: usize = 0, + lead: usize = 0, + tab: u16 = 8, + /// Each key cell's column, which a spine hangs under (UX D5). + cols: [Band.max_spines]u16 = undefined, + ncols: u8 = 0, - fn put(b: *Builder, s: []const u8) !void { + /// A head for pane `pane`'s tag, out of rest in the live view. + pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !Builder { + const prefix = try tagline.prefixIn(p, arena, pane); + const id = panes.paneIdOf(p, pane) orelse 0; + return .{ .arena = arena, .width = tagline.tagWidth(p, id), .lead = panes.File.displayWidth(p.settings.tab, prefix), .tab = p.settings.tab }; + } + + pub fn put(b: *Builder, s: []const u8) !void { try b.text.appendSlice(b.arena, s); } - fn styled(b: *Builder, s: []const u8, style: Style) !void { + pub fn styled(b: *Builder, s: []const u8, style: Style) !void { const lo: u32 = @intCast(b.text.items.len); try b.text.appendSlice(b.arena, s); try b.spans.append(b.arena, .{ .lo = lo, .hi = @intCast(b.text.items.len), .style = style }); } + + /// The cells the row being built takes so far, the prefix's before the + /// first row's. + pub fn col(b: *const Builder) usize { + const items = b.text.items; + const nl = std.mem.lastIndexOfScalar(u8, items, '\n') orelse return b.lead + panes.File.displayWidth(b.tab, items); + return panes.File.displayWidth(b.tab, items[nl + 1 ..]); + } + + /// The right block (UX D3): what was put from byte `count` on (the + /// count) and from byte `hints` on (the hints) moved to the right end of + /// their row, its last cell the row's last, two spaces between them and + /// at least two before; so it never moves while the field grows toward + /// it. Hints that do not fit go whole, first; the count never does (a + /// head with no room for it runs past its row). + pub fn right(b: *Builder, count: usize, hints: usize) !void { + var h = hints; + b.cut(h, h + spaces(b.text.items[h..])); + const lead_c = spaces(b.text.items[count..h]); + b.cut(count, count + lead_c); + h -= lead_c; + while (h > count and b.text.items[h - 1] == ' ') : (h -= 1) b.cut(h - 1, h); + const items = b.text.items; + const start = if (std.mem.lastIndexOfScalar(u8, items[0..count], '\n')) |nl| nl + 1 else 0; + const left = panes.File.displayWidth(b.tab, items[start..count]) + if (start == 0) b.lead else 0; + var trail: usize = 0; + while (trail < count - start and items[count - 1 - trail] == ' ') trail += 1; + const cw = panes.File.displayWidth(b.tab, items[count..h]); + var hw = panes.File.displayWidth(b.tab, items[h..]); + const between: usize = if (cw > 0) 2 else 0; + if (hw > 0 and left + (2 -| trail) + cw + between + hw > b.width) { + b.text.shrinkRetainingCapacity(h); + var kept: usize = 0; + for (b.spans.items) |span| if (span.lo < h) { + b.spans.items[kept] = span; + kept += 1; + }; + b.spans.shrinkRetainingCapacity(kept); + hw = 0; + } + if (cw + hw == 0) return; + if (cw > 0 and hw > 0) try b.insert(h, 2); + const used = left + cw + (if (cw > 0 and hw > 0) between else 0) + hw; + try b.insert(count, if (used + (2 -| trail) <= b.width) b.width - used else 2 -| trail); + } + + /// `n` spaces put in at byte `at`, the spans after it moved with them. + fn insert(b: *Builder, at: usize, n: usize) !void { + const blank = try b.arena.alloc(u8, n); + @memset(blank, ' '); + try b.text.insertSlice(b.arena, at, blank); + for (b.spans.items) |*span| if (span.lo >= at) { + span.lo += @intCast(n); + span.hi += @intCast(n); + }; + } + + /// Bytes `lo` to `hi` taken out (blanks: no span starts in them). + fn cut(b: *Builder, lo: usize, hi: usize) void { + if (hi <= lo) return; + b.text.replaceRangeAssumeCapacity(lo, hi - lo, &.{}); + for (b.spans.items) |*span| if (span.lo >= hi) { + span.lo -= @intCast(hi - lo); + span.hi -= @intCast(hi - lo); + }; + } + + fn spaces(s: []const u8) usize { + return s.len - std.mem.trimStart(u8, s, " ").len; + } /// The keys typed as 2-cell key cells: the root's, then each key. - fn keyCells(b: *Builder, root: Root, path: []const u8) !void { - try b.styled(bindings.info.get(root).cell, .key); + pub fn keyCells(b: *Builder, root: Root, path: []const u8) !void { + try b.keyCell(bindings.info.get(root).cell); + for (path) |k| try b.keyCell(if (k == ' ') "␣" else bindings.keyName(k)); + b.cells = @intCast(b.spans.items.len); + } + + /// One key cell, its column kept for its spine, and the blank after it. + pub fn keyCell(b: *Builder, name: []const u8) !void { + if (b.ncols < b.cols.len) { + b.cols[b.ncols] = @intCast(b.col()); + b.ncols += 1; + } + try b.styled(name, .key); try b.put(" "); - for (path) |k| { - try b.styled(if (k == ' ') "␣" else bindings.keyName(k), .key); - try b.put(" "); + } + + /// A row more of the head (UX D2): the spines on through it under each + /// key cell left of `indent` (UX D5), then blanks up to `indent`, a + /// hanging indent where the row's words start. + pub fn row(b: *Builder, indent: usize) !void { + try b.text.append(b.arena, '\n'); + var at: usize = 0; + for (b.cols[0..b.ncols]) |c| { + if (c >= indent) break; + try b.text.appendNTimes(b.arena, ' ', c - at); + try b.styled("│", .rule); + at = c + 1; } - b.cells = @intCast(b.spans.items.len); + try b.text.appendNTimes(b.arena, ' ', indent -| at); } - fn done(b: *const Builder) Head { + pub fn done(b: *const Builder) Head { return .{ .text = b.text.items, .spans = b.spans.items, .cells = b.cells }; } }; @@ -493,18 +625,21 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { if (p.panes[id] != pane) return asking(p, arena, pane); const v = p.view(); const s = &v.tag_state; - var b: Builder = .{ .arena = arena }; + var b = try Builder.of(p, arena, pane); switch (s.kind) { .rest => {}, - // `Help`, its scope's keys, the field, its count: - // ` Help ␣ l hov 2/17`. + // `Help`, its scope's keys, the field, its count at the row's end: + // `Help ␣ l hov 2/17` (out of rest the head starts the tag's + // text, after the owner's and the newest step's words: UX D1). .help => { - try b.put(" Help "); + try b.put("Help "); if (s.scope_root) |r| try b.keyCells(r, s.scope[0..s.scope_n]); var h = b.done(); h.caret = @intCast(b.text.items.len + s.field.caret); try b.put(s.field.text()); - try b.text.print(arena, " {d}/{d}", .{ s.nhits, s.ntotal }); + const count = b.text.items.len; + if (s.nhits == 0) try b.put("0") else try b.text.print(arena, "{d}/{d}", .{ s.rows.at + 1, s.nhits }); + try b.right(count, b.text.items.len); h.text = b.text.items; h.spans = b.spans.items; return h; @@ -512,7 +647,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { // `Keys`, then the keys replayed: `Keys g ▸ d Def`; struck, // `runs nothing`. .describe => { - try b.put(" Keys "); + try b.put("Keys "); if (s.missed) { var buf: [64]u8 = undefined; const keys = if (s.scope_root) |r| bindings.chord(r, s.scope[0..s.scope_n], &buf) else bindings.keyName(s.scope[0]); @@ -532,79 +667,90 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { try b.put(" "); try b.put(c.label); }; - try b.styled(" ⏎ run · esc", .dim); + const hints = b.text.items.len; + try b.styled("⏎ run · esc", .dim); + try b.right(hints, hints); } else try b.put("…"); } else { try b.put("…"); - try b.styled(" press a key or a sequence · esc", .dim); + const hints = b.text.items.len; + try b.styled("press a key or a sequence · esc", .dim); + try b.right(hints, hints); } return b.done(); }, .pick => return @import("Pick.zig").head(p, arena, pane), .prompt => return try Prompt.head(p, arena, pane), // `Rename`, the name under the caret struck, the new one typed: - // ` Rename best → top▌ ⏎ rename · esc` (SPEC §3.5). + // `Rename best → top▌ ⏎ rename · esc` (SPEC §3.5). .rename => { - try b.put(" Rename "); + try b.put("Rename "); try b.styled(Ask.renamedWord(pane) orelse "", .strike); try b.put(" → "); var h = b.done(); h.caret = @intCast(b.text.items.len + s.field.caret); try b.put(s.field.text()); - try b.styled(" ⏎ rename · esc", .dim); + const hints = b.text.items.len; + try b.styled("⏎ rename · esc", .dim); + try b.right(hints, hints); h.text = b.text.items; h.spans = b.spans.items; return h; }, } const cur = current(p, v) orelse return null; - try b.put(" "); try b.keyCells(cur.root, cur.path); - if (!shown(p, v)) { - try b.put("…"); - return b.done(); - } const node = bindings.nodeAt(cur.root, cur.path) orelse return b.done(); if (s.unrolled) { try b.put(if (cur.path.len == 0) bindings.info.get(cur.root).label else node.label); - try b.styled(" ↑ roll up · ? field · esc", .dim); + const hints = b.text.items.len; + try b.styled("↑ roll up · ? field · esc", .dim); + try b.right(hints, hints); return b.done(); } try b.styled("▸", .key); - // The rolled level wraps to three rows at most (SPEC §3.6): what does - // not fit is `…`, and `↓` unrolls it whole. A terminal's to the rows its - // own text takes: its pty is its body, and a which-key that took a row - // would resize the program under it at every SPC. Every cell is - // counted: the prefix, each child with its separator and a group's - // `…`, the ` …` after a child that is not the last, a struck key, the - // hint (left out, a terminal's tag took a row more at some widths). - const hint = " ↓ unroll · ? help"; - const more = " …"; - const prefix = try tagline.prefixIn(p, arena, pane); + // The rolled level breaks between its children only (UX D2), each row + // after the first from the first child's column, its spines on through + // it; three rows at most, the last ending `+N ↓` (N the children not + // shown, `↓` unrolls them) when they do not fit. A terminal's to the + // rows its own text takes: its pty is its body, and a which-key that + // took a row would resize the program under it at every SPC. const tab = p.settings.tab; const most: usize = if (pane.isTerminal()) @import("layout.zig").wantedTagRows(p, id) else 3; - const struck: usize = if (s.miss != 0) 1 + panes.File.displayWidth(tab, bindings.keyName(s.miss)) else 0; - const room = most * tagline.tagWidth(p, id) -| panes.File.displayWidth(tab, prefix) -| panes.File.displayWidth(tab, hint) -| struck; var children: [bindings.max_children]bindings.Child = undefined; - const level = bindings.level(p, cur.root, cur.path, &children); + var level = bindings.level(p, cur.root, cur.path, &children); const used = s.used[node.group]; + const indent = b.col() + 1; + // A struck key goes after the children: the last row keeps its cells. + const struck: usize = if (s.miss != 0) 1 + panes.File.displayWidth(tab, bindings.keyName(s.miss)) else 0; // The registers rolled (UX D11): the ones holding something, each its // value as a row shows it, a newline's `⏎` dim. const registers = cur.root == .register and cur.path.len == 0; - var width = panes.File.displayWidth(tab, b.text.items); - var put: usize = 0; + if (registers) { + var n: usize = 0; + for (level) |c| if (bindings.registerValue(p, c.key).len > 0) { + level[n] = c; + n += 1; + }; + level = level[0..n]; + } + var rows: usize = 1; for (level, 0..) |c, i| { - const value = if (registers) bindings.registerValue(p, c.key) else ""; - if (registers and value.len == 0) continue; - const label = if (registers) try bindings.registerShown(arena, tab, value) else c.label; + const label = if (registers) try bindings.registerShown(arena, tab, bindings.registerValue(p, c.key)) else c.label; const cut = c.group or (c.more and !registers); - width += @as(usize, if (put == 0) 1 else 2) + panes.File.displayWidth(tab, bindings.keyName(c.key)) + 1 + panes.File.displayWidth(tab, label) + @intFromBool(cut); - if (width + @as(usize, if (i + 1 < level.len) panes.File.displayWidth(tab, more) else 0) > room) { - try b.put(more); + const w = panes.File.displayWidth(tab, bindings.keyName(c.key)) + 1 + panes.File.displayWidth(tab, label) + @intFromBool(cut); + var gap: usize = if (i == 0) 1 else 2; + if (b.col() + gap + w + more(level.len - i - 1, rows == most) + (if (rows == most) struck else 0) > b.width and rows < most and b.col() > indent) { + try b.row(indent); + rows += 1; + gap = 0; + } + if (b.col() + gap + w + more(level.len - i - 1, rows == most) + (if (rows == most) struck else 0) > b.width) { + try b.put(if (gap == 0) "" else " "); + try b.styled(try std.fmt.allocPrint(arena, "+{d} ↓", .{level.len - i}), .dim); break; } - try b.put(if (put == 0) " " else " "); - put += 1; + try b.put(if (gap == 0) "" else if (gap == 1) " " else " "); const lo: u32 = @intCast(b.text.items.len); try b.styled(bindings.keyName(c.key), .key); try b.put(" "); @@ -618,18 +764,29 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { if (c.key == used) try b.spans.append(arena, .{ .lo = lo, .hi = hi, .style = .under }); } if (s.miss != 0) { - try b.put(" "); + if (b.col() + struck > b.width and rows < most) try b.row(indent) else try b.put(" "); try b.styled(bindings.keyName(s.miss), .strike); } - try b.styled(hint, .dim); + // Its hints on its last row, by the right block's rule (UX D3). + const hints = b.text.items.len; + try b.styled("↓ unroll · ? help", .dim); + try b.right(hints, hints); return b.done(); } +/// The cells `+N ↓` takes after a child on a rolled level's last row, with +/// `n` children after it. +fn more(n: usize, last: bool) usize { + if (n == 0 or !last) return 0; + // Two blanks, `+`, N, a blank, `↓`. + return 5 + std.fmt.count("{d}", .{n}); +} + fn testTag(p: *Pardes, arena: std.mem.Allocator) ![]const u8 { return tagline.tagText(p, arena, p.panes[p.view().active].?); } -test "SPC writes its key cell at once and its level 250 ms on; g changes nothing until 500 ms, and gg never" { +test "SPC changes nothing in the tag until its level shows 250 ms on, its keys on the notice row's right end at once; g nothing until 500 ms, and gg never" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer p.deinit(); @@ -641,9 +798,12 @@ test "SPC writes its key cell at once and its level 250 ms on; g changes nothing p.clock.stepped_ns = p.clock.now_ns; p.sync(); const rest = try a.dupe(u8, try testTag(p, a)); - // SPC: its cell at once, the own words gone under the head. + // SPC: the tag at rest until the level shows (UX D12), the keys typed + // at the notice row's right end at once. p.update(.{ .key = .{ .cp = ' ' } }); - try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " ␣ …")); + try std.testing.expectEqualStrings(rest, try testTag(p, a)); + var row: [Messages.notice_row_cap]u8 = undefined; + try std.testing.expect(std.mem.endsWith(u8, Messages.noticeRow(p, 100, &row).text, "SPC")); // Its time is the next advance's; the level waits 250 ms from it, and // the loop wakes for it then. try std.testing.expectEqual(asked, p.view().tag_state.due_ns); @@ -653,15 +813,15 @@ test "SPC writes its key cell at once and its level 250 ms on; g changes nothing const woken = p.nextWake().?; try std.testing.expect(woken >= due and woken < due + pardes.frame_ns); p.clock.now_ns = due - 1; - try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " ␣ …")); + try std.testing.expectEqualStrings(rest, try testTag(p, a)); p.clock.now_ns = due; const level = try testTag(p, a); - try std.testing.expect(std.mem.indexOf(u8, level, " ␣ ▸ b buffer…") != null); + try std.testing.expect(std.mem.indexOf(u8, level, "␣ ▸ b buffer…") != null); try std.testing.expect(std.mem.indexOf(u8, level, "l lsp…") != null); - try std.testing.expect(std.mem.endsWith(u8, level, "↓ unroll · ? help")); + try std.testing.expect(std.mem.count(u8, level, "\n") < 3); // Down a level: its keys, its children. p.update(.{ .key = .{ .cp = 'l' } }); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " ␣ l ▸ a CodeAction b Callstack") != null); + try std.testing.expect(std.mem.startsWith(u8, try testTag(p, a), "␣ l ▸ a CodeAction b Callstack")); // Esc: rest, the tag as it was. p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expectEqualStrings(rest, try testTag(p, a)); @@ -677,7 +837,7 @@ test "SPC writes its key cell at once and its level 250 ms on; g changes nothing p.clock.now_ns = g_due - 1; try std.testing.expectEqualStrings(rest, try testTag(p, a)); p.clock.now_ns = g_due; - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " g ▸ ") != null); + try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), "g ▸ ") != null); try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), "d Def") != null); p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expectEqualStrings(rest, try testTag(p, a)); @@ -695,6 +855,109 @@ test "SPC writes its key cell at once and its level 250 ms on; g changes nothing try std.testing.expect(p.view().tag_state.root == null); } +fn firstRow(tag: []const u8) []const u8 { + return tag[0 .. std.mem.indexOfScalar(u8, tag, '\n') orelse tag.len]; +} + +test "the right block: the count `cursor/matches` at the row's right end, fixed while the field grows; the hints after it, dim, dropped whole where the row has no room" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 30 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const tab = p.settings.tab; + const width = tagline.tagWidth(p, p.view().active); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = '?' } }); + const s = &p.view().tag_state; + var row = firstRow(try testTag(p, a)); + try std.testing.expect(std.mem.startsWith(u8, row, "Help ")); + try std.testing.expectEqual(width, panes.File.displayWidth(tab, row)); + try std.testing.expect(std.mem.endsWith(u8, row, try std.fmt.allocPrint(a, " 1/{d}", .{s.nhits}))); + // Typed into: the field grows toward the count, which stays put. + for ("wr") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + row = firstRow(try testTag(p, a)); + try std.testing.expect(std.mem.startsWith(u8, row, "Help wr ") and s.nhits > 1); + try std.testing.expectEqual(width, panes.File.displayWidth(tab, row)); + try std.testing.expect(std.mem.endsWith(u8, row, try std.fmt.allocPrint(a, " 1/{d}", .{s.nhits}))); + // The cursor's row is the count's first number. + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expect(std.mem.endsWith(u8, firstRow(try testTag(p, a)), try std.fmt.allocPrint(a, " 2/{d}", .{s.nhits}))); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Unrolled: the hints at the row's end, dim. + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + row = firstRow(try testTag(p, a)); + try std.testing.expect(std.mem.startsWith(u8, row, "␣ SPC ")); + try std.testing.expect(std.mem.endsWith(u8, row, " ↑ roll up · ? field · esc")); + try std.testing.expectEqual(width, panes.File.displayWidth(tab, row)); + const h = (try head(p, a, p.panes[p.view().active].?)).?; + const last = h.spans[h.spans.len - 1]; + try std.testing.expect(last.style == .dim and last.hi == h.text.len); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Where the row has no room for them, the hints go whole. + const narrow = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 24, .rows = 30 }); + defer narrow.deinit(); + _ = try narrow.setTestFile("one\n"); + narrow.update(.{ .key = .{ .cp = ' ' } }); + narrow.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expectEqualStrings("␣ SPC", firstRow(try testTag(narrow, a))); +} + +test "a rolled level breaks between its children only: the rows after the first from its first child's column under the spines, three at most, the last ending `+N ↓`" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 30 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const tab = p.settings.tab; + const width = tagline.tagWidth(p, p.view().active); + p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + const tag = try testTag(p, a); + var rows: [8][]const u8 = undefined; + var n: usize = 0; + var split = std.mem.splitScalar(u8, tag, '\n'); + while (split.next()) |row| : (n += 1) rows[n] = row; + try std.testing.expectEqual(@as(usize, 3), n); + try std.testing.expect(std.mem.startsWith(u8, rows[0], "␣ ▸ b buffer… ")); + // Each row after the first: the spine under `␣`, then from the first + // child's column; no row wider than the tag. + for (rows[1..n]) |row| try std.testing.expect(std.mem.startsWith(u8, row, "│ ") and row["│ ".len] != ' '); + for (rows[0..n]) |row| try std.testing.expect(panes.File.displayWidth(tab, row) <= width); + // The spines in the rule ink, never the key's (UX D4). + const h = (try head(p, a, p.panes[p.view().active].?)).?; + var spines: usize = 0; + for (h.spans) |span| if (std.mem.eql(u8, h.text[span.lo..span.hi], "│")) { + try std.testing.expectEqual(Style.rule, span.style); + spines += 1; + }; + try std.testing.expect(spines > 0); + // Every child shown is whole on one row; the ones not shown are counted. + var children: [bindings.max_children]bindings.Child = undefined; + const level = bindings.level(p, .leader, "", &children); + var whole: usize = 0; + for (level) |c| { + const item = try std.fmt.allocPrint(a, "{s} {s}", .{ bindings.keyName(c.key), c.label }); + for (rows[0..n]) |row| { + if (std.mem.indexOf(u8, row, item) != null) break; + } else continue; + whole += 1; + } + try std.testing.expect(whole > 0 and whole < level.len); + try std.testing.expect(std.mem.endsWith(u8, rows[n - 1], try std.fmt.allocPrint(a, "+{d} ↓", .{level.len - whole}))); + // A level that fits takes one row; its hints at its end where the row + // has room for them, else none. + p.update(.{ .key = .{ .cp = 'h' } }); + const help = try testTag(p, a); + try std.testing.expect(std.mem.indexOfScalar(u8, help, '\n') == null); + try std.testing.expect(std.mem.startsWith(u8, help, "␣ h ▸ b Build ") and std.mem.endsWith(u8, help, " t Tutor")); +} + test "a leaf run from a level is remembered: underlined there, and said on the line under it" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer p.deinit(); @@ -765,7 +1028,7 @@ test "SPC ? is Help over every root's leaves: typed in its head, filtered by the var buf: [row_max]u8 = undefined; try std.testing.expectEqualStrings("SPC t w · Wrap · view", helpLeaf(p, s.hits[0], &buf).row); const tag = try testTag(p, a); - try std.testing.expect(std.mem.indexOf(u8, tag, " Help wrap ") != null); + try std.testing.expect(std.mem.indexOf(u8, tag, "Help wrap ") != null); // Its box says it is typed in; the band holds its rows and a doc. _ = try p.render(a); const r = p.view().rects[p.view().active]; @@ -819,10 +1082,10 @@ test "SPC h k is describe-key: g d replays `g ▸ d Def` with its level and doc for (" hk") |c| p.update(.{ .key = .{ .cp = c } }); const s = &p.view().tag_state; try std.testing.expectEqual(Kind.describe, s.kind); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys …") != null); + try std.testing.expect(std.mem.startsWith(u8, try testTag(p, a), "Keys …")); for ("gd") |c| p.update(.{ .key = .{ .cp = c } }); try std.testing.expect(s.found); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys g ▸ d Def") != null); + try std.testing.expect(std.mem.startsWith(u8, try testTag(p, a), "Keys g ▸ d Def")); // The level unrolled under the tag, the leaf on the cursor, its doc. try std.testing.expect(pane.band_rows >= 4); _ = try p.render(a); @@ -833,9 +1096,9 @@ test "SPC h k is describe-key: g d replays `g ▸ d Def` with its level and doc try std.testing.expect(std.mem.indexOf(u8, text.items, "keys g d") != null); // Another sequence from the next key: ] f, then SPC l r. for ("]f") |c| p.update(.{ .key = .{ .cp = c } }); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys ] ▸ f function") != null); + try std.testing.expect(std.mem.startsWith(u8, try testTag(p, a), "Keys ] ▸ f function")); for (" lr") |c| p.update(.{ .key = .{ .cp = c } }); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys ␣ l ▸ r Rename") != null); + try std.testing.expect(std.mem.startsWith(u8, try testTag(p, a), "Keys ␣ l ▸ r Rename")); // Unbound: struck, runs nothing. for ("gq") |c| p.update(.{ .key = .{ .cp = c } }); try std.testing.expect(s.missed); @@ -861,16 +1124,17 @@ test "C-w's level is window mode's, shown at once and kept while its keys move t var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const a = frame.allocator(); + const rest = try a.dupe(u8, try testTag(p, a)); p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); try std.testing.expect(p.view().window_pick != null); - try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " C-w …")); + try std.testing.expectEqualStrings(rest, try testTag(p, a)); p.advance(p.clock.now_ns); p.clock.now_ns = p.view().tag_state.due_ns; - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " C-w ▸ c Collapse d Del h Left j Down k Up l Right") != null); + try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), "C-w ▸ c Collapse d Del h Left j Down k Up l Right") != null); // Sticky: a move keeps the level. p.update(.{ .key = .{ .cp = 'k' } }); try std.testing.expect(p.view().window_pick != null); - try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " C-w ▸ ") != null); + try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), "C-w ▸ ") != null); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(p.view().window_pick == null); try std.testing.expect(p.view().tag_state.root == null); @@ -953,12 +1217,6 @@ test "a terminal's rolled level keeps its tag rows at every width, C-w's as SPC' p.sync(); const rows, const tag_rows = .{ pane.rows, pane.tag_rows }; p.update(.{ .key = root }); - // Its key cell alone, before the level shows: shorter than the - // tag's own text, the rows it had. - p.sync(); - try std.testing.expect(hostOf(p) == shell); - try std.testing.expectEqual(tag_rows, pane.tag_rows); - try std.testing.expectEqual(rows, pane.rows); p.advance(p.clock.now_ns); p.clock.now_ns = p.view().tag_state.due_ns; p.sync(); @@ -968,7 +1226,43 @@ test "a terminal's rolled level keeps its tag rows at every width, C-w's as SPC' }; } -test "a tag out of rest keeps its rows at rest at least: a root's key cell alone moves no body up" { +test "a band over a terminal is drawn over its body's top rows: its pty keeps its size across the band's open and close" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .file = "mise.toml", .cols = 100, .rows = 30 }); + defer p.deinit(); + _ = pardes.exec.spawnTty(p, 0); + const shell = p.view().active; + const pane = p.panes[shell].?; + pane.body.mode = .normal; + p.sync(); + while (p.nextEffect()) |_| {} + const rows, const cols, const tag_rows = .{ pane.rows, pane.cols, pane.tag_rows }; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + // SPC unrolled, then `:`'s rows (the tagnav case): a band each, over + // the body; no resize of the pty while it is open, nor when it goes. + for ([_][]const Key{ &.{ .{ .cp = ' ' }, .{ .cp = Key.down } }, &.{.{ .cp = ':' }} }) |keys| { + for (keys) |k| p.update(.{ .key = k }); + p.sync(); + try std.testing.expect(hostOf(p) == shell); + try std.testing.expectEqual(@as(u16, 0), pane.band_rows); + try std.testing.expectEqual(tag_rows, pane.tag_rows); + try std.testing.expectEqual(rows, pane.rows); + try std.testing.expectEqual(cols, pane.cols); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const layer = &p.surface.tag_layers[pardes.BAND_LAYER]; + try std.testing.expect(layer.kind == .band and layer.rows > 0); + try std.testing.expectEqual(p.bodyTop(pane, p.view().rects[shell]), layer.viewport.y); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.sync(); + try std.testing.expectEqual(rows, pane.rows); + while (p.nextEffect()) |e| try std.testing.expect(e != .resize_pty); + } +} + +test "a tag out of rest keeps its rows at rest at least: a level shorter than its own text moves no body up" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile("one\n"); @@ -976,19 +1270,23 @@ test "a tag out of rest keeps its rows at rest at least: a root's key cell alone p.sync(); const rows, const tag_rows = .{ pane.rows, pane.tag_rows }; try std.testing.expect(tag_rows > 1); + // Help's head, a row, its band under the rows the tag keeps. p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = '?' } }); p.sync(); try std.testing.expect(hostOf(p) != null); - try std.testing.expectEqual(tag_rows, pane.tag_rows); - try std.testing.expectEqual(rows, pane.rows); + try std.testing.expectEqual(tag_rows, pane.tag_rows - pane.band_rows); + try std.testing.expectEqual(rows, pane.rows + pane.band_rows); // With the keyboard in the tag (whose top row follows its caret) too: - // C-w's key cell, a line, in a tag of more rows. + // C-w's level, a line, in a tag of more rows. toRest(p); pane.focus = .tag; p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; p.sync(); try std.testing.expect(hostOf(p) != null); - try std.testing.expectEqual(tag_rows, pane.tag_rows); + try std.testing.expect(pane.tag_rows >= tag_rows); try std.testing.expectEqual(@as(u16, 0), pane.tag_top); } @@ -1039,6 +1337,8 @@ test "a click that gives the keyboard to another pane ends a level as Esc does; const rl = p.view().rects[low]; // SPC's level in the lower pane's tag; a click in its own tag keeps it. p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; try std.testing.expect(hostOf(p) == low); click(p, rl.x + config.GUTTER + 2, rl.y); try std.testing.expect(hostOf(p) == low and p.view().prefix == .leader); diff --git a/core/Trail.zig b/core/Trail.zig index 1cd14091..c2c1b0c3 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -15,6 +15,7 @@ const config = @import("config.zig"); const layout = @import("layout.zig"); const panes = @import("panes.zig"); const look = @import("look.zig"); +const modal = @import("modal.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; const Spot = look.Spot; @@ -27,7 +28,8 @@ pub const max_steps = 16; /// The bytes of a step's path or word. A jump to a longer path is no step: /// it opens a pane as `Jumps new-pane` does (look.zig open). pub const max_text = 256; -/// The newest back steps written whole; older ones fold to `·` (§6.3). +/// The newest back steps written whole at rest; older ones fold to `·` +/// (§6.3). Out of rest the owner's and the newest alone (`.newest`). pub const shown_steps = 3; /// `cur` held to the steps there are (walk.zig `.place`). @@ -326,81 +328,172 @@ pub fn walkBy(p: *Pardes, id: usize, delta: i32) bool { // ---- the words ---- -pub const Part = enum { back, forward }; +/// `newest`: out of rest (UX D1), the owner's word and the newest back +/// step's alone, each with the separator after it. +pub const Part = enum { back, forward, newest }; /// Step `i`'s word: `‹@p3`, `rank.zig:14`, a list's, search's or recipe's -/// own text; `·` when it is folded. -fn wordOf(t: *const Trail, i: usize, whole: usize, buf: *[max_text + 16]u8) []const u8 { - const s = &t.steps[i]; - if (s.* != .owner and i < t.cur and t.cur - i > whole) return "·"; +/// own text. A place's file is named by its basename, or where another +/// step's file has the same one, by the shortest end of its path the other +/// does not share (UX D8: `core/x.zig:3` beside `dev/x.zig:9`). +fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { // The step itself, not a copy: a word's text is the step's own bytes. - return switch (s.*) { + return switch (t.steps[i]) { .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits - .place => |at| std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(at.path.slice()), at.line }) catch unreachable, // a basename and a u32 fit + .place => |*at| place: { + const path = at.path.slice(); + var from = if (std.mem.lastIndexOfScalar(u8, path, '/')) |slash| slash + 1 else 0; + while (from > 0 and shared(t, i, path[from..])) from = if (std.mem.lastIndexOfScalar(u8, path[0 .. from - 1], '/')) |slash| slash + 1 else 0; + break :place std.fmt.bufPrint(buf, "{s}:{d}", .{ path[from..], at.line }) catch unreachable; // a path and a u32 fit + }, .list, .search, .recipe => |*w| w.text.slice(), }; } +/// Whether another step's file, not step `i`'s, has a path ending in `end` +/// at a `/`. +fn shared(t: *const Trail, i: usize, end: []const u8) bool { + const own = t.steps[i].place.path.slice(); + for (t.steps[0..t.n], 0..) |*s, j| { + if (j == i or s.* != .place) continue; + const path = s.place.path.slice(); + if (std.mem.eql(u8, path, own) or !std.mem.endsWith(u8, path, end)) continue; + if (path.len == end.len or path[path.len - end.len - 1] == '/') return true; + } + return false; +} + const sep = " › "; /// The steps a part writes. fn range(t: *const Trail, part: Part) [2]usize { return switch (part) { - .back => .{ 0, t.cur }, + .back, .newest => .{ 0, t.cur }, .forward => .{ @min(t.n, t.cur + 1), t.n }, }; } +/// The forward steps written whole after the path at rest; the rest fold. +pub const shown_ahead = 2; + +/// Steps [lo, hi) of `part` folded into one dim `+N` word where the first +/// of them is (UX D8): at rest the back steps older than the newest +/// `whole` (never the owner), the forward steps past `shown_ahead`. +fn foldOf(t: *const Trail, part: Part, whole: usize) [2]usize { + return switch (part) { + .back => back: { + const lo: usize = @intFromBool(t.cur > 0 and t.steps[0] == .owner); + break :back .{ lo, @max(lo, t.cur -| whole) }; + }, + .forward => .{ @min(t.n, t.cur + 1 + shown_ahead), t.n }, + .newest => .{ 0, 0 }, + }; +} + +/// The cells of the newest step's word out of rest: a longer one is cut +/// in its middle (UX D1: `Find pick…3/6`). +const newest_cells = 24; + +/// Step `i`'s word as `part` writes it, null where it writes none: a +/// folded step's is the fold's `+N` on its first, none on the others; out +/// of rest the owner's and the newest step's alone, a word longer than +/// `newest_cells` keeping its first and last cells around a `…`. +fn wordIn(t: *const Trail, part: Part, whole: usize, i: usize, buf: *[max_text + 16]u8, cut: *[max_text + 16]u8) ?[]const u8 { + if (part == .newest and t.steps[i] != .owner and i + 1 != t.cur) return null; + const fold = foldOf(t, part, whole); + if (i >= fold[0] and i < fold[1]) return if (i == fold[0]) std.fmt.bufPrint(buf, "+{d}", .{fold[1] - fold[0]}) catch unreachable else null; + const word = wordOf(t, i, buf); + if (part != .newest or word.len <= newest_cells) return word; + const tab = 8; // a step's word holds no tab + if (panes.File.displayWidth(tab, word) <= newest_cells) return word; + const head = panes.File.byteAtDisplay(tab, word, newest_cells / 2); + var tail = word.len; + while (tail > head and panes.File.displayWidth(tab, word[tail..]) < newest_cells / 2 - 1) tail = modal.prevGrapheme(word, tail); + return std.fmt.bufPrint(cut, "{s}…{s}", .{ word[0..head], word[tail..] }) catch word; +} + /// The back steps' words, each with the separator after it (before the -/// path), or the forward steps', each with one before it (after the path): -/// the newest `whole` back steps whole, older ones `·` (`folded`). +/// path), or the forward steps', each with one before it (after the path), +/// the folded ones as one `+N` (`foldOf`); out of rest (`newest`) the +/// owner's and the newest's. pub fn write(t: *const Trail, part: Part, whole: usize, w: *std.Io.Writer) std.Io.Writer.Error!void { var buf: [max_text + 16]u8 = undefined; + var cut: [max_text + 16]u8 = undefined; const r = range(t, part); - for (r[0]..r[1]) |i| switch (part) { - .back => try w.print("{s}" ++ sep, .{wordOf(t, i, whole, &buf)}), - .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, whole, &buf)}), + for (r[0]..r[1]) |i| if (wordIn(t, part, whole, i, &buf, &cut)) |word| switch (part) { + .back, .newest => try w.print("{s}" ++ sep, .{word}), + .forward => try w.print(sep ++ "{s}", .{word}), }; } /// The bytes `write` writes for `part`. pub fn bytes(t: *const Trail, part: Part, whole: usize) usize { var buf: [max_text + 16]u8 = undefined; + var cut: [max_text + 16]u8 = undefined; var n: usize = 0; const r = range(t, part); - for (r[0]..r[1]) |i| n += wordOf(t, i, whole, &buf).len + sep.len; + for (r[0]..r[1]) |i| if (wordIn(t, part, whole, i, &buf, &cut)) |word| { + n += word.len + sep.len; + }; return n; } -/// The back steps pane `pane`'s tag writes whole: `shown_steps` at rest, -/// the newest alone while its tag is out of rest (SPEC §3.4.3), so a -/// state's head and the band's spines keep their room. -pub fn folded(p: *const Pardes, pane: *const Pane) usize { - const host = TagState.hostOf(p) orelse return shown_steps; - return if (p.panes[host] == pane) 1 else shown_steps; +/// The back step `n` and N walk (Pick.walkList, Prompt): the newest list +/// or search; its word is underlined (UX D8). +pub fn walked(t: *const Trail) ?u8 { + var i: usize = t.cur; + while (i > 0) { + i -= 1; + if (t.steps[i] == .list or t.steps[i] == .search) return @intCast(i); + } + return null; +} + +/// Whether pane `pane`'s tag is out of rest in the live view (UX D1: its +/// path, its forward steps and its own words hidden, the owner and the +/// newest step before the head). Asking is rest. +pub fn outOfRest(p: *const Pardes, pane: *const Pane) bool { + const host = TagState.hostOf(p) orelse return false; + return p.panes[host] == pane; } -/// Where back step `i`'s word is in what `write` writes for the back -/// steps, the newest `whole` whole. +/// Where back step `i`'s word (the fold's `+N` for a folded one) is in +/// what `write` writes for the back steps, the newest `whole` whole. pub fn spanOf(t: *const Trail, i: usize, whole: usize) [2]usize { var buf: [max_text + 16]u8 = undefined; + var cut: [max_text + 16]u8 = undefined; var x: usize = 0; - for (0..i) |j| x += wordOf(t, j, whole, &buf).len + sep.len; - return .{ x, x + wordOf(t, i, whole, &buf).len }; + for (0..i) |j| if (wordIn(t, .back, whole, j, &buf, &cut)) |word| { + x += word.len + sep.len; + }; + const fold = foldOf(t, .back, whole); + const at = if (i >= fold[0] and i < fold[1]) fold[0] else i; + if (at < i) return spanOf(t, at, whole); + const word: []const u8 = wordIn(t, .back, whole, i, &buf, &cut) orelse ""; + return .{ x, x + word.len }; +} + +/// Whether back step `i` is in the fold's `+N`. +pub fn isFolded(t: *const Trail, i: usize, whole: usize) bool { + const fold = foldOf(t, .back, whole); + return i >= fold[0] and i < fold[1]; } /// The step whose word in `part` is at byte `off` of what `write` wrote /// for it, null on a separator or past them. pub fn stepAt(t: *const Trail, part: Part, whole: usize, off: usize) ?u8 { var buf: [max_text + 16]u8 = undefined; + var cut: [max_text + 16]u8 = undefined; var x: usize = 0; const r = range(t, part); + const fold = foldOf(t, part, whole); for (r[0]..r[1]) |i| { + const word = wordIn(t, part, whole, i, &buf, &cut) orelse continue; if (part == .forward) x += sep.len; - const n = wordOf(t, i, whole, &buf).len; - if (off >= x and off < x + n) return @intCast(i); - x += n; - if (part == .back) x += sep.len; + // A fold's `+N` is the step nearest the path of those it holds. + if (off >= x and off < x + word.len) return @intCast(if (i == fold[0] and fold[0] < fold[1]) (if (part == .back) fold[1] - 1 else fold[0]) else i); + x += word.len; + if (part != .forward) x += sep.len; } return null; } @@ -744,19 +837,88 @@ test "the walk drawn with a picker's list and the notice row (walk UI G 2e, SPEC try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, p.panes[rd].?), "› Refs 2/2 ›") != null); } -test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" { +test "a trail keeps its newest three back steps whole, older ones fold into one dim `+N` after the owner, and a click on it goes back to the newest of them; forward steps past two fold the same way" { var f: Fixture = .{ .p = undefined, .tmp = undefined }; try f.init(60); defer f.deinit(); const p = f.p; const owner = p.panes[f.held].?.serial; - for ([_]usize{ 10, 20, 30, 40, 50 }) |line| f.look(f.held, "rank.zig", line); + for ([_]usize{ 10, 20, 30, 40, 50, 60 }) |line| f.look(f.held, "rank.zig", line); const r = p.view().active; + const pane = p.panes[r].?; + // Wide enough for the three newest whole on its one row. + p.update(.{ .resize = .{ .cols = 400, .rows = 60 } }); var buf: [4096 + 128]u8 = undefined; var at: [64]u8 = undefined; - try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › · › rank.zig:20 › rank.zig:30 › rank.zig:40 › {s}/rank.zig Keep ", .{ owner, f.dir }))); - try clickTag(p, r, "·", .right); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&buf, "‹@p{d} › +2 › rank.zig:30 › rank.zig:40 › rank.zig:50 › {s}/rank.zig Keep ", .{ owner, f.dir }))); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const fold = try tagCell(p, r, "+2"); + // In the dim ink of the reading tag's ground (UX D4). + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.on(.reading_focused).dim }, s.at(fold[0], fold[1]).style.fg); + try clickTag(p, r, "+2", .right); + try std.testing.expectEqualStrings("rank.zig:20", f.at(&at)); + // Back at the first: five forward steps, the two nearest whole. + try clickTag(p, r, "rank.zig:10", .right); try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), try std.fmt.bufPrint(&buf, "{s}/rank.zig › rank.zig:20 › rank.zig:30 › +3 Keep ", .{f.dir})) != null); +} + +test "the walk never adds a row to a tag at rest: its steps fold further, down to the newest alone, where they would" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + f.look(f.held, "rank.zig", 10); + for ([_]usize{ 20, 30, 40, 50, 60, 70 }) |line| { + f.look(p.view().active, "place.zig", line); + f.look(p.view().active, "rank.zig", line); + } + const r = p.view().active; + const pane = p.panes[r].?; + try std.testing.expect(pane.trail.cur > 4); + // A width 35 cells past what the tag takes with no walk: room on its + // row for the owner, a fold and the newest, not for three. + const walk = pane.trail; + pane.trail.n = 0; + pane.trail.cur = 0; + const bare = panes.File.displayWidth(p.settings.tab, try tagOf(p, pane)); + pane.trail = walk; + const extra = p.view().screen_w - pardes.tagline.tagWidth(p, r); + p.update(.{ .resize = .{ .cols = @intCast(bare + 35 + extra), .rows = 60 } }); + p.sync(); + try std.testing.expectEqual(@as(u16, 1), pardes.layout.wantedTagRows(p, r)); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), " › +") != null); +} + +test "two steps whose files have the same name are told apart by the shortest path they do not share; the word n walks is underlined" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + try f.tmp.dir.createDirPath(std.testing.io, "core"); + try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = "core/rank.zig", .data = "a\nb\nc\n" }); + f.look(f.held, "rank.zig", 12); + f.look(p.view().active, "core/rank.zig", 2); + f.look(p.view().active, "place.zig", 3); + const r = p.view().active; + var buf: [4096 + 128]u8 = undefined; + const tag = try tagOf(p, p.panes[r].?); + try std.testing.expect(std.mem.indexOf(u8, tag, try std.fmt.bufPrint(&buf, " › {s}/rank.zig:12 › core/rank.zig:2 › ", .{std.Io.Dir.path.basename(f.dir)})) != null); + // A list word: the one n walks, underlined. + p.panes[r].?.trail.steps[1] = .{ .list = .{} }; + p.panes[r].?.trail.steps[1].list.text.set("Find x 1/2"); + p.sync(); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const word = try tagCell(p, r, "Find x 1/2"); + try std.testing.expect(s.at(word[0], word[1]).style.ul == .single); + // In the key ink (UX D8, D4). + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.key }, s.at(word[0], word[1]).style.fg); + const other = try tagCell(p, r, "place.zig"); + try std.testing.expect(s.at(other[0], other[1]).style.ul != .single); } test "Ctrl-o goes back one place in a reader's walk and Ctrl-i forward again; past its first place, or from a held pane, the jump list's" { @@ -857,12 +1019,38 @@ test "Enter on a word of the walk, the keyboard in the tag, goes back to its ste var at: [64]u8 = undefined; const rest = try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:10 › rank.zig:20 › {s}/rank.zig Keep", .{ owner, f.dir }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest)); - // SPC: out of rest, the older steps are `·`, the owner and the newest - // whole; back at rest, as they were. + // SPC, its level shown: out of rest the tag is the owner, the newest + // step and the head (UX D1): no older step, no path, no Keep, none of + // its own words; back at rest, as they were. + const whole_rest = try p.gpa.dupe(u8, try tagOf(p, pane)); + defer p.gpa.free(whole_rest); p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + p.sync(); var folded_buf: [4096 + 128]u8 = undefined; - try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&folded_buf, "‹@p{d} › · › rank.zig:20 › {s}/rank.zig", .{ owner, f.dir }))); + const out = try tagOf(p, pane); + try std.testing.expect(std.mem.startsWith(u8, out, try std.fmt.bufPrint(&folded_buf, "‹@p{d} › rank.zig:20 › ␣ ▸ ", .{owner}))); + try std.testing.expect(std.mem.indexOf(u8, out, "rank.zig:10") == null and std.mem.indexOf(u8, out, f.dir) == null and std.mem.indexOf(u8, out, "Keep") == null); + // Look on the newest step's word is that step. + try std.testing.expectEqual(@as(?u8, pane.trail.cur - 1), pardes.tagline.trailAt(p, pane, std.mem.indexOf(u8, out, "rank.zig:20").? + 3)); + try std.testing.expectEqual(@as(?u8, 0), pardes.tagline.trailAt(p, pane, 1)); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expectEqualStrings(whole_rest, try tagOf(p, pane)); + // Back a step (a forward step after the path at rest): out of rest the + // forward step is hidden as the path is. + pardes.Trail.lookStep(p, r, pane.trail.cur - 1); + p.sync(); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "rank.zig:30") != null); + p.update(.{ .key = .{ .cp = ' ' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + p.sync(); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), try std.fmt.bufPrint(&folded_buf, "‹@p{d} › rank.zig:10 › ␣ ▸ ", .{owner}))); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "rank.zig:30") == null); p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + pardes.Trail.lookStep(p, r, pane.trail.cur + 1); + p.sync(); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), rest)); // The keyboard in the tag, its cursor on `rank.zig:10`: Enter goes back // to that step, the keyboard in the body again. diff --git a/core/bindings.zig b/core/bindings.zig index 96ce1359..a8b81842 100644 --- a/core/bindings.zig +++ b/core/bindings.zig @@ -45,26 +45,25 @@ pub const Info = struct { /// Its key cell in the head (`␣` for SPC). cell: []const u8, label: []const u8, - /// How long after it the level shows (SPEC §3.3). + /// How long after it the level shows (SPEC §3.3): the tag changes + /// nothing before (UX D12), so `gg` and a fast `SPC f s` never touch it; + /// its keys show on the notice row's right end at once (Messages). delay_ns: u64, - /// Its key cell is written at once (SPC, `"`, `C-w`); the others change - /// nothing until the delay is up, so `gg` never touches the tag. - at_once: bool, node: *const Node, }; const ms = std.time.ns_per_ms; pub const info = std.EnumArray(Root, Info).init(.{ - .leader = .{ .key = .{ .cp = config.leader[0].cp }, .cell = "␣", .label = "SPC", .delay_ns = 250 * ms, .at_once = true, .node = &leader_tree }, - .window = .{ .key = .{ .cp = config.window_prefix[0].cp, .ctrl = true }, .cell = "C-w", .label = "window", .delay_ns = 250 * ms, .at_once = true, .node = &window_tree }, - .goto = .{ .key = .{ .cp = config.goto_prefix }, .cell = "g", .label = "goto", .delay_ns = 500 * ms, .at_once = false, .node = &goto_tree }, - .match = .{ .key = .{ .cp = config.match_prefix }, .cell = "m", .label = "match", .delay_ns = 500 * ms, .at_once = false, .node = &match_tree }, - .view = .{ .key = .{ .cp = config.view_prefix }, .cell = "z", .label = "view", .delay_ns = 500 * ms, .at_once = false, .node = &view_tree }, - .view_sticky = .{ .key = .{ .cp = config.view_prefix_sticky }, .cell = "Z", .label = "view", .delay_ns = 500 * ms, .at_once = false, .node = &view_sticky_tree }, - .prev = .{ .key = .{ .cp = config.prev_prefix }, .cell = "[", .label = "previous", .delay_ns = 500 * ms, .at_once = false, .node = &prev_tree }, - .next = .{ .key = .{ .cp = config.next_prefix }, .cell = "]", .label = "next", .delay_ns = 500 * ms, .at_once = false, .node = &next_tree }, - .register = .{ .key = .{ .cp = config.register_prefix }, .cell = "\"", .label = "registers", .delay_ns = 300 * ms, .at_once = true, .node = ®ister_tree }, + .leader = .{ .key = .{ .cp = config.leader[0].cp }, .cell = "␣", .label = "SPC", .delay_ns = 250 * ms, .node = &leader_tree }, + .window = .{ .key = .{ .cp = config.window_prefix[0].cp, .ctrl = true }, .cell = "C-w", .label = "window", .delay_ns = 250 * ms, .node = &window_tree }, + .goto = .{ .key = .{ .cp = config.goto_prefix }, .cell = "g", .label = "goto", .delay_ns = 500 * ms, .node = &goto_tree }, + .match = .{ .key = .{ .cp = config.match_prefix }, .cell = "m", .label = "match", .delay_ns = 500 * ms, .node = &match_tree }, + .view = .{ .key = .{ .cp = config.view_prefix }, .cell = "z", .label = "view", .delay_ns = 500 * ms, .node = &view_tree }, + .view_sticky = .{ .key = .{ .cp = config.view_prefix_sticky }, .cell = "Z", .label = "view", .delay_ns = 500 * ms, .node = &view_sticky_tree }, + .prev = .{ .key = .{ .cp = config.prev_prefix }, .cell = "[", .label = "previous", .delay_ns = 500 * ms, .node = &prev_tree }, + .next = .{ .key = .{ .cp = config.next_prefix }, .cell = "]", .label = "next", .delay_ns = 500 * ms, .node = &next_tree }, + .register = .{ .key = .{ .cp = config.register_prefix }, .cell = "\"", .label = "registers", .delay_ns = 300 * ms, .node = ®ister_tree }, }); /// SPC's groups by their key, as the spec names them (Appendix A). @@ -590,14 +589,15 @@ test "\" shows the registers rolled, the ones holding something, each its value p.clock.now_ns = 5 * std.time.ns_per_s; p.clock.stepped_ns = p.clock.now_ns; p.sync(); - // Its key cell at once, the level 300 ms on. + // Nothing in the tag until the level shows 300 ms on (UX D12). + const rest = try a.dupe(u8, try tagline.tagText(p, a, pane)); p.update(.{ .key = .{ .cp = '"' } }); - try std.testing.expect(std.mem.endsWith(u8, try tagline.tagText(p, a, pane), " \" …")); + try std.testing.expectEqualStrings(rest, try tagline.tagText(p, a, pane)); p.advance(p.clock.now_ns); try std.testing.expectEqual(p.clock.now_ns + 300 * std.time.ns_per_ms, p.view().tag_state.due_ns); p.clock.now_ns = p.view().tag_state.due_ns; const rolled = try tagline.tagText(p, a, pane); - try std.testing.expect(std.mem.indexOf(u8, rolled, " \" ▸ \" self.dirty = true; a const best = rank(q… n ab⏎ p …proj/core/Query.zig % /test.txt . o # 1 ") != null); + try std.testing.expect(std.mem.indexOf(u8, rolled, "\" ▸ \" self.dirty = true; a const best = rank(q… n ab⏎ p …proj/core/Query.zig % /test.txt . o # 1 ") != null); // Its ⏎ in the dim ink. const h = (try @import("TagState.zig").head(p, a, pane)).?; const nl = std.mem.indexOf(u8, h.text, "ab⏎").? + 2; @@ -654,13 +654,13 @@ test "\" shows the registers rolled, the ones holding something, each its value try std.testing.expect(std.mem.indexOf(u8, band.items, "return best;") != null); // Enter chooses it: its verbs, rolled. p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" a ▸ p after P before R replace y into") != null); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), "\" a ▸ p after P before R replace y into") != null); try std.testing.expect(!p.view().tag_state.unrolled); // Backspace: the registers again; a register's own key goes down too. p.update(.{ .key = .{ .cp = Key.backspace } }); - try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" ▸ \" self.dirty") != null); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), "\" ▸ \" self.dirty") != null); p.update(.{ .key = .{ .cp = 'a' } }); - try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" a ▸ p after") != null); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), "\" a ▸ p after") != null); // p pastes it after the selection, and the tag is at rest. p.update(.{ .key = .{ .cp = 'p' } }); try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "const best = rank(q, name) orelse continue;") != null); diff --git a/core/draw.zig b/core/draw.zig index 16dfde5e..a459e3e9 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -362,23 +362,16 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { s.body_layers[id].rows = 0; try renderPane(p, s, arena, pane, v.rects[id], id, id == v.active); } + // A band over a terminal's or a kind's body (Band.over), drawn after + // the body it covers. + if (TagState.hostOf(p)) |id| if (p.panes[id]) |pane| if (pane.band_rows == 0) { + const rows = pardes.Band.shownRows(p, pane, id); + if (rows > 0) try joinBand(p, s, arena, pane, v.rects[id], id, id == v.active, rows); + }; // Each notice once, as a tag layer, and the grid's chip a copy of it. for (s.regionList()) |region| { if (region.kind == .notice) try joinNotice(p, s, arena, region); } - // A tag out of rest: the other panes dim a little so the eye goes to - // it, but a pane showing a preview (walk UI SPEC §3.6). - if (TagState.hostOf(p)) |host| for (p.panes, 0..) |slot, id| { - if (slot == null or id == host or v.tag_state.previewing.isSet(id)) continue; - const r = v.rects[id]; - for (r.y..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { - s.at(@intCast(x), @intCast(y)).style.dim = true; - }; - for ([_]*pardes.Layer{ &s.body_layers[id], &s.tag_layers[id] }) |layer| for (layer.cells[0 .. @as(usize, layer.cols) * layer.rows]) |*cell| { - cell.style.dim = true; - }; - }; - try paintHeaders(p, s); try paintNoticeRow(p, s); @@ -513,7 +506,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: try joinPaneTag(p, s, arena, pane, r, id, active); // The keyboard is in the head's field: its caret, not the body's. const head_caret = if (typing) s.cursor else null; - if (pane.band_rows > 0) try joinBand(p, s, arena, pane, r, id, active); + if (pane.band_rows > 0) try joinBand(p, s, arena, pane, r, id, active, pane.band_rows); // Folded panes retain their body state, but neither body pixels nor // its cursor belong outside the one-row tag rectangle. @@ -704,6 +697,8 @@ fn joinCells(s: *Surface, x: u16, y: u16, cells: []const Cell, from: usize, w: u } fn tagTintRange(p: *const Pardes, pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { lo: usize, hi: usize } { + // Out of rest no name is shown (UX D1). + if (pardes.Trail.outOfRest(p, pane)) return null; if (pane.isTerminal()) { // `tag` is the first row; a prefix longer than it ran to a second. var words = std.mem.tokenizeAny(u8, tag[@min(prefix_len, tag.len)..], " \t\r\n"); @@ -841,8 +836,9 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r const chrome = p.chromeTheme(); const region = s.region(.tag, id).?; const tag = (try tagline.shownTag(p, arena, pane)).text; - // The tag's text rows; the band's under them are its own layer. - const rows = pane.tag_rows - pane.band_rows; + // The tag's text rows; the band's under them are its own layer. A + // tag's own are MAX_TAG_ROWS at most (Pardes.sync). + const rows = @min(pane.tag_rows -| pane.band_rows, MAX_TAG_ROWS); // As wide as the widest row it shows, and never narrower than its // viewport. var widest: usize = 0; @@ -893,13 +889,12 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r /// layer at the tagline's pitch, a row to a line, and copied into the /// grid: the spines and steps in the tag's tint, the keys in the key ink, /// the cursor's row tinted as a selection. -fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { +fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rows: u16) !void { const layer = &s.tag_layers[pardes.BAND_LAYER]; if (r.w <= TAG_TEXT_INSET) return; const band = (try pardes.Band.of(p, arena, pane, id)) orelse return; const chrome = p.chromeTheme(); const tab = p.settings.tab; - const rows = pane.band_rows; const columns = @max(band.columns, 1); // The walk UI's inks on this tag's ground (colors.Roles, UX DESIGN.md // D4), and on its cursor row's selection. @@ -930,7 +925,13 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R for (ln.parts) |part| { const end = @min(columns, part.col + part.width); const on = if (part.row.on) selected else inks; - if (part.row.on and part.col < end) t.tint(part.col, row, end - part.col, selected.ground, selected.text); + // The cursor's row across its step's width, its lead too (UX + // D5), the lead's `▶` keeping the key ink (D4). + if (part.row.on and part.col < end) { + const lead = part.col -| 2; + t.tint(lead, row, part.col - lead, selected.ground, null); + t.tint(part.col, row, end - part.col, selected.ground, selected.text); + } var c = part.col; var b: usize = 0; while (b < part.row.text.len and c < end) : (c += 1) { @@ -968,7 +969,9 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R const count = @as(usize, columns) * rows; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); @memcpy(layer.cells, storage); - const y0 = p.tagTop(pane, r) + (pane.tag_rows - rows); + // Under the tag's text rows; a terminal's or a kind's over its body's + // top rows (Band.over). + const y0 = p.tagTop(pane, r) + (pane.tag_rows - pane.band_rows); layer.kind = .band; layer.id = @intCast(id); layer.serial = pane.serial; @@ -984,7 +987,7 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R /// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its /// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w` /// wide) they wrapped onto. -fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8) void { +fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8, under: bool) void { const a = modal.positionAt(shown.text, shown.fromReal(lo)); const b = modal.positionAt(shown.text, shown.fromReal(hi)); var row = @max(a.row, top); @@ -995,8 +998,11 @@ fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); const row_y = y + @as(u16, @intCast(row - top)); - while (col < end and col < w) : (col += 1) - s.at(x + @as(u16, @intCast(col)), row_y).style.fg = .{ .rgb = ink }; + while (col < end and col < w) : (col += 1) { + const style = &s.at(x + @as(u16, @intCast(col)), row_y).style; + style.fg = .{ .rgb = ink }; + if (under) style.ul = .single; + } } } @@ -1019,7 +1025,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const tx = r.x + @min(TAG_TEXT_INSET, r.w); const tw = r.w -| TAG_TEXT_INSET; const tag_y = p.tagTop(pane, r); - const rows = pane.tag_rows - pane.band_rows; + const rows = @min(pane.tag_rows -| pane.band_rows, MAX_TAG_ROWS); const page_rgb = th.bg orelse th.tag_bg; // Focus is visible along the tag without changing its live text grid. const inks = chrome.roles.on(groundOf(p, pane, active)); @@ -1047,7 +1053,11 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, var lines: [MAX_TAG_ROWS][]const u8 = @splat(""); var split = std.mem.splitScalar(u8, shown.text, '\n'); for (0..top) |_| _ = split.next(); - for (lines[0..rows]) |*line| line.* = split.next() orelse ""; + var nlines: usize = 0; + for (lines[0..rows]) |*line| { + line.* = split.next() orelse continue; + nlines += 1; + } // The keyboard's caret, as a row and a byte offset in that row's // line: in the tag's own text when the tag has the keyboard, in the // path while a name is drafted. @@ -1074,9 +1084,22 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const start = @min(line.len, panes.File.rawAtDisplay(p.settings.tab, line, pane.tag_scroll)); _ = s.print(tx, tag_y + @as(u16, @intCast(row)), tw, line[start..], text_style); } + // Out of rest the rows the tag keeps from rest under the head's own: + // the spines on through them, under the key cells (UX D5). + if (head) |h| if (top == 0 and nlines < rows and h.cells > 0) { + const first = lines[0]; + for (h.spans[0..h.cells]) |cell| { + const at = prefix_len + cell.lo; + if (at > first.len) break; + const col = panes.File.rawDisplayCol(p.settings.tab, first, at); + if (col >= tw) break; + for (nlines..rows) |row| s.set(tx + @as(u16, @intCast(col)), tag_y + @as(u16, @intCast(row)), "│", .{ .fg = .{ .rgb = inks.rule }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }); + } + }; // The pane's progress at its first row's right end, where the row has - // room for it (SPEC §10.2). - if (top == 0 and rows > 0) for (p.messages.live[0..p.messages.live_len]) |*said| { + // room for it (SPEC §10.2); out of rest the head's right block is + // there (UX D1). + if (top == 0 and rows > 0 and !pardes.Trail.outOfRest(p, pane)) for (p.messages.live[0..p.messages.live_len]) |*said| { if (said.kind != .progress or said.pane != pane.serial) continue; var buf: [Messages.LoggedMessage.cap + 8]u8 = undefined; const text = std.fmt.bufPrint(&buf, "{s} {s}", .{ Messages.Kind.progress.glyph(), said.words() }) catch break; @@ -1089,12 +1112,26 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, // wrapped onto. const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len]; if (tagTintRange(p, pane, first_line, prefix_len)) |name| if (name.lo < name.hi) - inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg); - // A walk's forward steps, after the name, dimmed (Trail.zig). + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg, false); + // A walk's forward steps, after the name, dimmed (Trail.zig); its fold + // `+N` too; the word n walks underlined in the key ink (UX D8). const ahead = tagline.forwardSpan(p, pane); - if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), inks.dim); + if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), inks.dim, false); + if (pane.file != null and pane.trail.n > 0 and !pardes.Trail.outOfRest(p, pane)) { + const kept = tagline.folded(p, pane); + const t = &pane.trail; + const first: usize = @intFromBool(t.steps[0] == .owner); + if (t.isFolded(first, kept)) { + const fold = t.spanOf(first, kept); + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, fold[0], @min(fold[1], first_line.len), inks.dim, false); + } + if (t.walked()) |i| if (!t.isFolded(i, kept)) { + const word = t.spanOf(i, kept); + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, word[0], @min(word[1], first_line.len), chrome.roles.key, true); + }; + } // A search word its places no longer light, dimmed (Prompt.zig). - if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), inks.dim); + if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), inks.dim, false); // The head's keys in the key ink, its dimmed, underlined and struck // words (TagState.Span), over whichever rows they wrapped onto. if (head) |h| for (h.spans) |span| { diff --git a/core/dump.zig b/core/dump.zig index 2dde9c8c..c0403c06 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -149,6 +149,7 @@ fn afterLoad(p: *Pardes) void { }; @import("jj.zig").loaded(p); pardes.TagState.loaded(p); + pardes.views.bandsLoaded(p); } /// A file kept slim in a dump (`dumpExit`, a closed pane's past its bound, diff --git a/core/layout.zig b/core/layout.zig index c0d21d6d..5550ef4a 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -594,7 +594,7 @@ pub fn wantedTagRows(p: *Pardes, id: usize) u16 { var wrap_room: [32 * 1024]u8 = undefined; var wrap_fba = std.heap.FixedBufferAllocator.init(&wrap_room); const wa = wrap_fba.allocator(); - const prefix = tagline.prefixIn(p, wa, pane) catch return pane.tag_rows; + const prefix = tagline.restPrefix(p, wa, pane) catch return pane.tag_rows; const whole = std.mem.concat(wa, u8, &.{ prefix, tagline.curTail(pane) }) catch return pane.tag_rows; const shown = tagline.wrapTag(p.settings.tab, wa, whole, tagline.tagWidth(p, id)) catch return pane.tag_rows; return @intCast(@max(BOX_H, @min(shown.rows(), MAX_TAG_ROWS))); @@ -1471,13 +1471,17 @@ pub fn compute(p: *Pardes) void { /// The fewest rows a pane is laid out with: a terminal keeps its tag and /// `min_body_rows` (a shorter one loses its prompt and reads busy for /// ever), a text pane may shrink to its tag, as acme's windows do. A tag -/// out of rest keeps its band and four body rows, borrowed from the -/// column's other panes, which shrink to their tags until it rolls up -/// (walk UI SPEC §3.6). +/// out of rest keeps its band and a body of 60 % of the pane and 6 rows at +/// least (UX D6), borrowed from the column's other panes, which shrink to +/// their tags until it rolls up (walk UI SPEC §3.6). fn floorRows(p: *Pardes, id: usize) u16 { const pane = p.panes[id] orelse return 0; - const band = pardes.Band.takes(p, id); - if (band > 0) return @max(1, pane.tag_rows -| pane.band_rows) + band + 4; + const band = if (pardes.Band.over(pane)) 0 else pardes.Band.takes(p, id); + if (band > 0) { + const above = @max(1, pane.tag_rows -| pane.band_rows) + band; + // A body of 60 % is one and a half times what is above it. + return above + @max(pardes.Band.body_rows, (3 * above + 1) / 2); + } return if (pane.isTerminal()) @max(1, pane.tag_rows) + min_body_rows else 1; } diff --git a/core/lsp/Ask.zig b/core/lsp/Ask.zig index f10ddf26..0f4f39dc 100644 --- a/core/lsp/Ask.zig +++ b/core/lsp/Ask.zig @@ -788,7 +788,7 @@ test "SPC l r types the new name in the pane's own tag, the body showing each pl var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const a = frame.allocator(); - try std.testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagText(p, a, pane), " Rename best → bester") != null); + try std.testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagText(p, a, pane), "Rename best → bester") != null); p.sync(); const sf = try p.render(a); const body = sf.region(.body, id).?.rect; diff --git a/core/mouse.zig b/core/mouse.zig index 89a8c5c6..6516ea71 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -914,10 +914,11 @@ fn bandPoint(p: *Pardes, mcol: u16, mrow: u16) ?BandPoint { if (v.pointer_tag_hit) |hit| return if (hit.kind == .band) .{ .id = hit.id, .line = hit.line, .col = hit.col } else null; const id = paneAt(p, mcol, mrow) orelse return null; const pane = p.panes[id].?; - if (pane.band_rows == 0) return null; + const rows = pardes.Band.shownRows(p, pane, id); + if (rows == 0) return null; const r = v.rects[id]; const top = p.tagTop(pane, r) + (pane.tag_rows - pane.band_rows); - if (mrow < top or mrow >= top + pane.band_rows or mcol < r.x + TAG_TEXT_INSET) return null; + if (mrow < top or mrow >= top + rows or mcol < r.x + TAG_TEXT_INSET) return null; return .{ .id = id, .line = mrow - top, .col = mcol - (r.x + TAG_TEXT_INSET) }; } diff --git a/core/tagline.zig b/core/tagline.zig index e95d68de..5f7222e6 100644 --- a/core/tagline.zig +++ b/core/tagline.zig @@ -124,6 +124,22 @@ fn shownDir(arena: std.mem.Allocator, pane: *Pane) ![]const u8 { /// PDF's page, a terminal's directory -- without the space that parts it /// from the tag's own text. It is never stored, and never edited. pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { + // Out of rest (UX D1) the owner's word and the newest step's, each with + // its ` › `, before the head: the path, the forward steps and Keep + // hidden until rest (a picker's preview is no name of the pane's). + if (pardes.Trail.outOfRest(p, pane)) { + if (pane.trail.n == 0) return ""; + const room = try arena.alloc(u8, pane.trail.bytes(.newest, 1)); + var w: std.Io.Writer = .fixed(room); + pane.trail.write(.newest, 1, &w) catch return error.OutOfMemory; + return w.buffered(); + } + return restPath(p, arena, pane); +} + +/// The computed start of a pane's tag at rest, whatever its state: what +/// the tag's own text follows. +fn restPath(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { // What is drawn: with no graphics, glyph art whatever `Petscii` says. if (pane.addon != null) return pardes.Kind.prefix(p, arena, pane); // A command pane: its directory, the line it runs, and how that went. @@ -163,7 +179,7 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co // Its walk's back steps before the name, its forward steps after // it (Trail.zig), in the room `bytes` measures them. const keep_word = if (keep) " Keep" else ""; - const whole = pardes.Trail.folded(p, pane); + const whole = folded(p, pane); const room = try arena.alloc(u8, pane.trail.bytes(.back, whole) + name.len + pane.trail.bytes(.forward, whole) + keep_word.len); var w: std.Io.Writer = .fixed(room); pane.trail.write(.back, whole, &w) catch return error.OutOfMemory; @@ -185,14 +201,47 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co /// Where a file's name starts in its tag: after its walk's back steps. pub fn nameAt(p: *const Pardes, pane: *const Pane) usize { - return if (pane.file != null and pane.trail.n > 0) pane.trail.bytes(.back, pardes.Trail.folded(p, pane)) else 0; + return if (pane.file != null and pane.trail.n > 0) pane.trail.bytes(.back, folded(p, pane)) else 0; +} + +/// The back steps a file's tag writes whole at rest (UX D8): the newest +/// three (Trail.shown_steps), fewer, down to the newest alone, while its +/// walk would make the tag take more rows than it takes with none: the +/// walk never adds a row to a tag at rest (as far as the newest alone +/// lets it). +pub fn folded(p: *const Pardes, pane: *const Pane) usize { + const t = &pane.trail; + const f = pane.file orelse return pardes.Trail.shown_steps; + if (t.n == 0) return pardes.Trail.shown_steps; + const id = panes.paneIdOf(p, pane) orelse return pardes.Trail.shown_steps; + const tab = p.settings.tab; + const width = tagWidth(p, id); + const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; + const bare = panes.File.displayWidth(tab, name) + @as(usize, if (pardes.Trail.reading(p, pane)) " Keep".len else 0) + panes.File.displayWidth(tab, curTail(pane)); + var whole: usize = pardes.Trail.shown_steps; + while (whole > 1 and rowsOf(bare + walkCells(t, whole, tab), width) > rowsOf(bare, width)) whole -= 1; + return whole; +} + +/// The cells a walk's words take at rest with its newest `whole` back +/// steps whole. +fn walkCells(t: *const pardes.Trail, whole: usize, tab: u16) usize { + var room: [pardes.Trail.max_steps * (pardes.Trail.max_text + 32)]u8 = undefined; + var w: std.Io.Writer = .fixed(&room); + t.write(.back, whole, &w) catch {}; + t.write(.forward, whole, &w) catch {}; + return panes.File.displayWidth(tab, w.buffered()); +} + +fn rowsOf(cells: usize, width: usize) usize { + return @max(1, (cells + width - 1) / width); } /// The bytes of a file's tag its walk's forward steps take, after its name. pub fn forwardSpan(p: *const Pardes, pane: *const Pane) [2]usize { const f = pane.file orelse return .{ 0, 0 }; - if (pane.trail.n == 0) return .{ 0, 0 }; - const whole = pardes.Trail.folded(p, pane); + if (pane.trail.n == 0 or pardes.Trail.outOfRest(p, pane)) return .{ 0, 0 }; + const whole = folded(p, pane); const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; const lo = pane.trail.bytes(.back, whole) + name.len; return .{ lo, lo + pane.trail.bytes(.forward, whole) }; @@ -202,7 +251,8 @@ pub fn forwardSpan(p: *const Pardes, pane: *const Pane) [2]usize { /// text, if one is there. pub fn trailAt(p: *const Pardes, pane: *const Pane, at: usize) ?u8 { if (pane.trail.n == 0 or pane.file == null) return null; - const whole = pardes.Trail.folded(p, pane); + if (pardes.Trail.outOfRest(p, pane)) return pane.trail.stepAt(.newest, 1, at); + const whole = folded(p, pane); const ahead = forwardSpan(p, pane); if (at < nameAt(p, pane)) return pane.trail.stepAt(.back, whole, at); if (at >= ahead[0] and at < ahead[1]) return pane.trail.stepAt(.forward, whole, at - ahead[0]); @@ -227,7 +277,17 @@ pub fn tagPrefix(p: *Pardes, pane: *Pane) ![]u8 { /// The prefix as the tag shows it, built in `arena`. pub fn prefixIn(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]u8 { - const prefix = try pathPrefix(p, arena, pane); + // Out of rest the head follows: its words end in their own space. + if (pardes.Trail.outOfRest(p, pane)) return arena.dupe(u8, try pathPrefix(p, arena, pane)); + return restPrefix(p, arena, pane); +} + +/// The prefix as the tag has it at rest, whatever its state: what its own +/// text follows, so what its cursor and selections are offsets after +/// (Core.sync's `tag_lead`), and the rows it takes at rest +/// (layout.wantedTagRows). +pub fn restPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]u8 { + const prefix = try restPath(p, arena, pane); // The space between the prefix and the tag's own text is the text's // (every default starts with one), but the prefix is computed, so it // cannot trust the text to keep it: with it deleted, Look and Exec @@ -241,7 +301,7 @@ pub fn prefixIn(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]u8 { /// The tag's own text: what the user made of it, or until then the /// default a pane of its kind starts with. -pub fn curTail(pane: *Pane) []const u8 { +pub fn curTail(pane: *const Pane) []const u8 { return pane.tag.own orelse defaultTail(pane); } @@ -411,13 +471,13 @@ pub fn tagOffsetAt(p: *Pardes, pane: *Pane, row: usize, col: usize) ?modal.Curso /// words; null off it. pub fn onName(p: *const Pardes, pane: *Pane, row: usize, col: usize) ?usize { const tab = p.settings.tab; - if (row + pane.tag_top != 0) return null; + if (row + pane.tag_top != 0 or pardes.Trail.outOfRest(p, pane)) return null; const f = pane.file orelse return null; if (!panes.Output.fileTraits(f.output).saves) return null; const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; var room: [pardes.Trail.max_steps * (pardes.Trail.max_text + 32)]u8 = undefined; var w: std.Io.Writer = .fixed(&room); - pane.trail.write(.back, pardes.Trail.folded(p, pane), &w) catch return null; + pane.trail.write(.back, folded(p, pane), &w) catch return null; const lead = panes.File.displayWidth(tab, w.buffered()); if (col < lead or col >= lead + panes.File.displayWidth(tab, name)) return null; return col - lead; diff --git a/core/views.zig b/core/views.zig index b05a116b..7bf30aa0 100644 --- a/core/views.zig +++ b/core/views.zig @@ -171,6 +171,24 @@ pub fn undumped(p: *Pardes, others: []const Shares) void { } } +/// After a load: each view's band rows (derived: never dumped nor in the +/// digest) made again from its own tag state as its last sync made them. +/// Its tag rows, which are dumped, count them, and a view drawn before its +/// next step (drawOne: a replica client's, a pump's other views) reads the +/// tag's own rows as their difference (draw.zig): with the band's rows +/// zeroed by the load, a band's became the tag's, past MAX_TAG_ROWS. +pub fn bandsLoaded(p: *Pardes) void { + for (0..max) |k| if (isOpen(p, @intCast(k))) within(p, @intCast(k), {}, bandsOf) catch {}; +} + +fn bandsOf(_: void, p: *Pardes) anyerror!void { + for (p.panes, 0..) |slot, id| if (slot) |pane| { + const rows = if (pane.collapsed or pardes.Band.over(pane)) 0 else @min(pardes.Band.takes(p, id), pane.tag_rows -| 1); + pane.band_rows = rows; + pane.pv[p.live].band_rows = rows; + }; +} + /// The views a dump before `Pardes.views` kept (`OldDumped`): each its /// share of the session into `views[k]`, the rest as `undumped`. What /// each view holds is the session's then; one refused is freed. @@ -581,6 +599,36 @@ pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa const testing = std.testing; const Key = pardes.Key; +test "a view not live with its band open, dumped and loaded, draws before its next step: its band's rows made again from its state, its tag's rows never past MAX_TAG_ROWS" { + const gpa = std.testing.allocator; + const dump = @import("dump.zig"); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + // SPC's level unrolled in view 0: its band's rows in the tag's. + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + p.sync(); + const band = pane.band_rows; + try testing.expect(band > 0 and pane.tag_rows - band <= pardes.MAX_TAG_ROWS and pane.tag_rows > pardes.MAX_TAG_ROWS); + // Another view live, view 0 parked, as a client's is when a join's + // (a replica's rollback's) dump is made. + const k = p.openView().?; + p.toView(k); + const bytes = try dump.byteDump(p); + defer gpa.free(bytes); + const q = try dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 30 }, bytes, null); + defer q.deinit(); + try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, q)); + // View 0 drawn with no step first (a client's draw, a pump's other + // views): its band as it was, the tag's own rows under the bound. + try testing.expectEqual(band, q.panes[0].?.pv[0].band_rows); + _ = try drawOne(q, 0, .{ .ctx = null, .f = null }); + // The bound itself: a share whose band's rows are not counted. + q.panes[0].?.pv[0].band_rows = 0; + _ = try drawOne(q, 0, .{ .ctx = null, .f = null }); +} + fn typeKeys(p: *Pardes, text: []const u8) void { for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } |
