Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ All notable changes to `diffs-rs` are recorded here. The forked crates in
- `Engine::set_markers` draws host markers in the gutter for ranges of source lines.
- `Engine::reveal` moves the cursor to a source line, and `Engine::select_range` selects a
range of source lines as the user would.
- The Files sidebar and the file headers color each status by kind (added, modified, deleted,
renamed or copied) and show `+N` in the addition color and `−N` in the deletion color, as
Hunk does. Folder totals use the same count colors. `theme::Colors` gains `modified` and
`renamed`, which breaks code that builds `Colors` by hand.

## 0.1.0 - 2026-10-27

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ the viewer for all key bindings.
- Syntax colors for the bundled grammars, with changed-word emphasis.
- For `files` and `git`, colors are computed from the complete old and new sources, and unchanged
gaps can be expanded 20 lines at a time.
- A changed-file sidebar with the patch's directory tree and per-file and per-folder counts.
- A changed-file sidebar with the patch's directory tree, per-file status, and per-file and
per-folder counts, colored by kind of change.
- Large patches open progressively; the first screen appears while the rest is still indexing.
- Literal search across the whole diff (`/`, F3).
- Source-line selection (`V`), saving the original bytes (Ctrl-S), and plain-text copy (`y`, macOS).
Expand Down
4 changes: 4 additions & 0 deletions docs/design/SIDEBAR_SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Folder totals include every descendant file, regardless of expansion or scrollin
Totals use checked arithmetic and never wrap silently.
Binary entries show their binary status instead of a misleading textual `+0 -0` claim.
Metadata-only entries remain selectable with their status and zero textual counts.
The status letter takes the theme color of its kind: the addition color for added files, the deletion color for deleted files, `terminal.ansiBlue` for modified and type-changed files, and `terminal.ansiMagenta` for renamed and copied files.
A nonzero `+N` takes the addition color and a nonzero `−N` the deletion color; zero counts, folder file counts, and binary entries stay muted.
The file header in the diff colors its status and counts the same way. Colors change only the text, never the row background, width, or layout.
With `NO_COLOR` set, the terminal backend drops every color and the text stays the same.
Renames use the destination hierarchy. The diff header retains the existing old-to-new path display.

## 4. Layout and visibility
Expand Down
71 changes: 70 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod sidebar;
use crate::{
app::{App, Position},
model::{Kind, RowKind},
theme::Colors,
view::LineId,
};

Expand Down Expand Up @@ -537,6 +538,7 @@ fn file_header(buf: &mut Buffer, app: &App, file: usize, x: u16, y: u16) {
};
let summary = format!(" {} +{} −{} ", f.status, f.additions, f.deletions);
let summary_width = summary.width().min(app.width as usize) as u16;
let summary_x = x + app.width.saturating_sub(summary_width);
label(
buf,
x,
Expand All @@ -549,14 +551,29 @@ fn file_header(buf: &mut Buffer, app: &App, file: usize, x: u16, y: u16) {
);
label(
buf,
x + app.width.saturating_sub(summary_width),
summary_x,
y,
summary_width,
&summary,
c.dim,
c.chrome,
false,
);
color_summary(
buf,
summary_x,
y,
summary_width,
Summary {
at: 1,
lead: f.status.width() as u16,
lead_fg: status_color(c, &f.status),
gap: 3,
additions: f.additions,
deletions: f.deletions,
},
c,
);
}

fn draw_row(
Expand Down Expand Up @@ -909,5 +926,57 @@ fn label(
buf.set_stringn(x, y, text, width as usize, style);
}

/// The color of a file status in the Files tree and file headers.
fn status_color(c: &Colors, status: &str) -> Color {
match status {
"Added" => c.added,
"Deleted" => c.deleted,
"Renamed" | "Copied" => c.renamed,
"Binary change" => c.dim,
_ => c.modified,
}
}

/// The parts of a `STATUS +A −D` change summary that `color_summary` colors.
struct Summary {
/// The first cell of the status or file count.
at: u16,
/// The width of the status or file count.
lead: u16,
lead_fg: Color,
/// The cells between the status and `+A`.
gap: u16,
additions: usize,
deletions: usize,
}

/// Colors a change summary already drawn as muted text from `x`: the status in its color,
/// `+A` in the addition color and `−D` in the deletion color. A zero count stays muted, as do
/// cells from `visible` on.
fn color_summary(buf: &mut Buffer, x: u16, y: u16, visible: u16, s: Summary, c: &Colors) {
let count = |n: usize, color: Color| if n == 0 { c.dim } else { color };
let plus = s.at + s.lead + s.gap;
let minus = plus + 1 + digits(s.additions) + 1;
let spans = [
(s.at, s.at + s.lead, s.lead_fg),
(plus, minus - 1, count(s.additions, c.added)),
(
minus,
minus + 1 + digits(s.deletions),
count(s.deletions, c.deleted),
),
];
for (start, end, fg) in spans {
for cell in start..end.min(visible) {
buf[(x + cell, y)].set_fg(fg);
}
}
}

/// The number of decimal digits in `n`.
fn digits(n: usize) -> u16 {
n.checked_ilog10().map_or(1, |log| log as u16 + 1)
}

#[cfg(test)]
mod tests;
50 changes: 39 additions & 11 deletions src/render/sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ratatui::buffer::Buffer;
use unicode_width::UnicodeWidthStr;

use super::{ellipsize, fill, label};
use super::{Summary, color_summary, digits, ellipsize, fill, label, status_color};
use crate::{app::App, tree::Kind};

pub(super) const HELP: &str = " ↑/↓, j/k Move cursor; PgUp/PgDn page; Home/End bounds\n ←/→ Collapse / expand folders; parent / child\n Enter Open file or toggle folder; Space toggles\n Esc, Tab Return to diff; f hides Files\n Wheel Scroll the pane under the pointer\n * Current diff file; > keyboard cursor\n Esc, ? Close help\n q, Ctrl-C Quit / cancel";
Expand Down Expand Up @@ -56,7 +56,7 @@ pub(super) fn draw(buf: &mut Buffer, app: &App) {
ratatui::layout::Rect::new(area.x, y, area.width, 1),
bg,
);
let (name, summary, suffix) = match &node.kind {
let (name, summary, colors, suffix) = match &node.kind {
Kind::Folder { expanded, .. } => (
format!("{} {}/", if *expanded { "▾" } else { "▸" }, node.label),
format!(
Expand All @@ -66,6 +66,14 @@ pub(super) fn draw(buf: &mut Buffer, app: &App) {
node.totals.deletions,
if node.totals.binary > 0 { " B" } else { "" }
),
Some(Summary {
at: 0,
lead: digits(node.totals.files),
lead_fg: c.dim,
gap: 1,
additions: node.totals.additions,
deletions: node.totals.deletions,
}),
String::new(),
),
Kind::File { id, occurrence } => {
Expand All @@ -75,17 +83,27 @@ pub(super) fn draw(buf: &mut Buffer, app: &App) {
} else {
format!(" [{}]", occurrence)
};
let summary = if file.status.as_ref() == "Binary change" {
"B binary".into()
let (summary, colors) = if file.status.as_ref() == "Binary change" {
("B binary".into(), None)
} else {
format!(
"{} +{} −{}",
file.status.chars().next().unwrap_or('M'),
file.additions,
file.deletions
(
format!(
"{} +{} −{}",
file.status.chars().next().unwrap_or('M'),
file.additions,
file.deletions
),
Some(Summary {
at: 0,
lead: 1,
lead_fg: status_color(c, &file.status),
gap: 1,
additions: file.additions,
deletions: file.deletions,
}),
)
};
(node.label.clone(), summary, suffix)
(node.label.clone(), summary, colors, suffix)
}
};
let summary_width = summary.width().min(16) as u16;
Expand Down Expand Up @@ -117,15 +135,25 @@ pub(super) fn draw(buf: &mut Buffer, app: &App) {
bg,
focused || current,
);
let summary_x = area.right() - summary_width - 1;
label(
buf,
area.right() - summary_width - 1,
summary_x,
y,
summary_width,
&ellipsize(&summary, summary_width as usize),
c.dim,
bg,
false,
);
if let Some(colors) = colors {
// A truncated summary ends in a muted ellipsis.
let visible = if summary.width() > usize::from(summary_width) {
summary_width.saturating_sub(1)
} else {
summary_width
};
color_summary(buf, summary_x, y, visible, colors, c);
}
}
}
Loading
Loading