//! docs/typ/reference.typ, Panes . const std = @import("std"); const Session = @import("session").Session; test "panes: a collapsed pane's ctl says so" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const first = try s.focused(); _ = try s.newPane(); try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed ") != null); } test "panes: a pane's ctl read ends with its mode, as its tag's box shows it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const ctl = s.at("/pane/{d}/ctl", .{try s.focused()}); try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal ")); try s.keys("i"); try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:insert ")); try s.keys("\x1bv"); try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select ")); } test "panes: the ctl word name refuses a leading blank, as the name file does" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const ctl = s.at("/pane/{d}/ctl", .{try s.focused()}); try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name /tmp/lead.txt\n")); try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said); try s.write(ctl, "name /tmp/ok.txt\n"); try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n"); } test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" }); var dir_buf: [4096]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const first = try s.focused(); try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n"); try s.write(s.at("/pane/{d}/body", .{first}), "edited\n"); // A file opened from disk, unedited. try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir})); try std.testing.expectEqual(@as(usize, 0), s.saved); try s.write("/ctl", "Putall\n"); try std.testing.expectEqual(@as(usize, 1), s.saved); } /// `name` in `dir`: a one-page PDF with no outline and no text. fn plainPdf(dir: std.Io.Dir, name: []const u8) !void { const objects = [_][]const u8{ "<< /Type /Catalog /Pages 2 0 R >>", "<< /Type /Pages /Count 1 /Kids [3 0 R] >>", "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>", }; var buf: [1024]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); var at: [objects.len]usize = undefined; try w.writeAll("%PDF-1.4\n"); for (objects, 0..) |object, i| { at[i] = w.end; try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object }); } const xref = w.end; try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1}); for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off}); try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref }); try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); } test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); const body = s.at("/pane/{d}/body", .{n}); try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n")); try s.write(s.at("/pane/{d}/look", .{n}), ":2\n"); try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n")); } test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const text = try s.focused(); const n = try s.lookFile("docs/design.pdf"); const ctl = s.at("/pane/{d}/ctl", .{n}); const tag = try s.read(s.at("/pane/{d}/tag", .{n})); try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del")); for ([_][]const u8{ "full", "disabled", "filtered" }) |want| { try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null); try s.write(ctl, "PdfTint\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); try s.expect(s.at("/pane/{d}/tag", .{n}), tag); // Another pane's PdfTint is refused and leaves the PDF's alone. try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n")); try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); } } test "panes: PdfSections on a PDF with no outline says so and opens nothing" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try plainPdf(tmp.dir, "plain.pdf"); var buf: [4096]u8 = undefined; const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir})); try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]); const index = try s.read("/index"); for (0..2) |_| { _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); try s.expect("/index", index); } try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null); } test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" { var s = try Session.initFile(std.testing.allocator, "abc\n"); defer s.deinit(); const text = try s.focused(); const exec = s.at("/pane/{d}/exec", .{text}); try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text})); const mini = try s.focused(); try std.testing.expect(mini != text); const body = try s.read(s.at("/pane/{d}/body", .{mini})); const index = try s.read("/index"); try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n")); try std.testing.expectEqual(mini, try s.focused()); try s.expect("/index", index); try s.expect(s.at("/pane/{d}/body", .{mini}), body); }