//! docs/typ/reference.typ, Placement . const std = @import("std"); const Session = @import("session").Session; test "placement: an empty active column takes the new pane whole, a look from another column's text too" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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, "x\n"); defer s.deinit(); const first = try s.focused(); // Its tag has the keyboard: it is the active column, and stays so // when Esc gives the keyboard back. try s.write("/ctl", "Newcol\n"); try s.keys("\x1b"); try std.testing.expectEqual(first, try s.focused()); // A look from the first pane's text still lands in the active column, // as acme's makenewwindow puts it in activecol first (util.c:456). try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt\n", .{at})); const made = try s.focused(); try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), first, try s.column(1), made }), try s.columns()); } /// The screen row the tag starting `name` is drawn on. fn tagRow(s: *Session, name: []const u8) !usize { return (try s.where(s.at("{s} Save", .{name})))[1]; } test "placement: New and 9P's new take the bottom half of the column's last pane" { 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}/exec", .{first}), "New\n"); const last = try s.focused(); try s.write(s.at("/pane/{d}/name", .{last}), "/last\n"); const was = try tagRow(&s, "/last"); const made = try s.newPane(); try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), first, last, made }), try s.columns()); // The last pane keeps its top and gives up its bottom half. try std.testing.expectEqual(was, try tagRow(&s, "/last")); const rows = std.mem.count(u8, try s.screen(), "\n") + 1; try std.testing.expect(try tagRow(&s, "/made") >= was + (rows - was) / 2); } test "placement: a look from a pane pane/new made after Delcol opens its file in a column" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); 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 first = try s.focused(); try s.write(s.at("/pane/{d}/exec", .{first}), "Delcol\n"); const made = try s.newPane(); try s.write(s.at("/pane/{d}/look", .{made}), s.at("{s}/f.txt:#4\n", .{dir})); const opened = try s.focused(); try std.testing.expect(opened != made); // The pane pane/new made is placed, and so is the one its look opened. const cols = try s.columns(); for ([_]u32{ made, opened }) |n| try std.testing.expect(std.mem.indexOf(u8, cols, s.at(":{d}", .{n})) != null or std.mem.indexOf(u8, cols, s.at(",{d}", .{n})) != null); } /// Each pane's tag row and its rows down to the next tag, or the screen's /// end, as /screen draws them; `names` top to bottom in one column. fn spans(s: *Session, comptime n: usize, names: [n][]const u8) ![n][2]usize { var out: [n][2]usize = undefined; for (names, 0..) |name, i| out[i][0] = try tagRow(s, name); const rows = std.mem.count(u8, try s.screen(), "\n") + 1; for (0..n) |i| out[i][1] = (if (i + 1 < n) out[i + 1][0] else rows) - out[i][0]; return out; } test "placement: a look from a pane's text goes under its text when it has many blank rows, else halves the biggest pane" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); for ([_][]const u8{ "second.txt", "third.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\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, "one\ntwo\nthree\n"); defer s.deinit(); s.detach(); // Wide enough that a tag with the test's temporary path stays one row, // wherever the checkout is. try s.write("/ctl", "size 200 60\n"); const top = try s.focused(); // Most of the pane is blank: the look starts under its three lines // (makenewwindow, util.c:482-486). try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/second.txt\n", .{at})); const second = try s.focused(); const shown = s.at("{s}/second.txt", .{at}); try std.testing.expectEqual((try tagRow(&s, "/test.txt")) + 1 + 3, try tagRow(&s, shown)); // Both full of text: no blank space is big, so the biggest is halved. const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); for ([_]u32{ top, second }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); const was = try spans(&s, 2, .{ "/test.txt", shown }); const big: usize = if (was[1][1] >= was[0][1]) 1 else 0; try s.write(s.at("/pane/{d}/look", .{second}), s.at("{s}/third.txt\n", .{at})); const third = try s.focused(); const ids = [_]u32{ top, second }; try std.testing.expect(std.mem.indexOf(u8, try s.columns(), s.at("{d},{d}", .{ ids[big], third })) != null); const made = s.at("{s}/third.txt", .{at}); const now = if (big == 1) try spans(&s, 3, .{ "/test.txt", shown, made }) else try spans(&s, 3, .{ "/test.txt", made, shown }); try std.testing.expectEqual(was[big][0] + was[big][1] / 2, now[big + 1][0]); try std.testing.expectEqual(was[big][0] + was[big][1], now[big + 1][0] + now[big + 1][1]); } test "placement: a look from the middle of a column splits only the pane it came from, short ones round it kept short, through a Newcol too" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); for ([_][]const u8{ "mid", "low", "made" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\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, "one\n"); defer s.deinit(); s.detach(); try s.write("/ctl", "size 100 60\n"); const top = try s.focused(); try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/mid\n", .{at})); const mid = try s.focused(); try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/low\n", .{at})); const low = try s.focused(); const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); for ([_]u32{ top, mid, low }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); // Short names, so a narrowed column draws each tag on one row. try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); const names = [_][]const u8{ "/test.txt", "/mid", "/low", "/made" }; // The top and bottom panes dragged by their boxes to two and three // rows, under a placement's minimum; the middle one full of text, so // the one halved. const rows = std.mem.count(u8, try s.screen(), "\n") + 1; const r = try spans(&s, 3, names[0..3].*); try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[1][0], r[0][0] + 2, r[0][0] + 2 })); try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[2][0], rows - 3, rows - 3 })); const was = try spans(&s, 3, names[0..3].*); try std.testing.expectEqual(@as(usize, 2), was[0][1]); try std.testing.expectEqual(@as(usize, 3), was[2][1]); try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/made\n", .{at})); const made = try s.focused(); try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d},{d}", .{ try s.column(0), top, mid, made, low }), try s.columns()); const now = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); try std.testing.expectEqual(was[0], now[0]); try std.testing.expectEqual(was[2], now[3]); // A new column narrows this one, and the short panes keep their rows. try s.write(s.at("/pane/{d}/exec", .{made}), "Newcol\n"); try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.columns(), ":")); const narrowed = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); try std.testing.expectEqual(was[0][1], narrowed[0][1]); try std.testing.expectEqual(was[2][1], narrowed[3][1]); }