diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Core.zig | 4 | ||||
| -rw-r--r-- | src/File.zig | 167 | ||||
| -rw-r--r-- | src/ansi.zig | 170 | ||||
| -rw-r--r-- | src/body_layer.zig | 2 | ||||
| -rw-r--r-- | src/look.zig | 2 |
5 files changed, 175 insertions, 170 deletions
diff --git a/src/Core.zig b/src/Core.zig index 7c550d74..ecfd8fa7 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -399,8 +399,8 @@ frame_arena: std.heap.ArenaAllocator, /// for — see edit.RowsCache for the lifetime rule shell_rows: pardes.edit.RowsCache = .{}, /// Finished command panes' output, and whether it is a diff -/// (File.DiffOutput). -diff_output: pardes.panes.File.DiffOutput = .{}, +/// (ansi.DiffOutput). +diff_output: pardes.ansi.DiffOutput = .{}, /// What the replay digest leaves out (src/walk.zig): what drawing /// keeps, what is recomputed, and what only the session that performs diff --git a/src/File.zig b/src/File.zig index d4032171..0c3c3a2f 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1813,9 +1813,9 @@ fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) return written; } -const SynStyle = struct { fg: [3]u8, bold: bool }; +pub const SynStyle = struct { fg: [3]u8, bold: bool }; -fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { +pub fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { return .{ .fg = Colors.synInk(p.theme(), sy) orelse return null, .bold = false }; } @@ -1981,169 +1981,6 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. } } -/// What finished command panes printed, kept while it cannot change: a -/// copy of the terminal's rows, and whether they are a diff. Taken once -/// when a finished pane is first drawn, so neither a frame nor a look dumps -/// the whole scrollback again; a still running command is shown as it -/// printed. Keyed by the pane and its width (a resize rewraps every row); -/// a rerun in the pane drops it (exec.runCommand). -pub const DiffOutput = struct { - const Entry = struct { - serial: u32, - cols: u16, - is_diff: bool, - /// The rows joined by '\n'; `rows` are slices of it. - text: []u8, - rows: [][]const u8, - used: u64, - }; - slots: [4]?Entry = @splat(null), - clock: u64 = 0, - - pub fn reset(o: *DiffOutput, gpa: std.mem.Allocator) void { - for (&o.slots) |*slot| { - if (slot.*) |e| { - gpa.free(e.text); - gpa.free(e.rows); - } - slot.* = null; - } - } - - /// A pane's command runs again: what it printed before is gone. - pub fn drop(o: *DiffOutput, gpa: std.mem.Allocator, serial: u32) void { - for (&o.slots) |*slot| if (slot.*) |e| if (e.serial == serial) { - gpa.free(e.text); - gpa.free(e.rows); - slot.* = null; - }; - } - - /// A finished command pane's rows when they are a diff, else null. - pub fn lines(o: *DiffOutput, p: *Pardes, pane: *Pane) ?[]const []const u8 { - if (pane.command == null or !pane.command_done or pane.command_pty or pane.terminal == null) return null; - o.clock += 1; - for (&o.slots) |*slot| if (slot.*) |*e| if (e.serial == pane.serial and e.cols == pane.cols) { - e.used = o.clock; - return if (e.is_diff) e.rows else null; - }; - const shown = pardes.edit.shellRows(p, pane) catch return null; - const is_diff = diff.looksLikeDiff(shown); - var total: usize = 0; - if (is_diff) for (shown) |row| { - total += row.len + 1; - }; - const text = p.gpa.alloc(u8, total) catch return null; - const rows = p.gpa.alloc([]const u8, if (is_diff) shown.len else 0) catch { - p.gpa.free(text); - return null; - }; - var at: usize = 0; - for (rows, 0..) |*row, i| { - @memcpy(text[at..][0..shown[i].len], shown[i]); - row.* = text[at..][0..shown[i].len]; - at += shown[i].len; - text[at] = '\n'; - at += 1; - } - // An empty slot, else the least recently used. - var oldest: usize = 0; - for (&o.slots, 0..) |*slot, i| { - const e = slot.* orelse { - oldest = i; - break; - }; - if (e.used < o.slots[oldest].?.used) oldest = i; - } - if (o.slots[oldest]) |old| { - p.gpa.free(old.text); - p.gpa.free(old.rows); - } - o.slots[oldest] = .{ .serial = pane.serial, .cols = pane.cols, .is_diff = is_diff, .text = text, .rows = rows, .used = o.clock }; - return if (is_diff) rows else null; - } -}; - -/// A command pane whose output is a diff (`git diff`, `diff -u`): its -/// rows coloured as a .diff file's are, over what the program printed. -/// The rows are the terminal's own, so a line it wrapped goes on in the -/// row below (syntax.highlightDiffRows). -pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void { - if (pane.command == null) return; - const tz = tracy.zone(@src(), "diffRecolor"); - defer tz.end(); - const rows = p.diff_output.lines(p, pane) orelse return; - // The copy's rows are one text, joined as a file's lines are. - const flat_start = @intFromPtr(rows[0].ptr); - const last = rows[rows.len - 1]; - const flat = rows[0].ptr[0 .. @intFromPtr(last.ptr) + last.len - flat_start]; - const raw = pane.body.mode == .tty; - const View = struct { row: usize, at: usize }; - const arena = p.scratch.allocator(); - const views = arena.alloc(?View, body_h) catch return; - var lo: usize = std.math.maxInt(usize); - var hi: usize = 0; - var shown = std.mem.splitScalar(u8, body, '\n'); - for (views, 0..) |*view, vi| { - view.* = null; - const line = shown.next() orelse continue; - const vr: i32 = @intCast(vi); - const w = pane.wrapAt(vr); - const row_i: i32 = if (raw) @as(i32, @intCast(panes.Term.grid(pane.terminal).offset)) + vr else w.line; - if (row_i < 0 or row_i >= @as(i64, @intCast(rows.len))) continue; - const row: usize = @intCast(row_i); - const from: usize = if (raw) 0 else @intCast(@max(0, w.at)); - if (from > rows[row].len or !std.mem.startsWith(u8, rows[row][from..], line)) continue; - view.* = .{ .row = row, .at = from }; - const start = @intFromPtr(rows[row].ptr) - flat_start; - lo = @min(lo, start); - hi = @max(hi, start + rows[row].len); - } - if (lo > hi) return; - const styles = syntax.highlightDiffRows(arena, flat, lo, hi, true) catch return; - const tints: Colors.Grounds = .of(p.theme()); - const ink = p.theme().fg orelse p.theme().tag_fg; - const body_y = p.bodyTop(pane, r); - shown = std.mem.splitScalar(u8, body, '\n'); - for (views, 0..) |maybe, vr| { - const line = shown.next() orelse break; - const view = maybe orelse continue; - const base = @intFromPtr(rows[view.row].ptr) - flat_start - lo; - const tint = if (base < styles.len) tints.forStyle(styles[base]) else null; - if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.row, null); - var c: usize = 0; - var screen_c: usize = 0; - while (c < line.len and screen_c < tw) { - const grapheme_end = modal.nextGrapheme(line, c); - if (grapheme_end <= c) break; - const cells = graphemeDisplayWidth(line[c..grapheme_end]); - const style = styles[base + view.at + c]; - const prefix = view.at + c == 0 and (line[0] == '+' or line[0] == '-'); - // On an added or removed line, what the program coloured red or - // green is the tint's to say now: its code takes the page's ink. - const paint: ?SynStyle = if (tint != null and prefix) - .{ .fg = tint.?.ink, .bold = true } - else if (synStyle(p, @fromBackingInt(@intCast(style & syntax.syn_bits)))) |ss| - ss - else if (tint != null) - .{ .fg = ink, .bold = false } - else - null; - if (paint) |ss| { - var fill: usize = 0; - while (fill < cells and screen_c + fill < tw) : (fill += 1) { - const cell = s.at(tx + @as(u16, @intCast(screen_c + fill)), body_y + @as(u16, @intCast(vr))); - if (cell.default) continue; - cell.style.fg = .{ .rgb = ss.fg }; - cell.style.bold = ss.bold; - } - } - screen_c += cells; - c = grapheme_end; - } - } -} - pub fn paintWordSelection( p: *Pardes, s: *Surface, diff --git a/src/ansi.zig b/src/ansi.zig index 3a3f9d85..ad74dabb 100644 --- a/src/ansi.zig +++ b/src/ansi.zig @@ -2,12 +2,17 @@ //! the body shows (its paint, handed over as plain cells: Term.paint), and //! its kitty images over them (Term.images). The terminal gives its view's //! cells in the theme's colours; which of them stand under which of the -//! body's rows, the edit copy's among them, is worked out here. +//! body's rows, the edit copy's among them, is worked out here. And a +//! finished command pane's diff, coloured over those cells as a .diff +//! file's lines are (DiffOutput, recolorDiffOutput). const std = @import("std"); const pardes = @import("pardes.zig"); const panes = @import("panes.zig"); const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); +const syntax = @import("syntax.zig"); +const diff = @import("diff.zig"); +const Colors = @import("colors.zig"); const Term = @import("Term.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; @@ -303,3 +308,166 @@ const Anchor = struct { return an.prefix == std.math.maxInt(usize); } }; + +/// What finished command panes printed, kept while it cannot change: a +/// copy of the terminal's rows, and whether they are a diff. Taken once +/// when a finished pane is first drawn, so neither a frame nor a look dumps +/// the whole scrollback again; a still running command is shown as it +/// printed. Keyed by the pane and its width (a resize rewraps every row); +/// a rerun in the pane drops it (exec.runCommand). +pub const DiffOutput = struct { + const Entry = struct { + serial: u32, + cols: u16, + is_diff: bool, + /// The rows joined by '\n'; `rows` are slices of it. + text: []u8, + rows: [][]const u8, + used: u64, + }; + slots: [4]?Entry = @splat(null), + clock: u64 = 0, + + pub fn reset(o: *DiffOutput, gpa: std.mem.Allocator) void { + for (&o.slots) |*slot| { + if (slot.*) |e| { + gpa.free(e.text); + gpa.free(e.rows); + } + slot.* = null; + } + } + + /// A pane's command runs again: what it printed before is gone. + pub fn drop(o: *DiffOutput, gpa: std.mem.Allocator, serial: u32) void { + for (&o.slots) |*slot| if (slot.*) |e| if (e.serial == serial) { + gpa.free(e.text); + gpa.free(e.rows); + slot.* = null; + }; + } + + /// A finished command pane's rows when they are a diff, else null. + pub fn lines(o: *DiffOutput, p: *Pardes, pane: *Pane) ?[]const []const u8 { + if (pane.command == null or !pane.command_done or pane.command_pty or pane.terminal == null) return null; + o.clock += 1; + for (&o.slots) |*slot| if (slot.*) |*e| if (e.serial == pane.serial and e.cols == pane.cols) { + e.used = o.clock; + return if (e.is_diff) e.rows else null; + }; + const shown = pardes.edit.shellRows(p, pane) catch return null; + const is_diff = diff.looksLikeDiff(shown); + var total: usize = 0; + if (is_diff) for (shown) |row| { + total += row.len + 1; + }; + const text = p.gpa.alloc(u8, total) catch return null; + const rows = p.gpa.alloc([]const u8, if (is_diff) shown.len else 0) catch { + p.gpa.free(text); + return null; + }; + var at: usize = 0; + for (rows, 0..) |*row, i| { + @memcpy(text[at..][0..shown[i].len], shown[i]); + row.* = text[at..][0..shown[i].len]; + at += shown[i].len; + text[at] = '\n'; + at += 1; + } + // An empty slot, else the least recently used. + var oldest: usize = 0; + for (&o.slots, 0..) |*slot, i| { + const e = slot.* orelse { + oldest = i; + break; + }; + if (e.used < o.slots[oldest].?.used) oldest = i; + } + if (o.slots[oldest]) |old| { + p.gpa.free(old.text); + p.gpa.free(old.rows); + } + o.slots[oldest] = .{ .serial = pane.serial, .cols = pane.cols, .is_diff = is_diff, .text = text, .rows = rows, .used = o.clock }; + return if (is_diff) rows else null; + } +}; + +/// A command pane whose output is a diff (`git diff`, `diff -u`): its +/// rows coloured as a .diff file's are, over what the program printed. +/// The rows are the terminal's own, so a line it wrapped goes on in the +/// row below (syntax.highlightDiffRows). +pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void { + if (pane.command == null) return; + const tz = tracy.zone(@src(), "diffRecolor"); + defer tz.end(); + const rows = p.diff_output.lines(p, pane) orelse return; + // The copy's rows are one text, joined as a file's lines are. + const flat_start = @intFromPtr(rows[0].ptr); + const last = rows[rows.len - 1]; + const flat = rows[0].ptr[0 .. @intFromPtr(last.ptr) + last.len - flat_start]; + const raw = pane.body.mode == .tty; + const View = struct { row: usize, at: usize }; + const arena = p.scratch.allocator(); + const views = arena.alloc(?View, body_h) catch return; + var lo: usize = std.math.maxInt(usize); + var hi: usize = 0; + var shown = std.mem.splitScalar(u8, body, '\n'); + for (views, 0..) |*view, vi| { + view.* = null; + const line = shown.next() orelse continue; + const vr: i32 = @intCast(vi); + const w = pane.wrapAt(vr); + const row_i: i32 = if (raw) @as(i32, @intCast(panes.Term.grid(pane.terminal).offset)) + vr else w.line; + if (row_i < 0 or row_i >= @as(i64, @intCast(rows.len))) continue; + const row: usize = @intCast(row_i); + const from: usize = if (raw) 0 else @intCast(@max(0, w.at)); + if (from > rows[row].len or !std.mem.startsWith(u8, rows[row][from..], line)) continue; + view.* = .{ .row = row, .at = from }; + const start = @intFromPtr(rows[row].ptr) - flat_start; + lo = @min(lo, start); + hi = @max(hi, start + rows[row].len); + } + if (lo > hi) return; + const styles = syntax.highlightDiffRows(arena, flat, lo, hi, true) catch return; + const tints: Colors.Grounds = .of(p.theme()); + const ink = p.theme().fg orelse p.theme().tag_fg; + const body_y = p.bodyTop(pane, r); + shown = std.mem.splitScalar(u8, body, '\n'); + for (views, 0..) |maybe, vr| { + const line = shown.next() orelse break; + const view = maybe orelse continue; + const base = @intFromPtr(rows[view.row].ptr) - flat_start - lo; + const tint = if (base < styles.len) tints.forStyle(styles[base]) else null; + if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.row, null); + var c: usize = 0; + var screen_c: usize = 0; + while (c < line.len and screen_c < tw) { + const grapheme_end = modal.nextGrapheme(line, c); + if (grapheme_end <= c) break; + const cells = panes.File.graphemeDisplayWidth(line[c..grapheme_end]); + const style = styles[base + view.at + c]; + const prefix = view.at + c == 0 and (line[0] == '+' or line[0] == '-'); + // On an added or removed line, what the program coloured red or + // green is the tint's to say now: its code takes the page's ink. + const paint: ?panes.File.SynStyle = if (tint != null and prefix) + .{ .fg = tint.?.ink, .bold = true } + else if (panes.File.synStyle(p, @fromBackingInt(@intCast(style & syntax.syn_bits)))) |ss| + ss + else if (tint != null) + .{ .fg = ink, .bold = false } + else + null; + if (paint) |ss| { + var fill: usize = 0; + while (fill < cells and screen_c + fill < tw) : (fill += 1) { + const cell = s.at(tx + @as(u16, @intCast(screen_c + fill)), body_y + @as(u16, @intCast(vr))); + if (cell.default) continue; + cell.style.fg = .{ .rgb = ss.fg }; + cell.style.bold = ss.bold; + } + } + screen_c += cells; + c = grapheme_end; + } + } +} diff --git a/src/body_layer.zig b/src/body_layer.zig index abd21090..26513b93 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -346,7 +346,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.Query.paint(p, s, pane, r, body_h); } else if (pane.isTerminal()) { ansi.recolor(p, s, pane, r, tx, tw, body_h, body); - panes.File.recolorDiffOutput(p, s, pane, r, tx, tw, body_h, body); + ansi.recolorDiffOutput(p, s, pane, r, tx, tw, body_h, body); } tz_color.end(); if (pane.isTerminal()) ansi.images(p, s, pane, id, tx, tw, body_y, body_h); diff --git a/src/look.zig b/src/look.zig index 783ff54c..136a7a9c 100644 --- a/src/look.zig +++ b/src/look.zig @@ -1285,7 +1285,7 @@ pub fn isHistoryList(pane: *const Pane) bool { // ---- a look on a diff line ---- /// The lines of a pane showing a diff, a .diff or .patch file or a -/// finished command pane whose output is one (File.DiffOutput); null for +/// finished command pane whose output is one (ansi.DiffOutput); null for /// any other pane. Good until the next step. pub fn diffLines(p: *Pardes, pane: *Pane) ?[]const []const u8 { if (pane.file) |*f| { |
