diff options
Diffstat (limited to 'src/body_layer.zig')
| -rw-r--r-- | src/body_layer.zig | 105 |
1 files changed, 102 insertions, 3 deletions
diff --git a/src/body_layer.zig b/src/body_layer.zig index a8d09c66..2df619ba 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -50,7 +50,7 @@ fn paintTerminalSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: [ lo -|= row.prompt_bytes; hi -|= row.prompt_bytes; } else if (!row.raw_terminal and raw) { - const prefix = panes.terminal.promptPrefixBytes(pane, source_row, line); + const prefix = panes.Term.promptPrefix(pane.terminal, source_row, line); lo += prefix; hi += prefix; } @@ -79,7 +79,7 @@ fn pastView(pane: *Pane, body: []const u8, line: i32, row: i32, at: i32, col: i3 fn paintSourceSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []const Pane.PointerRow, bg: [3]u8, fg: ?[3]u8) void { if (pane.isTerminal()) return paintTerminalSelection(p, s, pane, r, rows, bg, fg); - const terminal_lines = if (pane.file == null) panes.terminal.cursorLines(p, pane) catch return else &.{}; + const terminal_lines = if (pane.file == null) edit.terminalLines(p, pane) catch return else &.{}; const tx = r.x + config.GUTTER; const width = r.w -| config.GUTTER; const body_y = p.bodyTop(pane, r); @@ -546,5 +546,104 @@ pub fn bodyText(arena: std.mem.Allocator, pane: *Pane) ![]const u8 { return buf; } if (pane.file) |*f| return panes.File.bodyText(arena, pane, f); - return panes.terminal.bodyText(arena, pane); + // A terminal's: the rows its view shows (outside raw mode its prompts + // cut off), the edit copy's lines over the rows the copy covers. + const vp: []const []const u8 = if (pane.terminal) |state| try panes.Term.viewport(state, arena, pane.body.mode != .tty) else &.{}; + const len = fillBody(null, pane, vp); + const out = try arena.alloc(u8, len); + const filled = fillBody(out, pane, vp); + std.debug.assert(filled == out.len); + return out; +} + +pub const BodyRow = union(enum) { + grid: i32, + edit: struct { line: []const u8, idx: usize }, +}; + +/// A terminal pane's body rows, top down: a row of its emulator's view +/// (`grid`, its viewport row), or a line of the edit copy standing in for +/// the rows it covers (`edit`). +pub const BodyWalk = struct { + pane: *Pane, + goff: i32, + g: i32, + /// the buffer can start above the viewport: drop the lines scrolled past + skip: usize, + n: usize = 0, + lines: ?std.mem.SplitIterator(u8, .scalar) = null, + covered: i32 = 0, + line_idx: usize = 0, + + pub fn init(pane: *Pane) BodyWalk { + const off = pane.scroll(); + const goff: i32 = @intCast(panes.Term.grid(pane.terminal).offset); + return .{ + .pane = pane, + .goff = goff, + .g = if (pane.body.mode == .tty) goff + pane.pan else pane.gridRow(off), + .skip = if (pane.ovl) |o| @intCast(@max(0, off - pane.surfRow(o.row))) else 0, + }; + } + + pub fn next(w: *BodyWalk) ?BodyRow { + while (w.n < w.pane.rows) { + if (w.lines) |*it| { + if (it.next()) |line| { + const idx = w.line_idx; + w.line_idx += 1; + // Lines scrolled off the top still count: the index names a + // line of the BUFFER, not of the visible body. + if (w.skip > 0) { + w.skip -= 1; + continue; + } + w.n += 1; + return .{ .edit = .{ .line = line, .idx = idx } }; + } + // The buffer stands in for `rows` shell rows however many lines + // it actually spelled, which is the whole slide. + w.g += w.covered; + w.skip = 0; + w.lines = null; + continue; + } + if (w.pane.body.mode != .tty) if (w.pane.ovl) |o| if (w.g == o.row) { + w.lines = std.mem.splitScalar(u8, o.text, '\n'); + w.covered = o.rows; + w.line_idx = 0; + continue; + }; + const vi = w.g - w.goff; + w.g += 1; + w.n += 1; + return .{ .grid = vi }; + } + return null; + } +}; + +/// Run the terminal body row walk. A null destination counts bytes; a slice +/// fills the exact allocation made from that count. +fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize { + var walk: BodyWalk = .init(pane); + var written: usize = 0; + var first = true; + while (walk.next()) |row| { + if (!first) { + if (dst) |out| out[written] = '\n'; + written += 1; + } + first = false; + const bytes = switch (row) { + .edit => |e| e.line, + .grid => |vi| if (vi >= 0 and @as(usize, @intCast(vi)) < viewport.len) + viewport[@intCast(vi)] + else + "", + }; + if (dst) |out| @memcpy(out[written..][0..bytes.len], bytes); + written += bytes.len; + } + return written; } |
