//! docs/typ/reference.typ, Follow . const std = @import("std"); const Session = @import("session").Session; fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } /// A session on a 300-line file, `L001` to `L300`, in a window wide enough /// for three columns' tags on one row each. fn init() !Session { var text: [300 * 5]u8 = undefined; for (0..300) |i| _ = try std.fmt.bufPrint(text[i * 5 ..][0..5], "L{d:0>3}\n", .{i + 1}); var s = try Session.initFile(std.testing.allocator, &text); s.detach(); try s.write("/ctl", "size 200 30\n"); return s; } const Page = struct { x: usize, first: usize, last: usize, rows: usize }; /// Each pane's lines as /screen draws them, left to right: the labels /// drawn at one column of cells are one pane's. fn pages(s: *Session) ![]Page { var out: std.ArrayList(Page) = .empty; var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); while (rows.next()) |row| { var i: usize = 0; while (std.mem.indexOfScalarPos(u8, row, i, 'L')) |at| : (i = at + 1) { if (at + 4 > row.len) break; const n = std.fmt.parseInt(usize, row[at + 1 .. at + 4], 10) catch continue; const x = try std.unicode.utf8CountCodepoints(row[0..at]); for (out.items) |*p| { if (p.x != x) continue; p.first = @min(p.first, n); p.last = @max(p.last, n); p.rows += 1; break; } else try out.append(s.arena.allocator(), .{ .x = x, .first = n, .last = n, .rows = 1 }); } } std.mem.sort(Page, out.items, {}, struct { fn lt(_: void, a: Page, b: Page) bool { return a.x < b.x; } }.lt); return out.items; } /// The pages go on one from another: each starts the line after the one /// before it ends, every line drawn once. fn expectConsecutive(ps: []const Page) !void { for (ps) |p| try std.testing.expectEqual(p.last - p.first + 1, p.rows); for (ps[1..], ps[0 .. ps.len - 1]) |p, before| try std.testing.expectEqual(before.last + 1, p.first); } /// The pane the last Follow made: the newest serial in the index. fn newest(s: *Session) !u32 { var max: u32 = 0; var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (lines.next()) |l| max = @max(max, try std.fmt.parseInt(u32, std.mem.sliceTo(l, ' '), 10)); return max; } /// The first line of the cursor's selection, 0-based (a label is 5 bytes). fn line(s: *Session, pane: u32) !usize { return (try s.range(s.at("/pane/{d}/dot", .{pane})))[0] / 5; } test "follow: Follow puts the next page right of the chain, in a new column or at the end of the one there, Unfollow on its tag alone" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const second = try newest(&s); // No column to the right: a new one; the keyboard stays on the head. try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), head, try s.column(1), second }), try s.columns()); try std.testing.expectEqual(head, try s.focused()); try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{second})), " Unfollow")); try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{head})), "Unfollow")); try expectConsecutive(try pages(&s)); // A column right of the last page: the next goes at its end, from // any page of the chain. try s.write(s.at("/pane/{d}/exec", .{second}), "Newcol\n"); const other = try s.newPane(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const third = try newest(&s); try std.testing.expect(has(try s.columns(), s.at(":{d},{d}", .{ other, third }))); // A twin out of the chain is refused one of its own. try s.write(s.at("/pane/{d}/exec", .{head}), "Zerox\n"); try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{try newest(&s)}), "Follow\n") != 0); try std.testing.expect(has(s.said, "Follow: this text has a chain")); } test "follow: two and three pages show consecutive lines; the wheel in any page moves them all" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); var ps = try pages(&s); try std.testing.expectEqual(@as(usize, 2), ps.len); try std.testing.expectEqual(@as(usize, 1), ps[0].first); try expectConsecutive(ps); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); ps = try pages(&s); try std.testing.expectEqual(@as(usize, 3), ps.len); try expectConsecutive(ps); // Three notches down in the second page: the first follows. for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); const scrolled = try pages(&s); try std.testing.expectEqual(@as(usize, 4), scrolled[0].first); try expectConsecutive(scrolled); // And up again in the last. for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 8\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); try std.testing.expectEqual(@as(usize, 1), (try pages(&s))[0].first); try expectConsecutive(try pages(&s)); } test "follow: the cursor past a page's last row takes the keyboard to the next page, and back above its first" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const second = try newest(&s); const ps = try pages(&s); const rows = ps[0].rows; try s.keys(s.at("{d}j", .{rows - 1})); try std.testing.expectEqual(head, try s.focused()); try std.testing.expectEqual(rows - 1, try line(&s, head)); try s.keys("j"); try std.testing.expectEqual(second, try s.focused()); try std.testing.expectEqual(rows, try line(&s, second)); // Nothing scrolled: the pages are where they were. try std.testing.expectEqualSlices(Page, ps, try pages(&s)); try s.keys("k"); try std.testing.expectEqual(head, try s.focused()); try std.testing.expectEqual(rows - 1, try line(&s, head)); // Past the chain's end the chain scrolls as one pane. try s.keys(s.at("{d}j", .{2 * rows + 2})); try std.testing.expectEqual(second, try s.focused()); const moved = try pages(&s); try std.testing.expect(moved[0].first > 1); try expectConsecutive(moved); // A dot written to the other page scrolls it there, the chain with it. try s.write(s.at("/pane/{d}/dot", .{head}), "745 745"); const shown = try pages(&s); try std.testing.expect(shown[0].first <= 150 and 150 <= shown[0].last); try expectConsecutive(shown); } test "follow: a collapsed page is passed over: the cursor past the page before it scrolls the chain, never into the fold" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const second = try newest(&s); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const third = try newest(&s); try s.write(s.at("/pane/{d}/exec", .{third}), "Collapse\n"); const ps = try pages(&s); try std.testing.expectEqual(@as(usize, 2), ps.len); try s.keys(s.at("{d}j", .{ps[0].rows})); try std.testing.expectEqual(second, try s.focused()); // Past the second page's last row: the second is the chain's end now. try s.keys(s.at("{d}j", .{ps[1].rows})); try std.testing.expectEqual(second, try s.focused()); const moved = try pages(&s); try std.testing.expect(moved[0].first > 1); try expectConsecutive(moved); } test "follow: Unfollow leaves a twin with its own scroll and closes the chain up; Del of the head makes the next one head" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const second = try newest(&s); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); const third = try newest(&s); const was = try pages(&s); try s.write(s.at("/pane/{d}/exec", .{second}), "Unfollow\n"); try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{second})), "Unfollow")); var ps = try pages(&s); // The twin keeps its lines; the third follows the head now. try std.testing.expectEqual(was[1], ps[1]); try std.testing.expectEqual(was[0].last + 1, ps[2].first); // A wheel in the twin moves it alone, as Zerox's does. try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); const after = try pages(&s); try std.testing.expectEqual(ps[0], after[0]); try std.testing.expectEqual(ps[2], after[2]); try std.testing.expectEqual(ps[1].first + 1, after[1].first); try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{second}), "Unfollow\n") != 0); // Del of the head: the third is a chain of one, which is no chain. try s.write(s.at("/pane/{d}/ctl", .{second}), "Del\n"); try s.write(s.at("/pane/{d}/exec", .{third}), "Follow\n"); const fourth = try newest(&s); try s.write(s.at("/pane/{d}/ctl", .{head}), "Del\n"); try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{third})), "Unfollow")); try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); ps = try pages(&s); try std.testing.expectEqual(@as(usize, 2), ps.len); try expectConsecutive(ps); try s.write(s.at("/pane/{d}/ctl", .{third}), "Del\n"); try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); } test "follow: a Dump and Restore keep the chain, still one" { var s = try init(); defer s.deinit(); const head = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); try s.write("/ctl", "Dump\n"); const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' ')); try s.write(s.at("/pane/{d}/exec", .{head}), "Unfollow\n"); try s.write("/ctl", s.at("Restore {s}\n", .{label})); var ps = try pages(&s); try std.testing.expectEqual(@as(usize, 3), ps.len); try expectConsecutive(ps); for (0..2) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); ps = try pages(&s); try std.testing.expectEqual(@as(usize, 3), ps[0].first); try expectConsecutive(ps); }