//! docs/typ/guide.typ, Modes . const std = @import("std"); const Session = @import("session").Session; const expectEqual = std.testing.expectEqual; fn mode(s: *Session, pane: u32) ![]const u8 { const ctl = try s.read(s.at("/pane/{d}/ctl", .{pane})); const at = std.mem.lastIndexOf(u8, ctl, " mode:").? + " mode:".len; return std.mem.trimEnd(u8, ctl[at..], " "); } test "modes: Esc in normal mode alternates between two panes of the same kind" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "b\n" }); var dir: [4096]u8 = undefined; const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; var s = try Session.initFile(std.testing.allocator, "a\n"); defer s.deinit(); const a = try s.focused(); try s.write("/look", s.at("{s}/b.txt\n", .{at})); const b = try s.focused(); try std.testing.expect(a != b); for (0..6) |_| { const was = try s.focused(); try s.keys("\x1b"); try expectEqual(if (was == a) b else a, try s.focused()); } } test "modes: Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); _ = try s.newPane(); for ([_][]const u8{ "New Tty", "Newcol" }) |tag| { const was = try s.focused(); // A click types into the tag; Esc is normal mode there, the next // one is out of it. try s.click(tag, 0, 1); try s.keys("\x1b"); try s.expect("/focus", ""); try s.keys("\x1b"); try std.testing.expect(was != try s.focused()); } } test "modes: Shift-Esc backs out to the previous pane in every mode, never into raw" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const doc = try s.focused(); try s.keys("\u{F015}n"); // a shell under the doc const shell = try s.focused(); if (std.mem.eql(u8, try mode(&s, shell), "tty")) try s.keys("\x02"); try std.testing.expectEqualStrings("normal", try mode(&s, shell)); // A terminal in normal mode: out, still normal, never raw. try s.keys("\u{F016}\x1b"); try expectEqual(doc, try s.focused()); try std.testing.expectEqualStrings("normal", try mode(&s, shell)); // A text pane in insert mode: out of insert, and out of the pane. try s.keys("i"); try std.testing.expectEqualStrings("insert", try mode(&s, doc)); try s.keys("\u{F016}\x1b"); try std.testing.expectEqualStrings("normal", try mode(&s, doc)); try std.testing.expect(doc != try s.focused()); } /// The first line number drawn under the tag that starts with `tag`. fn topLine(s: *Session, tag: []const u8) !usize { const row = (try s.where(tag))[1]; var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); for (0..row + 1) |_| _ = rows.next(); var words = std.mem.tokenizeScalar(u8, rows.next().?, ' '); return std.fmt.parseInt(usize, words.next().?, 10); } test "modes: Esc back into a file leaves its view where it was, and a look centres its line" { var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); defer s.deinit(); const doc = try s.focused(); const dot = s.at("/pane/{d}/dot", .{doc}); try s.keys("\u{F015}n"); // a shell under the doc const shell = try s.focused(); try s.keys("\x1b"); try expectEqual(doc, try s.focused()); // Ride the cursor down until the view has scrolled. try s.keys(&@as([20]u8, @splat('j'))); const top = try topLine(&s, "/test.txt"); const at = try s.range(dot); try std.testing.expect(top > 1); try s.keys("\x1b"); try expectEqual(shell, try s.focused()); try s.keys("\x1b"); try expectEqual(doc, try s.focused()); // Nothing moved: the old landing recentred the line under the cursor. try expectEqual(top, try topLine(&s, "/test.txt")); try expectEqual(at, try s.range(dot)); // The other arm still centres: a look's `:NN`. const line = 60; try s.write(s.at("/pane/{d}/look", .{doc}), s.at(":{d}\n", .{line})); const rows = (try s.where("Tty Save Mode"))[1] - (try s.where("/test.txt"))[1] - 1; // Drawn mid-pane, a row either way. const drawn_at = line - try topLine(&s, "/test.txt"); try std.testing.expect(drawn_at + 1 >= rows / 2 and drawn_at <= rows / 2 + 1); } /// Three panes named /a.txt (left column), /b.txt over /c.txt (right), /// the keyboard on /a.txt; their serials. fn threePanes(s: *Session) ![3]u32 { const a = try s.focused(); try s.write(s.at("/pane/{d}/name", .{a}), "/a.txt\n"); try s.write("/ctl", "Newcol\n"); try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); const b = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{b}), "New\n"); const c = try s.focused(); try s.write(s.at("/pane/{d}/name", .{b}), "/b.txt\n"); try s.write(s.at("/pane/{d}/name", .{c}), "/c.txt\n"); try s.write("/focus", s.at("{d}\n", .{a})); return .{ a, b, c }; } /// The panes whose tag (`name Save`) is drawn in `lit`, the outline's colour. fn outlined(s: *Session, lit: []const u8, names: []const []const u8) ![]const u8 { var found: []const u8 = ""; for (names) |name| { const x, const y = try s.where(s.at("{s} Save", .{name})); if (std.mem.eql(u8, lit, try s.bg(x, y))) found = s.at("{s}{s}", .{ found, name }); } return found; } test "modes: Ctrl-w outlines a pane, h j k l move the outline, and Enter, Esc, : or a pane's letter gives it the keyboard" { var s = try Session.initFile(std.testing.allocator, "left\n"); defer s.deinit(); const a, const b, const c = try threePanes(&s); const names = &[_][]const u8{ "/a.txt", "/b.txt", "/c.txt" }; try s.keys("\x17"); const x, const y = try s.where("/a.txt Save"); const lit = try s.bg(x, y); try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); // Each pane's box shows its letter: the left column's is a. try std.testing.expectEqualStrings("a /a.txt", (try s.screen())[y * 81 ..][0.."a /a.txt".len]); try s.keys("l"); try std.testing.expectEqualStrings("/b.txt", try outlined(&s, lit, names)); try s.keys("j"); try std.testing.expectEqualStrings("/c.txt", try outlined(&s, lit, names)); try expectEqual(a, try s.focused()); try s.keys("\n"); try expectEqual(c, try s.focused()); try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); // SPC w enters it too, and Esc leaves it on the outlined pane. try s.keys(" wh\x1b"); try expectEqual(a, try s.focused()); // Other keys change nothing. try s.keys("\x17!"); try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); try s.keys("\x1b"); // `:` gives the outlined pane's tag the keyboard. try s.keys("\x17l:"); try expectEqual(b, try s.focused()); try expectEqual((try s.where("/b.txt Save"))[1], (try s.cursor()).?[1]); try s.keys("\x1b"); // A letter goes straight there. try s.keys("\x17a"); try expectEqual(a, try s.focused()); try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); } fn listed(s: *Session, n: u32) !bool { var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (rows.next()) |row| if (std.mem.startsWith(u8, row, s.at("{d} ", .{n}))) return true; return false; } test "modes: in window mode d deletes the outlined pane, asking k or j between two, c collapses it, and the mode stays on" { var s = try Session.initFile(std.testing.allocator, "top\n"); defer s.deinit(); // The neighbours below are what is under test: each New right under its asker. try s.write("/ctl", "Placement pardes\n"); const top = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); const mid = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{mid}), "New\n"); const low = try s.focused(); try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); const names = &[_][]const u8{ "/test.txt", "/mid", "/low" }; const row_top = (try s.where("/test.txt Save"))[1]; const row_mid = (try s.where("/mid Save"))[1]; const row_low = (try s.where("/low Save"))[1]; // From the bottom pane, the outline up to the middle one. const mx, const my = try s.where("/mid Save"); const plain = try s.bg(mx, my); try s.keys("\x17k"); const lit = try s.bg(mx, my); try std.testing.expect(!std.mem.eql(u8, plain, lit)); try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); // c folds it to its tag and back, the mode staying. try s.keys("c"); try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") != null); try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); try s.keys("c"); try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") == null); try expectEqual(row_low, (try s.where("/low Save"))[1]); // d between two panes asks, as SPC d does; Esc takes it back, the // outline where it was. try s.keys("d\x1b"); try std.testing.expect(try listed(&s, mid)); try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); // j: the pane below takes the rows, and the outline goes with them. try s.keys("dj"); try std.testing.expect(!try listed(&s, mid)); try expectEqual(row_top, (try s.where("/test.txt Save"))[1]); try expectEqual(row_mid, (try s.where("/low Save"))[1]); try std.testing.expectEqualStrings("/low", try outlined(&s, lit, &.{ "/test.txt", "/low" })); // With one neighbour nothing is asked: d closes it, the outline on the // pane that grew. try s.keys("d"); try std.testing.expect(!try listed(&s, low)); try std.testing.expectEqualStrings("/test.txt", try outlined(&s, lit, &.{"/test.txt"})); // Enter leaves the mode on it. try s.keys("\n"); try expectEqual(top, try s.focused()); try std.testing.expectEqualStrings("", try outlined(&s, lit, &.{"/test.txt"})); } test "modes: window keys walk from a pane up through its column's tag and the workspace's, across, and back down" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const a = try s.focused(); try s.write("/ctl", "Newcol\n"); try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); try s.write("/focus", s.at("{d}\n", .{a})); const right = 40; // two columns on 80 cells: the right one's first // Up from the top pane: its column's tag, in normal mode (w moves). try s.keys("\x17k"); try s.expect("/focus", ""); const x0, const y0 = (try s.cursor()).?; try expectEqual(@as(usize, 1), y0); try std.testing.expect(x0 < right); try s.keys("w"); const remembered = (try s.cursor()).?; try std.testing.expect(remembered[0] > x0); // Across to the right column's tag, and back to where the caret was. try s.keys("\x17l"); try std.testing.expect((try s.cursor()).?[0] >= right); try s.keys("\x17h"); try expectEqual(remembered, (try s.cursor()).?); // Up again: the workspace tag; down, the column's, then the pane. try s.keys("\x17k"); try expectEqual(@as(usize, 0), (try s.cursor()).?[1]); try s.keys("\x17j"); try expectEqual(@as(usize, 1), (try s.cursor()).?[1]); try s.keys("\x17j"); try expectEqual(a, try s.focused()); // `:` in a column's tag is the way back to the body. try s.keys("\x17k:"); try expectEqual(a, try s.focused()); try expectEqual((try s.where("body"))[1], (try s.cursor()).?[1]); try s.expect(s.at("/pane/{d}/body", .{a}), "body\n"); }