const std = @import("std"); const Session = @import("session").Session; const home = "\u{F00D}"; const right = "\u{F012}"; const window = "\x17"; // Ctrl-w /// The pane's tag file. fn tag(s: *Session, n: u32) ![]const u8 { return s.read(s.at("/pane/{d}/tag", .{n})); } /// The column of the screen's cursor in the tag drawn starting with /// `path`, which must be on its row. fn tagCol(s: *Session, path: []const u8) !usize { const x, const y = try s.where(path); const cx, const cy = (try s.cursor()).?; try std.testing.expectEqual(y, cy); return cx - x; } test "tags: s and S in a tag answer for the tag's own text; / still searches the body" { var s = try Session.initFile(std.testing.allocator, "Some body\n"); defer s.deinit(); const n = try s.focused(); try s.replace(s.at("/pane/{d}/tag", .{n}), " Save Del Snarf"); // The tag's line, its S's selected, then changed: both are the tag's. try s.keys(":xsS\ncZ\x1b"); try std.testing.expectEqualStrings("/test.txt Zave Del Znarf", try tag(&s, n)); try s.expect(s.at("/pane/{d}/body", .{n}), "Some body\n"); // `/` from the tag is the body's search, as acme's Look from a tag is. // Its places list the body's match. try s.keys(",/body\n"); _ = try s.where("test.txt:1:6-9"); } test "tags: ms and md edit a tag's own text, and the text the edit made is read back whole" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); try s.keys(":miwms("); try std.testing.expectEqualStrings("/test.txt (Save) Term Collapse Follow Del", try tag(&s, n)); try s.keys("md("); try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del", try tag(&s, n)); } test "tags: the keyboard reaches a tag's path, which it can select and yank but not edit" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); const before = try tag(&s, n); // `0` is the line's start, as in acme, where the name is ordinary text. try s.keys(":0"); try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); // An edit that reaches into it is refused, the cursor with it. try s.keys("Eyd"); try std.testing.expectEqualStrings(before, try tag(&s, n)); try std.testing.expectEqual(@as(usize, "/test.tx".len), try tagCol(&s, "/test.txt")); // Typed into, the path drafts a name there, as a click on it does. try s.keys(";0ix"); _ = try s.where("x/test.txt"); // Esc keeps the draft pending; a second drops it. try s.keys("\x1b\x1b"); try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); // After the path, the text is the tag's own to edit. try s.keys(":A!\x1b"); try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del!", try tag(&s, n)); // What it yanked was the path. try s.keys(":P"); try s.expect(s.at("/pane/{d}/body", .{n}), "/test.txtbody\n"); } test "tags: a tag remembers its cursor, starts on Save, and a click moves it" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const first = try s.focused(); try s.write("/ctl", "Newcol\n"); const second = try s.newPane(); try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); const focus = s.at("{d}", .{first}); const other = s.at("{d}", .{second}); try s.write("/focus", focus); // Nothing to remember yet: `:` lands on Save. try s.keys(":"); try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); // Home is the line's start, the path's: the keyboard reaches it. try s.keys(home ++ right ++ right ++ right); try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); // `:` in the tag's normal mode is the way back to the body. try s.keys(":"); try std.testing.expect((try s.cursor()).?[1] != (try s.where("/test.txt"))[1]); try s.write("/focus", other); try s.keys(":" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":"); try s.write("/focus", focus); try s.keys(":"); try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); try s.keys(":"); try s.write("/focus", other); try s.keys(":"); try std.testing.expectEqual(@as(usize, 5), try tagCol(&s, "/second.txt")); try s.keys(":"); // A click in the tag puts its cursor there, the pane's keyboard with it. try s.click("Save Term", 6, 1); try std.testing.expectEqual(first, try s.focused()); try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); // A prompt opened and left from the body leaves it there (a clicked // tag is in normal mode: `:` goes back to the body). try s.keys(":/\x1b:"); try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); try s.keys(":"); try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); } test "tags: executing a word from the tag runs it and gives the keyboard back to the body" { for ([_][]const u8{ "\t", "\n" }) |chord| { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); try s.keys("ix\x1b"); // `:` lands on Save; the execute key runs it, the look key looks // it up. Either way the tag lets go of the keyboard first. try s.keys(":"); try s.keys(chord); try std.testing.expectEqual(@as(usize, if (chord[0] == '\t') 1 else 0), s.saved); try s.keys("iQ\x1b"); try s.expect(s.at("/pane/{d}/body", .{n}), "xQbody\n"); // ...and the tag still knows where it was. try s.keys(":"); try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); } } test "tags: a renamed path moves the tag's cursor with the text after it and a shorter tag pulls it back" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); try s.keys("::"); // on Save, and back try s.write(s.at("/pane/{d}/name", .{n}), "/界.zig"); try s.keys(":"); try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); try s.keys(":"); try s.replace(s.at("/pane/{d}/tag", .{n}), "é"); try s.keys(":"); // `/界.zig é`: the cursor is pulled back onto the last character. try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); } test "tags: window keys move between panes from a tag and each tag keeps its cursor" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const first = try s.focused(); const second = try s.newPane(); try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); try s.write("/focus", s.at("{d}", .{first})); try s.keys(":" ++ home ++ "ll" ++ window ++ "j\n"); try std.testing.expectEqual(second, try s.focused()); try s.keys(":" ++ home ++ "llll" ++ window ++ "k\n"); try std.testing.expectEqual(first, try s.focused()); try s.keys(":"); try std.testing.expectEqual(@as(usize, 2), try tagCol(&s, "/test.txt")); try s.keys(window ++ "j\n:"); try std.testing.expectEqual(@as(usize, 4), try tagCol(&s, "/second.txt")); } test "tags: a tag's normal mode is the body's: motions stay in the tag and insert types into it" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const first = try s.focused(); try s.write("/ctl", "Newcol\n"); const right_pane = try s.newPane(); try s.write("/focus", s.at("{d}", .{first})); try s.keys(":" ++ home ++ "l"); try std.testing.expectEqual(@as(usize, 1), try tagCol(&s, "/test.txt")); try s.keys("hkj"); try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); try std.testing.expectEqual(first, try s.focused()); try s.keys(window ++ "l\n"); try std.testing.expectEqual(right_pane, try s.focused()); try s.keys(window ++ "h\n"); try std.testing.expectEqual(first, try s.focused()); try s.keys(":i\u{F018}hjklHJKL:"); try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow DelhjklHJKL:", try tag(&s, first)); try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " mode:normal ")); try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); try std.testing.expectEqual(first, try s.focused()); } test "tags: a tag with a newline in it is as tall as its lines, and each line is typed into and pointed at" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); // Insert mode in a tag is a body's: Enter is a new line. try s.keys(":A\nSecond"); try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del\nSecond", try tag(&s, n)); const x, const y = try s.where("/test.txt"); try std.testing.expectEqual(.{ x, y + 1 }, try s.where("Second")); // The caret is on the second line, after what was typed. try std.testing.expectEqual(.{ x + 6, y + 1 }, (try s.cursor()).?); // The body starts under the whole tag. const by = (try s.where("1 body"))[1]; try std.testing.expect(by == y + 2); // A click on the second line puts the tag's cursor on it. try s.keys("\x1b:"); try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2, y + 1, x + 2, y + 1 })); try std.testing.expectEqual(.{ x + 2, y + 1 }, (try s.cursor()).?); // A clicked tag is in normal mode: `i` types where it was clicked. try s.keys("iX"); try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del\nSeXcond", try tag(&s, n)); try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); } test "tags: a tag with more lines than rows scrolls to keep its caret in view" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); var many: std.ArrayList(u8) = .empty; defer many.deinit(std.testing.allocator); for (1..21) |i| try many.print(std.testing.allocator, "\nL{d}", .{i}); try s.write(s.at("/pane/{d}/tag", .{n}), many.items); const x, const y = try s.where("/test.txt"); // To the last line: the tag scrolls so it is the bottom row, caret on it. try s.keys(":ge"); const lx, const ly = try s.where("L20"); try std.testing.expectEqual(.{ lx, ly }, (try s.cursor()).?); // What the pointer sees is what is shown: its first row is line 13. try std.testing.expectEqual(.{ x, y }, try s.where("L13")); try std.testing.expectError(error.NotOnScreen, s.where("L12")); try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y + 1, x + 1, y + 1 })); try s.keys("iX"); _ = try s.where("LX14"); // Back to the first line brings the prefix back into view. try s.keys("\x1bgg"); try std.testing.expectEqual(.{ x, y }, try s.where("/test.txt")); try std.testing.expectEqual(y, (try s.cursor()).?[1]); // A column tag is one row, as acme's: its lines are joined. var layout = std.mem.tokenizeScalar(u8, try s.read("/layout"), ' '); const col_tag = s.at("/col/{s}/tag", .{layout.next().?}); try s.replace(col_tag, "New\nC1\nC2"); try std.testing.expect(std.mem.indexOfScalar(u8, try s.read(col_tag), '\n') == null); try std.testing.expectEqual(y, (try s.where("/test.txt"))[1]); } test "tags: a long pane tag wraps onto rows, and a click on a wrapped row maps to the whole tag's address" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); s.detach(); try s.write("/ctl", "size 50 12\n"); const n = try s.focused(); try s.write(s.at("/pane/{d}/name", .{n}), "/an/especially/deep/project/path/with/a/long/component/filename.zig"); try s.write(s.at("/pane/{d}/tag", .{n}), " JumpTrail"); const before = try tag(&s, n); try s.keys(":A"); try std.testing.expect((try s.cursor()).?[0] < 50); // JumpTrail is on a row of its own, below the path's. try std.testing.expect((try s.where("JumpTrail"))[1] > (try s.where("/an/"))[1]); // Its last character, which a wrapped row's offset not mapped back misses. try s.click("JumpTrail", "JumpTrai".len, 2); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "JumpTrail on\n") != null); try std.testing.expectEqualStrings(before, try tag(&s, n)); } test "tags: a word the tag's wrapping breaks across rows is still one word to a click" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); s.detach(); try s.write("/ctl", "size 50 12\n"); const n = try s.focused(); // Lengthen the path until the wrap splits the word after "Jump" (a // rename resets the tag's text, so it is written again each time). var pad: usize = 1; while (pad < 80) : (pad += 1) { try s.write(s.at("/pane/{d}/name", .{n}), s.at("/{s}/f.zig", .{(&@as([80]u8, @splat('a')))[0..pad]})); try s.write(s.at("/pane/{d}/tag", .{n}), " JumpTrail"); var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); const split = while (rows.next()) |row| { if (std.mem.endsWith(u8, std.mem.trimEnd(u8, row, " "), "Jump")) break true; } else false; if (split) break; } else return error.NoSplit; try s.click("Jump", 1, 2); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "JumpTrail on\n") != null); } /// A click on the tag's path, at its last character: a name drafted /// there, its caret taken to its end. fn clickPathEnd(s: *Session, path: []const u8) !void { try s.click(path, path.len - 1, 1); try s.keys("\u{F018}"); } test "tags: a name drafted in the tag commits with Enter, cancels with two Escs, and changes neither body nor tag text" { var s = try Session.initFile(std.testing.allocator, "original body\n"); defer s.deinit(); const n = try s.focused(); const name = s.at("/pane/{d}/name", .{n}); try s.write(s.at("/pane/{d}/tag", .{n}), " JumpTrail"); for ([_]bool{ false, true }) |commit| { const path = std.mem.trimEnd(u8, try s.read(name), "\n"); try clickPathEnd(&s, path); try s.keys("\x15/new name.zig"); try s.expect(name, s.at("{s}\n", .{path})); _ = try s.where("/new name.zig Save Term Collapse Follow Del JumpTrail"); try s.expect(s.at("/pane/{d}/body", .{n}), "original body\n"); // Esc keeps the draft pending; a second drops it. try s.keys(if (commit) "\n" else "\x1b\x1b"); try s.expect(name, if (commit) "/new name.zig\n" else "/test.txt\n"); try std.testing.expectEqualStrings(s.at("{s} Save Term Collapse Follow Del JumpTrail", .{if (commit) "/new name.zig" else "/test.txt"}), try tag(&s, n)); // The keyboard is the body's again. try std.testing.expect((try s.cursor()).?[1] != (try s.where("Save Term"))[1]); } try std.testing.expectEqual(@as(usize, 0), s.saved); // A name alone is no edit: the pane stays clean. try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); } test "tags: a drafted name's edits are by grapheme, and an empty one is not committed" { var s = try Session.initFile(std.testing.allocator, "untouched\n"); defer s.deinit(); const n = try s.focused(); try clickPathEnd(&s, "/test.txt"); try s.keys("\x15/café界\x08"); _ = try s.where("/café Save"); try s.keys("\x08"); _ = try s.where("/caf Save"); try s.keys("\x15\n"); try std.testing.expectError(error.NotOnScreen, s.where("/test.txt")); try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); // Esc keeps the draft pending; a second drops it. try s.keys("\x1b"); try std.testing.expectError(error.NotOnScreen, s.where("/test.txt")); try s.keys("\x1b"); _ = try s.where("/test.txt"); } test "tags: leaving a pane or entering a header drops an uncommitted name" { for ([_]bool{ false, true }) |header| { var s = try Session.initFile(std.testing.allocator, "untouched\n"); defer s.deinit(); const n = try s.focused(); try clickPathEnd(&s, "/test.txt"); try s.keys("\x15/draft"); _ = try s.where("/draft"); if (header) try s.click("Newcol", 1, 1) else _ = try s.newPane(); try std.testing.expectError(error.NotOnScreen, s.where("/draft")); try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); } } test "tags: middle clicking Save commits the drafted name before saving" { var s = try Session.initFile(std.testing.allocator, "untouched\n"); defer s.deinit(); const n = try s.focused(); try clickPathEnd(&s, "/test.txt"); try s.keys("\x15/renamed.txt"); try s.click("Save Term", 0, 2); try s.expect(s.at("/pane/{d}/name", .{n}), "/renamed.txt\n"); try std.testing.expectEqualStrings("/renamed.txt Save Term Collapse Follow Del", try tag(&s, n)); try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); try std.testing.expectEqual(@as(usize, 1), s.saved); } test "tags: an oversized drafted name is refused at commit and the draft stays to correct" { var s = try Session.initFile(std.testing.allocator, "untouched\n"); defer s.deinit(); const n = try s.focused(); try clickPathEnd(&s, "/test.txt"); const excess = s.at("\x15/{s}\n", .{&@as([4999]u8, @splat('x'))}); try s.keys(excess); try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); _ = try s.where("/xxxx"); } test "tags: a name typed into the tag's path survives Esc, pending, and the next Enter renames; a second Esc drops it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.focused(); const name = s.at("/pane/{d}/name", .{n}); // A click on the path types into it; a stray key while it waits // changes nothing. try s.click("/test.txt", 1, 1); try s.keys("z\x1bq\n"); try s.expect(name, "/ztest.txt\n"); try s.click("/ztest.txt", 1, 1); try s.keys("z\x1bq\x1b"); try s.expect(name, "/ztest.txt\n"); try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "/ztest.txt ")); } test "tags: a click on a tag puts its cursor there in normal mode: a letter is a command, i types where it was clicked" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); const was = try tag(&s, n); try s.click("Save", 0, 1); // `w` moves to the next word, as in a body's normal mode: nothing typed. try s.keys("w"); try std.testing.expectEqualStrings(was, try tag(&s, n)); // Clicked again: `i` types where the click put the cursor. try s.click("Save", 2, 1); try s.keys("iQ\x1b"); try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), " SaQve ") != null); try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); } test "tags: a pane's tag reads whole, its path first, and a write appends to its own text up to 4096 bytes" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.focused(); const path = s.at("/pane/{d}/tag", .{n}); const whole = try tag(&s, n); try std.testing.expect(std.mem.startsWith(u8, whole, "/test.txt")); try std.testing.expect(std.mem.indexOf(u8, whole, "Del") != null); try std.testing.expectEqual(@as(u64, whole.len), (try s.stat(path)).size); try s.write(path, " Mine"); try std.testing.expectEqualStrings(s.at("{s} Mine", .{whole}), try tag(&s, n)); // A tag is a text like the body: a newline written to it stays one. try s.write(path, "\nNext Line"); try std.testing.expectEqualStrings(s.at("{s} Mine\nNext Line", .{whole}), try tag(&s, n)); // Full, it takes not a byte more. try s.replace(path, &@as([4096]u8, @splat('x'))); try std.testing.expectEqual(@as(u16, 28), try s.refused(path, "x")); } test "tags: truncating a tag clears its own text, u in the tag undoes it, and > drops the one newline ending it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); const n = try s.focused(); const path = s.at("/pane/{d}/tag", .{n}); try s.write(path, " Mine"); // Done at the first read or write after it (or the close). const h = try s.hold(path, 1); try s.truncate(h); try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), "Mine") == null); s.clunk(h); try std.testing.expectEqualStrings("/test.txt", std.mem.trimEnd(u8, try tag(&s, n), " ")); // The tag keeps its undo: undoing in it brings back what was cleared. try s.keys(":u:"); try std.testing.expect(std.mem.endsWith(u8, try tag(&s, n), " Mine")); // Written back without a leading space, the text still stands apart // from the path, and a click on its first letter lands on it. try s.replace(path, "Get"); try std.testing.expectEqualStrings("/test.txt Get", try tag(&s, n)); try s.click("Get", 0, 1); try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); // `echo Make > tag`: the newline ending the truncating write's text is // dropped, one only; a newline between its writes stays. const m = try s.hold(path, 1); try s.truncate(m); _ = try s.writeFid(m, "Make\n"); _ = try s.writeFid(m, "More\n\n"); s.clunk(m); try std.testing.expectEqualStrings("/test.txt Make\nMore\n", try tag(&s, n)); // `>>` keeps what it is given. try s.write(path, " Tail\n"); try std.testing.expectEqualStrings("/test.txt Make\nMore\n Tail\n", try tag(&s, n)); } test "tags: a pane tag's file name is tinted, its directory, dirty marker and words not" { var s = try Session.initFile(std.testing.allocator, "body\n"); defer s.deinit(); const n = try s.focused(); try s.write(s.at("/pane/{d}/name", .{n}), "/project/界file.zig\n"); try s.write(s.at("/pane/{d}/dirty", .{n}), "1"); const x0, const y = try s.where("/project/"); const screen = try s.screen(); const styles = try s.styles(); const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); var rows = std.mem.splitScalar(u8, screen, '\n'); for (0..y) |_| _ = rows.next(); var cells = (try std.unicode.Utf8View.init(rows.next().?)).iterator(); var x: usize = 0; var tinted: ?[]const u8 = null; var ink: ?[]const u8 = null; while (cells.nextCodepointSlice()) |cell| : (x += 1) { if (x < x0 or std.mem.eql(u8, cell, " ")) continue; // `界file.zig`: the wide glyph's two cells, then eight. const in_name = x >= x0 + "/project/".len and x < x0 + "/project/".len + 10; const want = if (in_name) &tinted else &ink; if (want.* == null) want.* = styles[y * cols + x]; try std.testing.expectEqualStrings(want.*.?, styles[y * cols + x]); } try std.testing.expect(!std.mem.eql(u8, tinted.?, ink.?)); }