summaryrefslogtreecommitdiff
path: root/core/Band.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Band.zig')
-rw-r--r--core/Band.zig135
1 files changed, 118 insertions, 17 deletions
diff --git a/core/Band.zig b/core/Band.zig
index 3c800bf0..835c249f 100644
--- a/core/Band.zig
+++ b/core/Band.zig
@@ -27,6 +27,7 @@ const panes = @import("panes.zig");
const builtins = @import("builtins.zig");
const TagState = @import("TagState.zig");
const Names = @import("Names.zig");
+const Prompt = @import("Prompt.zig");
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
const File = panes.File;
@@ -100,11 +101,15 @@ pub fn takes(p: *Pardes, id: usize) u16 {
return @max(doc_rows, levelRows(@intCast(bindings.level(p, s.scope_root.?, s.scope[0 .. s.scope_n - 1], &children).len)));
},
.pick => return @import("Pick.zig").bandRows(p),
+ .prompt => return Prompt.takes(p),
+ .rename => return 0,
}
if (!s.unrolled) return 0;
const cur = TagState.current(p, v) orelse return 0;
var children: [bindings.max_children]bindings.Child = undefined;
const n: u16 = @intCast(bindings.level(p, cur.root, cur.path, &children).len);
+ // The registers, one column of eleven at most (registerSteps).
+ if (cur.root == .register and cur.path.len == 0) return @max(doc_rows, @min(Cursor.shown, n));
return @max(doc_rows, levelRows(n));
}
@@ -149,8 +154,20 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band {
try band.place(p, arena, pane, id);
return band;
},
+ .prompt => {
+ var steps: [max_steps]Step = undefined;
+ for (try Prompt.steps(p, arena, &steps)) |step| band.add(step);
+ try band.place(p, arena, pane, id);
+ return band;
+ },
+ .rename => return null,
}
const cur = TagState.current(p, v).?;
+ if (cur.root == .register) {
+ try band.registerSteps(p, arena, pane, cur.path);
+ try band.place(p, arena, pane, id);
+ return band;
+ }
// The level, its cursor on the child it names, else the leaf its group
// ran last, else the first; and that child's doc beside it.
const node = bindings.nodeAt(cur.root, cur.path);
@@ -196,6 +213,63 @@ fn helpSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, s: *const TagSta
band.add(try docStep(p, arena, at.root, at.path[0 .. at.path.len - 1], at.child));
}
+/// `"` unrolled (SPEC §9): the registers, a row each (its key, what it
+/// is, its first line) around the cursor, or under `"a` its verbs; beside
+/// them the value of the one on the cursor (or named) in full, as many of
+/// its lines as the band has rows, only those read.
+fn registerSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *const Pane, path: []const u8) !void {
+ const s = &p.view().tag_state;
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const level = bindings.level(p, .register, path, &children);
+ const node = bindings.nodeAt(.register, path);
+ const used = if (node) |n| s.used[n.group] else 0;
+ var at: usize = 0;
+ for (level, 0..) |c, i| if (c.key == (if (s.hi != 0) s.hi else used)) {
+ at = i;
+ };
+ if (path.len > 0) band.add(try levelStep(arena, pane, level, at, used)) else {
+ const w = (Cursor{ .at = @intCast(at) }).window(@intCast(level.len));
+ const rows = try arena.alloc(Row, w.hi - w.lo);
+ for (rows, w.lo..) |*row, i| {
+ const c = level[i];
+ const name = for (bindings.register_names) |r| {
+ if (r.key == c.key) break r.name;
+ } else "named";
+ row.* = .{
+ .text = try std.fmt.allocPrint(arena, "{s} {s: <17} {s}{s}", .{ bindings.keyName(c.key), name, c.label, if (c.more) "…" else "" }),
+ .on = i == at,
+ .key = 1,
+ .under = c.key == used,
+ .index = @intCast(i),
+ };
+ }
+ band.add(.{ .rows = rows });
+ }
+ const key = if (path.len > 0) path[0] else level[at].key;
+ const name = for (bindings.register_names) |r| {
+ if (r.key == key) break r.name;
+ } else "named";
+ var rows: std.ArrayList(Row) = .empty;
+ try rows.append(arena, .{ .text = try std.fmt.allocPrint(arena, "\"{s} · {s}", .{ bindings.keyName(key), name }), .key = 2 });
+ const value = bindings.registerValue(p, key);
+ if (value.len == 0) try rows.append(arena, .{ .text = "(empty)", .dim = true });
+ // A line is cut at what the band could show of it: a long one is
+ // never scanned past that.
+ const most = @as(usize, band.columns) * band.height;
+ var rest = value;
+ while (rest.len > 0 and rows.items.len + 1 < band.height) {
+ const look = rest[0..@min(rest.len, most)];
+ const end = std.mem.indexOfScalar(u8, look, '\n') orelse {
+ try rows.append(arena, .{ .text = rest[0..if (look.len < rest.len) modal.graphemeStart(rest, look.len) else look.len] });
+ break;
+ };
+ try rows.append(arena, .{ .text = look[0..end] });
+ rest = rest[end + 1 ..];
+ }
+ if (path.len == 0) try rows.append(arena, .{ .text = "next p after P before R replace y into", .dim = true });
+ band.add(.{ .rows = rows.items, .doc = true });
+}
+
/// A list's cursor and the rows of it a band shows: eleven at most, around
/// the cursor (SPEC §3.6, the prototype's `H.window`). Each view's (a
/// tag state's field): Help's rows, a picker's, a prompt's history.
@@ -245,7 +319,7 @@ pub fn levelStep(arena: std.mem.Allocator, pane: *const Pane, level: []const bin
for (level, rows, 0..) |c, *row, i| {
const key = bindings.keyName(c.key);
row.* = .{
- .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group) "…" else "" }),
+ .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group or c.more) "…" else "" }),
.on = i == at,
.key = @intCast(key.len),
.dim = bindings.dimmed(pane, c),
@@ -340,24 +414,31 @@ fn findSpines(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id
}
/// `rows` wrapped at `width` display columns, word by word, `height` rows
-/// at most: a doc's paragraphs.
+/// at most: a doc's paragraphs, a line each in its text (builtins.Doc),
+/// each starting a row (a newline drawn in a row was a `�`).
fn wrap(arena: std.mem.Allocator, tab: u16, rows: []const Row, width: u16, max: u16) ![]const Row {
var out: std.ArrayList(Row) = .empty;
- for (rows) |row| {
- if (File.displayWidth(tab, row.text) <= width) {
- try out.append(arena, row);
- } else {
- var rest = row.text;
- while (rest.len > 0 and out.items.len < max) {
- var cut = File.byteAtDisplay(tab, rest, width);
- if (cut < rest.len) {
- if (std.mem.lastIndexOfScalar(u8, rest[0..cut], ' ')) |space| {
- if (space > 0) cut = space;
+ for (rows) |whole| {
+ var paragraphs = std.mem.splitScalar(u8, whole.text, '\n');
+ var row = whole;
+ while (paragraphs.next()) |text| : (row = .{ .text = "" }) {
+ if (out.items.len >= max) break;
+ row.text = text;
+ if (File.displayWidth(tab, row.text) <= width) {
+ try out.append(arena, row);
+ } else {
+ var rest = row.text;
+ while (rest.len > 0 and out.items.len < max) {
+ var cut = File.byteAtDisplay(tab, rest, width);
+ if (cut < rest.len) {
+ if (std.mem.lastIndexOfScalar(u8, rest[0..cut], ' ')) |space| {
+ if (space > 0) cut = space;
+ }
}
+ if (cut == 0) cut = modal.nextGrapheme(rest, 0);
+ try out.append(arena, .{ .text = std.mem.trimEnd(u8, rest[0..cut], " ") });
+ rest = std.mem.trimStart(u8, rest[cut..], " ");
}
- if (cut == 0) cut = modal.nextGrapheme(rest, 0);
- try out.append(arena, .{ .text = std.mem.trimEnd(u8, rest[0..cut], " ") });
- rest = std.mem.trimStart(u8, rest[cut..], " ");
}
}
if (out.items.len >= max) break;
@@ -427,8 +508,8 @@ pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, y: u16, col: u16)
}
fn bandLayer(p: *Pardes, id: usize) ?*pardes.Layer {
- const layer = &p.surface.tag_layers[pardes.BAND_LAYER_BASE + id];
- return if (layer.rows > 0 and layer.kind == .band) layer else null;
+ const layer = &p.surface.tag_layers[pardes.BAND_LAYER];
+ return if (layer.rows > 0 and layer.kind == .band and layer.id == id) layer else null;
}
fn layerRow(arena: std.mem.Allocator, layer: *const pardes.Layer, row: usize) ![]const u8 {
@@ -552,3 +633,23 @@ test "the band's words are read-only text: B2 runs one, the tag back at rest" {
try std.testing.expect(p.settings.wrap != wrapped);
try std.testing.expect(p.view().prefix == .none and !p.view().tag_state.unrolled);
}
+
+test "a doc's paragraphs are rows of their own: no newline in a band's row" {
+ 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 id = p.view().active;
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ // SPC b b is Panes, whose documentation has three paragraphs.
+ for ([_]u21{ ' ', 'b', Key.down }) |c| p.update(.{ .key = .{ .cp = c } });
+ const band = (try of(p, a, p.panes[id].?, id)).?;
+ var docs: usize = 0;
+ for (band.steps[0..band.nsteps]) |step| for (step.rows) |row| {
+ try std.testing.expect(std.mem.indexOfScalar(u8, row.text, '\n') == null);
+ if (step.doc) docs += 1;
+ };
+ try std.testing.expect(docs > 2);
+}