//! docs/typ/reference.typ, Other files . const std = @import("std"); const Session = @import("session").Session; test "other-files: kbdin types at the pane with the keyboard and mousein clicks, each done before its write is answered" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const body = try std.fmt.allocPrint(s.arena.allocator(), "/pane/{d}/body", .{try s.focused()}); try s.keys("ihello \x1b"); try s.expect(body, "hello x\n"); // Plan 9's Khome rune, then text before it. try s.keys("A!\x1b\u{F00D}i>\x1b"); try s.expect(body, ">hello x!\n"); // A left click on the second l, then text there. try s.click(">hello", 4, 1); try s.keys("i#\x1b"); try s.expect(body, ">hel#lo x!\n"); // Refused, in words, as rio and devmouse refuse. try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "m 1 2\n")); try std.testing.expectEqual(@as(u16, 22), try s.refused("/mousein", "x 1 2 0\n")); try std.testing.expectEqual(@as(u16, 22), try s.refused("/kbdin", "\xff")); try std.testing.expectError(error.Refused, s.read("/kbdin")); } test "other-files: kbdin's Kalt rune is Alt on the key after it alone, and an Alt key types nothing" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); try s.keys("i\u{F015}zy\x1b"); try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "yx\n"); } test "other-files: pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile, and refuses a directory it cannot page in" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const asked = try s.hold("/pager", 2); try std.testing.expectEqual(@as(u16, 0), try s.writeFid(asked, "/tmp\n")); // Another client execs between the request and its read: a pane of its own. const racer = try s.hold("/exec", 2); try std.testing.expectEqual(@as(u16, 0), try s.writeFid(racer, "New\n")); s.clunk(racer); const paged = try s.readFid(asked); s.clunk(asked); // A fresh open reads the session's last answer, the racer's pane. try std.testing.expect(!std.mem.eql(u8, paged, try s.read("/exec"))); try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); // Refused, an open answers nothing, not the session's last answer. const refused = try s.hold("/pager", 2); try std.testing.expectEqual(@as(u16, 22), try s.writeFid(refused, "relative\n")); try std.testing.expectEqualStrings("", try s.readFid(refused)); s.clunk(refused); // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the // one made above, which keeps its page. const resolved = try s.hold("/pager", 2); try std.testing.expectEqual(@as(u16, 0), try s.writeFid(resolved, "/tmp/../tmp/.\n")); const second = try s.readFid(resolved); s.clunk(resolved); try std.testing.expect(!std.mem.eql(u8, paged, second)); try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, second, "\n")}), "/tmp/+Pager\n"); try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); // One that may not be written is refused, permission denied (Linux's /proc/1). try std.testing.expectEqual(@as(u16, 1), try s.refused("/pager", "/proc/1\n")); // One directory an open: one refused is not followed by another, as a // mount sends two lines in two writes. const twice = try s.hold("/pager", 2); try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "relative\n")); try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "/tmp\n")); s.clunk(twice); try std.testing.expectEqual(@as(u16, 22), try s.refused("/pager", "relative\n")); try std.testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", s.said); try std.testing.expectEqual(@as(u16, 2), try s.refused("/pager", "/tmp/pardes-surely-missing-dir\n")); try std.testing.expectEqual(@as(u16, 20), try s.refused("/pager", "/etc/passwd\n")); } test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("open {s}\n", .{name})) != null); try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("closed {s}\n", .{name})) != null); } fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } /// A fresh directory's absolute path, kept until deinit. fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { var buf: [4096]u8 = undefined; return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); } /// The serials /index lists with a name ending `suffix`. fn named(s: *Session, suffix: []const u8) ![]u32 { var out: std.ArrayList(u32) = .empty; var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); while (rows.next()) |line| { const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10)); } return out.items; } /// The body of the pane a root exec of `word` answers (Recent, Jumplist). fn listing(s: *Session, word: []const u8) ![]const u8 { try s.write("/exec", s.at("{s}\n", .{word})); return s.read(s.at("/pane/{s}/body", .{std.mem.trimEnd(u8, try s.read("/exec"), "\n")})); } test "other-files: Jumplist fills its one +Jumps again, as Recent does, never a second" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); for (0..2) |_| { try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Jumplist\n"); try std.testing.expectEqual(@as(usize, 1), (try named(&s, "/+Jumps")).len); } } test "other-files: a Jumplist entry is (closed) only while no pane has its file open" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "two\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); try s.write("/look", s.at("{s}/a.txt\n", .{dir})); const a = try s.focused(); try s.write("/look", s.at("{s}/b.txt\n", .{dir})); try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)")); // Open again, by a look: no entry is closed any more. try s.write("/look", s.at("{s}/a.txt\n", .{dir})); try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)")); } test "other-files: Recent's row for a file still open is where its dot is now, not where it last closed" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); try s.write("/look", s.at("{s}/a.txt:3\n", .{try tmpPath(&s, &tmp)})); try s.write(s.at("/pane/{d}/dot", .{try s.focused()}), "6 6"); // line 2, column 3 // A query pane under the file (line 1 `Recent `), its rows said from // its directory. const rows = try listing(&s, "Recent"); try std.testing.expect(std.mem.startsWith(u8, rows, "Recent \n")); try std.testing.expect(has(rows, "\na.txt:2:3 open\n")); } test "other-files: a closed file is found again: Recent and /recent list it closed, a look at its row reopens it at its dot, and a jump to it does too" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); const a = s.at("{s}/a.txt", .{dir}); // Opened, its dot on line 3, then closed. try s.write("/look", s.at("{s}:3\n", .{a})); const opened = try s.focused(); try std.testing.expect(has(try s.read("/recent"), s.at("open {s}\n", .{a}))); // Another file, so the jumplist has somewhere to come back from. try s.write("/look", s.at("{s}/b.txt\n", .{dir})); try s.write(s.at("/pane/{d}/ctl", .{opened}), "Del\n"); try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}\n", .{a}))); // Recent lists it, closed, at its dot; a look at the row opens it there. const rows = try listing(&s, "Recent"); const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n"); const at = std.mem.indexOf(u8, rows, "\na.txt:3:").? + 1; const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?]; try std.testing.expect(std.mem.endsWith(u8, row, " (closed)")); try s.write(s.at("/pane/{s}/look", .{recent}), s.at("{s}\n", .{row[0 .. row.len - " (closed)".len]})); const back = try s.focused(); try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}\n", .{a})); try std.testing.expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{back})))[0]); // Closed again; the jumplist keeps its entry, closed, and a jump back // to it opens it again. try s.write(s.at("/pane/{d}/ctl", .{back}), "Del\n"); try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)")); // The text pane closed too, the keyboard is the +Jumps listing's, // which has no file to keep to: Back takes the nearest place, the // closed a.txt's. try s.write(s.at("/pane/{d}/ctl", .{(try named(&s, "/test.txt"))[0]}), "Del\n"); try s.write("/exec", "Back\n"); try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), s.at("{s}\n", .{a})); } test "other-files: two sessions on one state file share Recent: what one opened and closed is in the next one's, and each reads the other's on use" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\nsea\n" }); var dir_buf: [std.fs.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var one = try Session.initShared(std.testing.allocator, dir); defer one.deinit(); const a = one.at("{s}/a.txt", .{dir}); try one.write("/look", one.at("{s}:3\n", .{a})); try one.write(one.at("/pane/{d}/ctl", .{try one.focused()}), "Del\n"); // A session started after it lists the file closed, at its dot. var two = try Session.initShared(std.testing.allocator, dir); defer two.deinit(); try std.testing.expect(has(try two.read("/recent"), two.at("closed {s}\n", .{a}))); try std.testing.expect(has(try listing(&two, "Recent"), "a.txt:3:")); // What it opens and closes is in the first one's, read there on use. const b = two.at("{s}/b.txt", .{dir}); try two.write("/look", two.at("{s}:2\n", .{b})); try std.testing.expect(has(try two.read("/recent"), two.at("open {s}\n", .{b}))); try two.write(two.at("/pane/{d}/ctl", .{try two.focused()}), "Del\n"); try std.testing.expect(has(try one.read("/recent"), one.at("closed {s}\n", .{b}))); try std.testing.expect(has(try one.read("/recent"), one.at("closed {s}\n", .{a}))); try std.testing.expect(has(try listing(&one, "Recent"), "b.txt:2:")); } test "other-files: Back and Forward over a closed file's entries: one reopen is that file's pane in every entry, so the next step moves" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "sea\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); try s.write("/look", s.at("{s}/b.txt\n", .{dir})); const b = try s.focused(); // Two entries for a.txt, far apart in it. try s.write("/look", s.at("{s}/a.txt:1\n", .{dir})); const a = try s.focused(); try s.write("/look", s.at("{s}/a.txt:90\n", .{dir})); try s.write("/look", s.at("{s}/c.txt\n", .{dir})); const c = try s.focused(); try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); try s.write("/focus", s.at("{d}\n", .{c})); try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try listing(&s, "Jumplist"), "(closed)")); try s.write("/focus", s.at("{d}\n", .{c})); // Back reopens it at line 90: both its entries are that pane now. try s.write("/exec", "Back\n"); const re = try s.focused(); try s.expect(s.at("/pane/{d}/name", .{re}), s.at("{s}/a.txt\n", .{dir})); try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)")); try s.write("/focus", s.at("{d}\n", .{re})); // Back again goes to its line 1, in the same pane, then to b. try s.write("/exec", "Back\n"); try std.testing.expectEqual(re, try s.focused()); try std.testing.expectEqual(@as(u32, 0), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); try s.write("/exec", "Back\n"); try std.testing.expectEqual(b, try s.focused()); // And Forward walks it all again, to c. try s.write("/exec", "Forward\n"); try s.write("/exec", "Forward\n"); try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); try s.write("/exec", "Forward\n"); try std.testing.expectEqual(c, try s.focused()); } test "other-files: Recent keeps only files on disk: not a name never saved, nor the served tree's" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "real.txt", .data = "real\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); const names = [_][]const u8{ s.at("{s}/c", .{dir}), "/virtual/zz", s.at("{s}/real.txt", .{dir}) }; for (names) |name| { const made = try s.newPane(); try s.write(s.at("/pane/{d}/name", .{made}), s.at("{s}\n", .{name})); // Asked once if it is a file with text to lose; gone either way. _ = try s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n"); _ = s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n") catch 0; } const recent = try s.read("/recent"); try std.testing.expect(!has(recent, s.at(" {s}\n", .{names[0]}))); try std.testing.expect(!has(recent, " /virtual/zz\n")); try std.testing.expect(has(recent, s.at(" {s}\n", .{names[2]}))); } test "other-files: Back over a closed file deleted since skips it, says so, and goes on to the place before" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); for ([_][]const u8{ "a.txt", "gone.txt", "c.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "text\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); try s.write("/look", s.at("{s}/a.txt\n", .{dir})); const a = try s.focused(); try s.write("/look", s.at("{s}/gone.txt\n", .{dir})); const gone = try s.focused(); try s.write("/look", s.at("{s}/c.txt\n", .{dir})); const c = try s.focused(); try s.write(s.at("/pane/{d}/ctl", .{gone}), "Del\n"); try tmp.dir.deleteFile(std.testing.io, "gone.txt"); try s.write("/focus", s.at("{d}\n", .{c})); try s.write("/exec", "Back\n"); try std.testing.expectEqual(a, try s.focused()); try std.testing.expect(has(try s.read("/log"), "gone.txt: deleted, skipped")); } test "other-files: a remembered place is clamped to the file as it is now, its selection's two ends kept where they fit" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\nthree\nfour\nfive\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); try s.write("/look", s.at("{s}/c.txt:2\n", .{dir})); const c = try s.focused(); // A selection over lines 2 to 4, then closed. try s.write(s.at("/pane/{d}/dot", .{c}), "4 19"); try s.write(s.at("/pane/{d}/ctl", .{c}), "Del\n"); // The file shrinks to two lines while it is closed. try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\n" }); const rows = try listing(&s, "Recent"); const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n"); const at = std.mem.indexOf(u8, rows, "c.txt:").?; const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?]; // Both ends are in the row, a range. try std.testing.expect(std.mem.indexOfScalar(u8, row, '-') != null); const loc = s.at("{s}/{s}\n", .{ dir, row[0..std.mem.indexOfScalar(u8, row, ' ').?] }); // A look at it opens the file, never a miss, clamped to its last line. try s.write(s.at("/pane/{s}/look", .{recent}), loc); const back = try s.focused(); try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}/c.txt\n", .{dir})); // Its ends clamped to the last character, `two`'s `o`, not its newline. const dot = try s.range(s.at("/pane/{d}/dot", .{back})); try std.testing.expect(dot[0] < dot[1] and dot[1] <= 7); try std.testing.expect(has(try s.read("/log"), "c.txt has 2 lines now: at its last")); // The same row with the file open: the same place, said the same way. try s.write(s.at("/pane/{s}/look", .{recent}), loc); try std.testing.expectEqual(back, try s.focused()); try std.testing.expectEqual(dot, try s.range(s.at("/pane/{d}/dot", .{back}))); } test "other-files: /index and /recent show a name's own backslash doubled, so `\\n` written in a name reads back unlike a newline" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "back\\nslash", .data = "x\n" }); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const dir = try tmpPath(&s, &tmp); const n = try s.focused(); try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/back\\nslash\n", .{dir})); const index = try s.read("/index"); try std.testing.expect(has(index, s.at("{s}/back\\\\nslash ", .{dir}))); try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, "\n")); try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}/back\\\\nslash\n", .{dir}))); }