summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-01 01:14:37 -0300
committerGabriel Schneider <[email protected]>2026-10-01 01:35:12 -0300
commite6ff64843b4a8986e9740c576f346690217cf5a7 (patch)
treedc710049d45abfabe79cf4ae22331c6879ea661d /src/File.zig
parent006fdc1d758fc2f9b956051e75f31a0122bd7883 (diff)
downloadpardes-e6ff64843b4a8986e9740c576f346690217cf5a7.tar.gz
pardes-e6ff64843b4a8986e9740c576f346690217cf5a7.zip
A +Pager pane whose text is a diff is drawn and looked at as a diff, as a diff command pane is: git diff, show and log -p page into review
With git paging into +Pager, a diff arrived as plain text: no colour for its sides and a right click on a hunk line searched the word. A +Pager pane now notes, as its text is set, whether its first 64 KiB holds a diff header or a unified hunk, and such a pane takes the diff highlighting and the diff look every diff pane has. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/File.zig b/src/File.zig
index d16d4fc0..c738ee58 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -72,6 +72,9 @@ 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).
+ pager_diff: bool = false,
/// Seconds of the last content change, for the filesystem's stat; zero until edited.
mtime: u32 = 0,
/// Non-null for a generated output buffer rather than an on-disk file.
@@ -998,6 +1001,36 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span: ?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)]);
+}
+
+/// Whether `head`, a text's first bytes, holds a diff: a `diff` header, or
+/// a unified hunk's `---`, `+++` and `@@` lines.
+pub fn looksLikeDiff(head: []const u8) bool {
+ if (std.mem.startsWith(u8, head, "diff ") or std.mem.indexOf(u8, head, "\ndiff ") != null) return true;
+ const minus = std.mem.indexOf(u8, head, "--- ") orelse return false;
+ const plus = std.mem.indexOfPos(u8, head, minus, "\n+++ ") orelse return false;
+ return std.mem.indexOfPos(u8, head, plus, "\n@@ ") != null;
+}
+
+test "a +Pager pane holding a diff is drawn and looked at as one; holding other text, as text" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ const pane = try p.setTestFile("");
+ const f = &pane.file.?;
+ p.gpa.free(f.path);
+ f.path = try p.gpa.dupe(u8, "/tmp/+Pager");
+ 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);
+ setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\n a plain log\n"));
+ try std.testing.expect(pane.colorAlgo() != .diff);
+}
+
+test "a pager's text is a diff when it holds a diff header or a unified hunk" {
+ try std.testing.expect(looksLikeDiff("commit abc\nAuthor: x\n\n msg\n\ndiff --git a/f b/f\n"));
+ try std.testing.expect(looksLikeDiff("--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n"));
+ try std.testing.expect(!looksLikeDiff("commit abc\nAuthor: x\n\n plain log\n"));
}
pub fn restoreSnap(pane: *Pane, f: *State, snap: Snapshot) void {