diff options
Diffstat (limited to 'core/layout.zig')
| -rw-r--r-- | core/layout.zig | 307 |
1 files changed, 291 insertions, 16 deletions
diff --git a/core/layout.zig b/core/layout.zig index 4b5b7d2c..89d206b9 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -11,6 +11,7 @@ const TOPBAR_H = pardes.TOPBAR_H; const COLBAR_H = pardes.COLBAR_H; const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; const Pane = pardes.Pane; +const Trail = @import("Trail.zig"); pub const Rect = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; const MAX_COLS = pardes.MAX_COLS; const MAX_PANES = pardes.MAX_PANES; @@ -946,9 +947,9 @@ pub fn replacePane(p: *Pardes, old: usize, new: usize) void { } } -/// Put new pane `id` in the layout: every new pane comes through here, as -/// acme's makenewwindow places it, and every one but a command pane takes -/// the keyboard. `from` is the pane that asked, `origin` the text it asked +/// Put new pane `id` in the layout: every new pane comes through here, +/// into free space first (freeSpace), else as acme's makenewwindow places +/// it, and every one but a command pane takes the keyboard. `from` is the pane that asked, `origin` the text it asked /// from: null when a column tag, the workspace tag or 9P asked (acme's /// t->w == nil). pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPane) void { @@ -960,24 +961,32 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan compute(p); const before = v.rects; const before_min = minima(p); - switch (kind) { - // New: the bottom half of the last pane in its own column - // (look.c:921-923, et->col). - .scratch => acmePlace(p, p.exec_column orelse columnOf(p, from), null, id), - // +Errors, a command's output: the last column (util.c:94-98); a - // command run from a column's tag, that column. - .command => acmePlace(p, p.exec_column orelse v.ncol - 1, null, id), - .errors => acmePlace(p, v.ncol - 1, null, id), - // makenewwindow(nil) for 9P's new (acme.c:877), else (t). - .fs => acmePlace(p, acmeColumn(p, from), null, id), - .shell, .doc => acmePlace(p, acmeColumn(p, from), asked, id), + // Free space first (UX J1): an untouched pane is taken, an empty + // column filled, before any pane is split; an untouched pane only where + // its rows hold the new one's tag at that width and two rows, as every + // placement leaves a pane. + const free = if (freeSpace(p, from, kind)) |space| switch (space) { + .pane => |k| holds: { + v.rects[id].w = v.rects[k].w; + break :holds if (v.rects[k].h >= minRows(p, id)) space else null; + }, + .column => space, + } else null; + if (free) |space| switch (space) { + .pane => |k| takePane(p, k, id), + .column => |c| acmePlace(p, c, null, id), + } else switch (kind) { .reader => readerPlace(p, from, id), + // makenewwindow(t) asked from a pane's text. + .shell, .doc => acmePlace(p, columnFor(p, from, kind), asked, id), + .scratch, .fs, .command, .errors => acmePlace(p, columnFor(p, from, kind), null, id), } // No pane is made shorter than its tag and two rows: where the place // chosen has not the room, the column's tallest pane gives it, and // where none can, the pane is refused, and closed at the step's end - // (Pardes.unplaced) once whatever made it is done with it. - if (!placedRoomy(p, id, &before)) if (findPane(p, id)) |f| { + // (Pardes.unplaced) once whatever made it is done with it. Free space + // splits no pane: a pane taken gives its rows whatever they are. + if (free == null and !placedRoomy(p, id, &before)) if (findPane(p, id)) |f| { removePane(p, id); for (v.cols[f.col].panes[0..p.view().cols[f.col].n]) |k| v.rects[k] = before[k]; snapColWeights(p, f.col); @@ -1045,6 +1054,76 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan if (findPane(p, id)) |f| p.setActiveColumn(f.col); } +/// The column a new pane of `kind` asked from pane `from` goes in. +fn columnFor(p: *Pardes, from: usize, kind: NewPane) usize { + return switch (kind) { + // New: the bottom half of the last pane in its own column + // (look.c:921-923, et->col). + .scratch => p.exec_column orelse columnOf(p, from), + // +Errors, a command's output: the last column (util.c:94-98); a + // command run from a column's tag, that column. + .command => p.exec_column orelse p.view().ncol - 1, + .errors => p.view().ncol - 1, + // makenewwindow(nil) for 9P's new (acme.c:877), else (t). + .fs, .shell, .doc => acmeColumn(p, from), + // Its held pane's (readerPlace). + .reader => columnOf(p, from), + }; +} + +/// Free space (UX J1): an untouched pane (Trail.untouched) or an empty +/// column. +pub const Free = union(enum) { pane: usize, column: usize }; + +/// The free space a new pane of `kind` asked from pane `from` goes into +/// before it splits a pane (UX J1): `from` itself when it is untouched; +/// else the nearest to where it would go, its column first (below `from`, +/// nearest first, then above it; another column from its top), then the +/// columns to its right, nearest first, then those to its left. Asked from +/// a column's tag, that column alone, as acme puts the pane there; from +/// the workspace's, no pane is the asker. None for New and 9P's pane/new, +/// which make it. +pub fn freeSpace(p: *Pardes, from: usize, kind: NewPane) ?Free { + const v = p.view(); + if (kind == .scratch or kind == .fs or v.ncol == 0) return null; + const at = findPane(p, from); + if (at != null and !p.exec_header) if (p.panes[from]) |asker| if (Trail.untouched(p, asker)) return .{ .pane = from }; + const c0 = columnFor(p, from, kind); + for (0..if (p.exec_column != null) 1 else v.ncol) |i| { + const c = if (c0 + i < v.ncol) c0 + i else v.ncol - 1 - i; + const col = v.cols[c].panes[0..v.cols[c].n]; + if (col.len == 0) return .{ .column = c }; + const below = if (at) |f| (if (f.col == c) f.idx + 1 else 0) else 0; + for (0..col.len) |j| { + const k = col[if (j < col.len - below) below + j else col.len - 1 - j]; + const pane = p.panes[k] orelse continue; + if (Trail.untouched(p, pane)) return .{ .pane = k }; + } + } + return null; +} + +/// New pane `id` in untouched pane `k`'s place (UX J1), with its rows and +/// weight; `k` closes at the step's end (Pardes.unplaced), once whatever +/// made `id` is done with it. A file takes its number too, `@pN` and the +/// 9P state its scripts hold, as a walk step's file does (Trail.show): `k` +/// then closes never logged, and the file is a revision on from its text, +/// so that nothing a worker was asked of it lands on this one. Any other +/// pane keeps its own: a shell's spawn and a kind's jobs went to the host +/// by it as the pane was made; a Zerox twin's revision is its text's. +fn takePane(p: *Pardes, k: usize, id: usize) void { + const was = p.panes[k].?; + const now = p.panes[id].?; + replacePane(p, k, id); + p.unplaced.set(k); + if (!Trail.isFile(now) or now.file.?.twin != 0) return; + std.mem.swap(u32, &was.serial, &now.serial); + std.mem.swap(@TypeOf(was.fs), &was.fs, &now.fs); + now.file.?.revision = was.file.?.revision +% 1; + now.file.?.saved_revision = now.file.?.revision; + pardes.ctlfs.events.noteLog(p, .rename, now); +} + /// acme's makenewwindow column (util.c:456-467): the column a command came /// from (its tag's), else the active column, else the keyboard's pane's, /// else the asking pane's. @@ -2301,3 +2380,199 @@ test "a new pane goes below the rows a pane's text takes, a wrapped line's each try std.testing.expect(p.view().active != a); try std.testing.expect(p.view().rects[a].h -| pane.tag_rows >= used); } + +/// UX J1's sessions: a directory of files of 100 lines, a.zig to g.zig, +/// the session's first pane `/test.txt`, jumps walking. +const FreeFixture = struct { + p: *Pardes, + tmp: std.testing.TmpDir, + dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined, + dir: []const u8 = "", + + fn init(f: *FreeFixture, cols: u16, rows: u16) !void { + f.tmp = std.testing.tmpDir(.{}); + errdefer f.tmp.cleanup(); + var text: [100 * 9]u8 = undefined; + var w: std.Io.Writer = .fixed(&text); + for (1..101) |n| try w.print("line {d}\n", .{n}); + for ("abcdefg") |name| try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = &.{ name, '.', 'z', 'i', 'g' }, .data = w.buffered() }); + f.dir = f.dir_buf[0..try f.tmp.dir.realPath(std.testing.io, &f.dir_buf)]; + f.p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = cols, .rows = rows }); + errdefer f.p.deinit(); + _ = try f.p.setTestFile("first\n"); + f.p.settings.jumps = .walk; + } + + fn deinit(f: *FreeFixture) void { + f.p.deinit(); + f.tmp.cleanup(); + } + + /// A pane made at `idx` of column `c` and laid there by hand: a +New + /// (`held` false), or a held file of the directory's. + fn paneAt(f: *FreeFixture, c: usize, idx: usize, held: bool) !usize { + const p = f.p; + const id = p.freeSlot().?; + if (held) { + const pane = try p.newDocPane(id); + pane.file = .{ .path = try std.Io.Dir.path.join(p.gpa, &.{ f.dir, "held.txt" }), .content = try p.gpa.dupe(u8, "held\n"), .history = try panes.File.History.create(p.gpa) }; + Trail.keep(pane); + } else _ = try panes.newScratch(p, 0, id, .scratch); + insert(p, c, idx, id); + p.sync(); + return id; + } + + /// A Look from pane `id` at `name`.zig:`line`; the pane it landed in. + fn look(f: *FreeFixture, id: usize, name: u8, line: usize) usize { + var buf: [std.Io.Dir.max_path_bytes + 16]u8 = undefined; + pardes.look.lookAt(f.p, id, std.fmt.bufPrint(&buf, "{s}/{c}.zig:{d}", .{ f.dir, name, line }) catch unreachable); + f.p.sync(); + return f.p.view().active; + } + + fn count(f: *const FreeFixture) usize { + var n: usize = 0; + for (f.p.panes) |slot| n += @intFromBool(slot != null); + return n; + } +}; + +test "free space first (UX J1): a held pane's reader takes the untouched +New nearest it, in its column below then above, then the columns to its right, then to its left, each keeping its number and rows; with none it splits a pane as before; an empty column is taken whole before a split" { + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(150, 45); + defer f.deinit(); + const p = f.p; + _ = insertColumn(p, 0, false).?; + _ = insertColumn(p, 1, false).?; + // Column 0: the first pane, a +New; column 1: a +New, the held pane, + // a +New; column 2: a +New. + const left = try f.paneAt(0, 1, false); + const above = try f.paneAt(1, 0, false); + const held = try f.paneAt(1, 1, true); + const below = try f.paneAt(1, 2, false); + const right = try f.paneAt(2, 0, false); + const n = f.count(); + for ([_]usize{ below, above, right, left }, 0..) |want, i| { + const serial = p.panes[want].?.serial; + const rect = p.view().rects[want]; + const reader = f.look(held, "abcd"[i], 10); + // In its place, its number, its rows; the +New gone. + try std.testing.expect(p.panes[want] == null); + try std.testing.expectEqual(serial, p.panes[reader].?.serial); + try std.testing.expectEqual(rect.y, p.view().rects[reader].y); + try std.testing.expectEqual(rect.x, p.view().rects[reader].x); + try std.testing.expectEqual(n, f.count()); + try std.testing.expectEqual(p.panes[held].?.serial, p.panes[reader].?.trail.steps[0].owner); + try std.testing.expectEqual("abcd"[i], std.Io.Dir.path.basename(p.panes[reader].?.file.?.path)[0]); + // Kept: the next jump makes a new reader. + Trail.keep(p.panes[reader].?); + } + // No free space: placed as before (readerPlace), the held pane too + // short to halve: at the bottom of the next column, splitting it. + const split = f.look(held, 'e', 20); + try std.testing.expectEqual(n + 1, f.count()); + try std.testing.expectEqual(@as(usize, 2), findPane(p, split).?.col); + try std.testing.expectEqual(@as(usize, 2), p.view().cols[2].n); + // An empty column, right of them all: the next reader takes it whole, + // the held pane keeping its rows. + Trail.keep(p.panes[split].?); + const c = insertColumn(p, 2, false).?; + p.sync(); + const kept = p.view().rects[held]; + const whole = f.look(held, 'f', 30); + try std.testing.expectEqual(c, findPane(p, whole).?.col); + try std.testing.expectEqual(@as(usize, 1), p.view().cols[c].n); + try std.testing.expectEqual(kept, p.view().rects[held]); +} + +test "free space first (UX J1): a jump from an untouched +New reads in it, the same pane by its number, its trail starting at the file; under Jumps new-pane it opens in it the same way; a +New typed into and emptied, kept, or held by a 9P open (answered with it, or one of its files open) is never free space" { + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(120, 40); + defer f.deinit(); + const p = f.p; + // Walking: the +New is the file now, as a reading pane's step makes it. + const scratch = try f.paneAt(0, 1, false); + const serial = p.panes[scratch].?.serial; + const n = f.count(); + const into = f.look(scratch, 'a', 12); + try std.testing.expectEqual(serial, p.panes[into].?.serial); + try std.testing.expectEqual(n, f.count()); + try std.testing.expect(Trail.reading(p, p.panes[into].?)); + try std.testing.expectEqual(@as(u8, 1), p.panes[into].?.trail.n); + try std.testing.expectEqual(@as(u32, 12), p.panes[into].?.trail.steps[0].place.line); + // Under Jumps new-pane: the same, placement not a walk. + p.settings.jumps = .@"new-pane"; + const other = try f.paneAt(0, 2, false); + const other_serial = p.panes[other].?.serial; + pardes.exec.runBuiltin(p, .Keep, into, "Keep", null); + const opened = f.look(other, 'b', 3); + try std.testing.expectEqual(other_serial, p.panes[opened].?.serial); + try std.testing.expectEqual(n + 1, f.count()); + try std.testing.expectEqualStrings("b.zig", std.Io.Dir.path.basename(p.panes[opened].?.file.?.path)); + p.settings.jumps = .walk; + // Never free space: typed into and emptied again, kept, leased. + const typed = try f.paneAt(0, 3, false); + p.view().active = typed; + for ("ix") |ch| p.update(.{ .key = .{ .cp = ch, .text = &[_]u8{ch} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.update(.{ .key = .{ .cp = 'u', .text = "u" } }); + try std.testing.expectEqual(@as(usize, 0), p.panes[typed].?.file.?.content.len); + const kept = try f.paneAt(0, 4, false); + Trail.keep(p.panes[kept].?); + const leased = try f.paneAt(0, 5, false); + p.fs.opens[0] = .{ .node = 1 }; + p.fs.opens[0].results.list[0] = p.panes[leased].?.serial; + p.fs.opens[0].results.len = 1; + p.fs.opens[0].results.wrote = true; + // ...and one whose own file a 9P client holds open (a script's look + // at it): a script's pane/new stays its own. + const held_open = try f.paneAt(0, 6, false); + p.fs.opens[1] = .{ .node = pardes.ctlfs.Node.of(p.panes[held_open].?.serial, .look) }; + p.fs.opens_len = 2; + defer p.fs.opens[0] = .{}; + defer p.fs.opens[1] = .{}; + for ([_]usize{ typed, kept, leased, held_open }) |k| try std.testing.expect(!Trail.untouched(p, p.panes[k].?)); + try std.testing.expect(freeSpace(p, 0, .doc) == null); + try std.testing.expect(freeSpace(p, 0, .reader) == null); +} + +test "free space first (UX J1): a command's output and a Term take an untouched +New's place, the asking one first, each by a number of its own; New and 9P's pane/new never do" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane's terminal + var f: FreeFixture = .{ .p = undefined, .tmp = undefined }; + try f.init(120, 40); + defer f.deinit(); + const p = f.p; + _ = insertColumn(p, 0, false).?; + // A +New in the last column, where a command's output goes. + const last = try f.paneAt(1, 0, false); + const rect = p.view().rects[last]; + const out = pardes.exec.runCommand(p, 0, "true").?; + p.sync(); + try std.testing.expect(p.panes[last] == null); + try std.testing.expect(p.panes[out].?.command != null); + try std.testing.expectEqual(rect, p.view().rects[out]); + // A Term run from an untouched +New is in its place. + const asker = try f.paneAt(0, 1, false); + const at = p.view().rects[asker]; + const n = f.count(); + pardes.exec.runBuiltin(p, .Term, asker, "Term", null); + p.sync(); + try std.testing.expectEqual(n, f.count()); + try std.testing.expect(p.panes[asker] == null); + try std.testing.expect(p.panes[p.view().active].?.isTerminal()); + try std.testing.expectEqual(at, p.view().rects[p.view().active]); + // New makes free space, and 9P's pane/new: neither takes it. + const before = f.count(); + pardes.exec.runBuiltin(p, .New, 0, "New", null); + p.sync(); + const scratch = p.view().active; + try std.testing.expect(Trail.untouched(p, p.panes[scratch].?)); + pardes.exec.runBuiltin(p, .New, scratch, "New", null); + p.sync(); + try std.testing.expectEqual(before + 2, f.count()); + panes.newScratchFor(p, scratch, .fs); + p.sync(); + try std.testing.expectEqual(before + 3, f.count()); + try std.testing.expect(p.panes[scratch] != null); +} |
