diff options
Diffstat (limited to 'core/Band.zig')
| -rw-r--r-- | core/Band.zig | 193 |
1 files changed, 167 insertions, 26 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]; |
