//! docs/typ/reference.typ, look and exec . const std = @import("std"); const Session = @import("session").Session; const expectEqual = std.testing.expectEqual; test "look-and-exec: a 9P look keeps its leading blanks, a blank-only look included, and exec still trims" { var s = try Session.initFile(std.testing.allocator, "a indented\nb\n indented\n \n"); defer s.deinit(); const n = try s.focused(); const dot = s.at("/pane/{d}/dot", .{n}); try s.write(dot, "0 0"); // The indented line, not the `indented` after `a `. try s.write(s.at("/pane/{d}/look", .{n}), " indented\n"); try expectEqual(.{ @as(u32, 13), @as(u32, 25) }, try s.range(dot)); // Blanks alone are text to find too. try s.write(dot, "26 26"); try s.write(s.at("/pane/{d}/look", .{n}), " \n"); try expectEqual(.{ @as(u32, 26), @as(u32, 30) }, try s.range(dot)); // exec's line is still trimmed: ` Msg hi` runs Msg. try s.write(s.at("/pane/{d}/exec", .{n}), " Msg blanks trimmed\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "blanks trimmed") != null); } test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its look, the root's and event write-back" { var s = try Session.initFile(std.testing.allocator, "alpha\nbeta\ngamma\n"); defer s.deinit(); const n = try s.focused(); const dot = s.at("/pane/{d}/dot", .{n}); try s.write(s.at("/pane/{d}/look", .{n}), ":2\n"); try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot)); try s.write("/look", ":3\n"); try expectEqual(.{ @as(u32, 11), @as(u32, 17) }, try s.range(dot)); try s.write(s.at("/pane/{d}/look", .{n}), ":3:2\n"); try expectEqual(@as(u32, 12), (try s.range(dot))[0]); // Written back by an event reader, the same. try s.write(s.at("/pane/{d}/event", .{n}), "ML0 0 0 2 :1\n"); try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot)); } /// The log's `err` records, counted. fn errs(s: *Session) !usize { return std.mem.count(u8, try s.read("/log"), "\nerr "); } /// The serial of the pane /index names with `suffix`. fn paneNamed(s: *Session, suffix: []const u8) !u32 { var lines = std.mem.splitScalar(u8, try s.read("/index"), '\n'); while (lines.next()) |line| { // serial kind dirty name column const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10); } return error.NoSuchPane; } test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); const look = s.at("/pane/{d}/look", .{n}); const pages = (try s.pdfPage(n))[1]; try std.testing.expect(pages >= 3); try s.write(look, ":2\n"); try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); try s.write(look, s.at("@p{d}:1\n", .{n})); try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); try s.write("/look", s.at("@p{d}:2\n", .{n})); try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); try expectEqual(@as(usize, 0), try errs(&s)); // `:N:H` is hit H of its search on page N, as a search row's // `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns // no page. try s.write(look, ":1\n"); _ = try s.refused(look, ":2:3\n"); try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null); try s.write(look, ":2\n"); _ = try s.refused(look, ":0\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null); // A byte offset or a pattern is no PDF address: refused with words. _ = try s.refused(look, ":#3\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null); const before = try errs(&s); _ = try s.refused(look, s.at(":{d}\n", .{pages + 1})); try expectEqual(before + 1, try errs(&s)); try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); } test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var buf: [4096]u8 = undefined; const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{}); var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); const look = s.at("/pane/{d}/look", .{n}); const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); // Open: it stays on its page. try s.write(look, ":2\n"); _ = try s.refused(look, s.at("{s}:999\n", .{name})); try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null); // Not open: nothing opens. const index = try s.read("/index"); _ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir})); try s.expect("/index", index); } test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.lookFile("docs/design.pdf"); try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); const out = try paneNamed(&s, "/+PdfSections"); const body = try s.read(s.at("/pane/{d}/body", .{out})); const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?]; const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S var fields = std.mem.splitScalar(u8, place, ':'); _ = fields.next(); const page = try std.fmt.parseInt(u32, fields.next().?, 10); const look = s.at("/pane/{d}/look", .{out}); // As listed: found, no miss. try s.write(look, s.at("{s}\n", .{place})); try expectEqual(@as(usize, 0), try errs(&s)); // The section on another page than the row says, and one not there. _ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1})); try expectEqual(@as(usize, 1), try errs(&s)); _ = try s.refused(look, "design.pdf:1:999\n"); try expectEqual(@as(usize, 2), try errs(&s)); }