//! 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); }