summaryrefslogtreecommitdiff
path: root/core/Trail.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Trail.zig')
-rw-r--r--core/Trail.zig93
1 files changed, 76 insertions, 17 deletions
diff --git a/core/Trail.zig b/core/Trail.zig
index 9223d1d3..58a72256 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`).
@@ -311,7 +313,9 @@ 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.
@@ -331,19 +335,43 @@ 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 },
};
}
+/// Whether step `i` is in what `part` writes: out of rest the owner and
+/// the newest back step only.
+fn writes(t: *const Trail, part: Part, i: usize) bool {
+ return part != .newest or t.steps[i] == .owner or i + 1 == t.cur;
+}
+
+/// 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: out of rest a word longer than
+/// `newest_cells` keeps its first and last cells around a `…`.
+fn shownWord(t: *const Trail, part: Part, i: usize, whole: usize, buf: *[max_text + 16]u8, cut: *[max_text + 16]u8) []const u8 {
+ const word = wordOf(t, i, whole, 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`).
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)}),
+ for (r[0]..r[1]) |i| if (writes(t, part, i)) switch (part) {
+ .back, .newest => try w.print("{s}" ++ sep, .{shownWord(t, part, i, whole, &buf, &cut)}),
.forward => try w.print(sep ++ "{s}", .{wordOf(t, i, whole, &buf)}),
};
}
@@ -351,18 +379,21 @@ pub fn write(t: *const Trail, part: Part, whole: usize, w: *std.Io.Writer) std.I
/// 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 (writes(t, part, i)) {
+ n += shownWord(t, part, i, whole, &buf, &cut).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;
+/// 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
@@ -378,14 +409,16 @@ pub fn spanOf(t: *const Trail, i: usize, whole: usize) [2]usize {
/// 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);
for (r[0]..r[1]) |i| {
+ if (!writes(t, part, i)) continue;
if (part == .forward) x += sep.len;
- const n = wordOf(t, i, whole, &buf).len;
+ const n = shownWord(t, part, i, whole, &buf, &cut).len;
if (off >= x and off < x + n) return @intCast(i);
x += n;
- if (part == .back) x += sep.len;
+ if (part != .forward) x += sep.len;
}
return null;
}
@@ -841,12 +874,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.