summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig69
1 files changed, 56 insertions, 13 deletions
diff --git a/src/File.zig b/src/File.zig
index 875d0f46..31392ebd 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -154,8 +154,11 @@ pub const State = struct {
/// Its file was deleted on disk: said once (`deleted`) until it is
/// written or found again.
disk_gone: bool = false,
- /// A +Pager pane (`pardes -`) whose text is a diff (`git diff`, `show`,
- /// `log -p`): drawn and looked at as a diff (Pane.colorAlgo).
+ /// A +Pager pane (`pardes -`): no line numbers, its program's colours
+ /// through Filter until it is toggled off.
+ pager: bool = false,
+ /// A +Pager whose text is a diff (`git diff`, `show`, `log -p`): drawn
+ /// and looked at as a diff (Pane.colorAlgo).
pager_diff: bool = false,
/// A +Pager's colours, as its program printed them (pager.zig): drawn
/// over its text by recolorSyntax, never read over 9P. ponytail: any
@@ -562,6 +565,10 @@ pub fn byteAtRowDisplay(pane: *const Pane, row: i32, from_raw: i32, display_col:
pub fn gutterWidth(pane: *const Pane) u16 {
if (pane.file == null) return 0;
const file = &pane.file.?;
+ // A +Pager, and a +Diff (hunks.zig), show no line numbers, as a
+ // pager and delta show none.
+ if (file.pager) return 0;
+ if (file.output) |o| if (std.meta.eql(o.from, Output.Origin{ .cmd = .Diff })) return 0;
var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content);
var digits: u16 = 1;
while (lines >= 10) : (lines /= 10) digits += 1;
@@ -1164,7 +1171,7 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
f.highlights = &.{};
f.highlight_start = 0;
f.syntax_dirty = true;
- if (std.mem.endsWith(u8, f.path, "/+Pager")) f.pager_diff = looksLikeDiff(new[0..@min(new.len, 64 * 1024)]);
+ if (f.pager) f.pager_diff = looksLikeDiff(new[0..@min(new.len, 64 * 1024)]);
for (twins[0..ntwins], 0..) |id, i| {
const t = p.panes[id].?;
const tf = &t.file.?;
@@ -1434,6 +1441,7 @@ test "a +Pager pane holding a diff is drawn and looked at as one; holding other
const f = &pane.file.?;
p.gpa.free(f.path);
f.path = try p.gpa.dupe(u8, "/tmp/+Pager");
+ f.pager = true;
setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n"));
try std.testing.expect(pane.colorAlgo() == .diff);
try std.testing.expect(pardes.look.diffLines(p, pane) != null);
@@ -1715,7 +1723,7 @@ fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool)
var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined;
const prefix = lbuf[0..prefix_width];
@memset(prefix, ' ');
- if (at == 0) {
+ if (at == 0 and prefix.len > 0) {
var lineno = row + 1;
var digit: usize = prefix.len - 1;
while (true) {
@@ -1790,6 +1798,12 @@ const SynStyle = struct { fg: [3]u8, bold: bool };
pub const DiffTints = struct {
added_bg: [3]u8,
removed_bg: [3]u8,
+ /// The words a changed line changed (hunks.zig), on a stronger ground.
+ added_emph_bg: [3]u8,
+ removed_emph_bg: [3]u8,
+ /// A +Diff line added or removed whole: a faint ground.
+ added_line_bg: [3]u8,
+ removed_line_bg: [3]u8,
added_fg: [3]u8,
removed_fg: [3]u8,
@@ -1804,6 +1818,10 @@ pub const DiffTints = struct {
return .{
.added_bg = added_bg,
.removed_bg = removed_bg,
+ .added_emph_bg = Colors.mixOklab(page, green, 0.28),
+ .removed_emph_bg = Colors.mixOklab(page, red, 0.28),
+ .added_line_bg = Colors.mixOklab(page, green, 0.07),
+ .removed_line_bg = Colors.mixOklab(page, red, 0.07),
.added_fg = legible(green, ink, added_bg),
.removed_fg = legible(red, ink, removed_bg),
};
@@ -1818,9 +1836,9 @@ pub const DiffTints = struct {
/// The row ground and prefix ink for a line's style byte, if it is an
/// added or removed line.
- pub fn forStyle(t: *const DiffTints, style: u8) ?struct { bg: [3]u8, fg: [3]u8 } {
- if (style & syntax.diff_added != 0) return .{ .bg = t.added_bg, .fg = t.added_fg };
- if (style & syntax.diff_removed != 0) return .{ .bg = t.removed_bg, .fg = t.removed_fg };
+ pub fn forStyle(t: *const DiffTints, style: u8) ?struct { bg: [3]u8, fg: [3]u8, emph_bg: [3]u8, line_bg: [3]u8 } {
+ if (style & syntax.diff_added != 0) return .{ .bg = t.added_bg, .fg = t.added_fg, .emph_bg = t.added_emph_bg, .line_bg = t.added_line_bg };
+ if (style & syntax.diff_removed != 0) return .{ .bg = t.removed_bg, .fg = t.removed_fg, .emph_bg = t.removed_emph_bg, .line_bg = t.removed_line_bg };
return null;
}
};
@@ -1855,7 +1873,17 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
const prefix_width = gutterWidth(pane);
const tz_recolor = tracy.zone(@src(), "synRecolor");
defer tz_recolor.end();
- const tints: ?DiffTints = if (pane.colorAlgo() == .diff and highlights.len > 0) DiffTints.of(p) else null;
+ // A diff's rows, and a +Diff's (hunks.zig): their markers coloured.
+ // A diff's whole row is tinted; a +Diff tints only the words a
+ // changed line changed.
+ const algo = pane.colorAlgo();
+ const tints: ?DiffTints = if ((algo == .diff or algo == .locations) and highlights.len > 0) DiffTints.of(p) else null;
+ const row_tint = algo == .diff;
+ var filtered_storage: terminal.FilteredColors = undefined;
+ const filtered: ?*terminal.FilteredColors = if (comptime terminal.enabled) (if (pane.tty_filter and spans.len > 0) blk: {
+ filtered_storage = .init(p, null);
+ break :blk &filtered_storage;
+ } else null) else null;
// indexed start, same as bodyText — an empty tail simply paints nothing
var flines = std.mem.splitScalar(u8, f.content[lineStart(p.gpa, f, f.scroll)..], '\n');
const total = nlines(p.gpa, f);
@@ -1893,7 +1921,11 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
t.forStyle(highlights[base - highlight_start])
else
null else null;
- if (tint) |t| if (prefix_width < tw) tintRow(s, tx + @as(u16, @intCast(prefix_width)), body_y + vr, tx + tw, t.bg);
+ if (tint) |t| if (prefix_width < tw) {
+ // A +Diff line added or removed whole: faintly (hunks.zig).
+ const whole = highlights[base - highlight_start] & syntax.diff_whole != 0;
+ if (row_tint or whole) tintRow(s, tx + @as(u16, @intCast(prefix_width)), body_y + vr, tx + tw, if (row_tint) t.bg else t.line_bg);
+ };
// A +Pager's own colours (pager.zig) on every row but one the diff
// look tints: an added or removed line is drawn as pardes draws a
// diff, and git's red and green on it would only fight the tint.
@@ -1924,6 +1956,11 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
cell.style.bold = ss.bold;
}
}
+ if (tint) |t| if (highlights[hidx] & syntax.diff_emph != 0) {
+ var fill: usize = 0;
+ while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1)
+ s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr).style.bg = .{ .rgb = t.emph_bg };
+ };
}
}
// Results keep their exact live text. Only the location,
@@ -1945,10 +1982,16 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.
while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1) {
const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr);
if (cell.default) continue;
- // A palette colour through the theme's sixteen, as a
- // terminal pane's cell; truecolor as it is.
- if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg;
- if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg;
+ // As a terminal pane's cell: through Filter, or a
+ // palette colour through the theme's sixteen and
+ // truecolor as it is.
+ if (filtered) |colors| {
+ if (st.fg != .default) cell.style.fg = colors.spanColor(st.fg, false);
+ if (st.bg != .default) cell.style.bg = colors.spanColor(st.bg, true);
+ } else {
+ if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg;
+ if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg;
+ }
cell.style.bold = cell.style.bold or st.bold;
cell.style.dim = st.dim;
cell.style.italic = st.italic;