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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
All notable changes to `diffs-rs` are recorded here. The forked crates in
`crates/` keep their own changelogs.

## 0.2.1 - Unreleased

- `Engine::set_file_marks` lets the host mark files in the Files sidebar, for example as
viewed. A `FileMark` names a file by path and carries an optional `Badge` of one or two
cells in a `BadgeColor` from the theme, and a `dimmed` flag that mutes the row. The badge
has its own column at the right of every row, so rows stay aligned, and a folder shows a
badge when every file under it has the same one. Hosts that never call it, and the `diffs`
viewer, look as before.

## 0.2.0 - 2026-11-15

- `Options.chrome`: set it to `false` and the engine draws no label row, footer or help
Expand Down
3 changes: 3 additions & 0 deletions docs/design/SIDEBAR_SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ Each tree row occupies one terminal row.
Long labels truncate visibly at grapheme boundaries without overwriting status or counts.
Indentation preserves directory depth. Excessive indentation is clipped with a visible overflow cue.
The focused row and current diff file have distinct indicators, not color alone.
An embedding host can mark files with `Engine::set_file_marks`: a badge of one or two cells in a three-cell column between the counts and the divider, and a dimmed row.
The column exists only once the host sets marks, so the standalone viewer's rows are unchanged. Labels truncate before the badge does.
A folder shows a badge when every file under it has the same one, and is dimmed when every file under it is dimmed.
The footer changes its control hints with keyboard focus.
Errors and incomplete-input status take precedence over ordinary hints.

Expand Down
28 changes: 28 additions & 0 deletions docs/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ Markers for lines that are not displayed, or for files that have not arrived yet
`set_markers` rejects the whole set, keeping the previous one, if any range belongs to another review or has `first` of 0 or greater than `last`. It also fails after `close()`.
`replace()` and `close()` drop the markers. Set them again for the new review with its `review_id()`.

### File marks

`set_file_marks(&[FileMark])` replaces every file mark with a new set. A `FileMark` decorates the Files sidebar row of a file:

```rust
use diffs_rs::engine::{Badge, BadgeColor, FileMark};

engine.set_file_marks(&[FileMark {
path: "src/lib.rs",
badge: Some(Badge { text: "✓", color: BadgeColor::Added }),
dimmed: true,
}])?;
```

- `path` is the file's `file(index).path`: the new path, or the old path of a deleted file. Every file with that path gets the mark, so a path that appears twice in a `git log -p` patch is marked twice.
- `badge` is one or two terminal cells of printable text. `BadgeColor` names a theme color (`Text`, `Muted`, `Accent`, `Added`, `Deleted`, `Modified`, `Renamed`), so the badge follows the dark and light themes.
- `dimmed` draws the row's name, status and counts in the muted color. The badge keeps its color.

From the first call, every row keeps a badge column of three cells (a gap and two badge cells) between its counts and the divider, so rows with and without a badge stay aligned. Long names are shortened to make room; the badge is never cut.
The column stays, even for an empty set, until `replace()` or `close()`. Call `set_file_marks(&[])` when the review opens if the first badge should not shift the rows.
A folder row shows a badge when every file under it has the same badge, text and color, and is dimmed when every file under it is dimmed. The engine computes this, collapsed folders included, because the host does not see the tree's folders. While a patch is loading, folders count the files that have arrived.

Marks for paths that are not in the review draw nothing, and marks can be set before the first file arrives. Later marks for the same path win.
`set_file_marks` redraws the sidebar only; the diff does not reflow. It costs one hash of each path in the set plus one pass over the files already in the tree, so it can run on every toggle.
It rejects the whole set, keeping the previous one, if a badge is empty, wider than two cells or contains control characters. It also fails after `close()`.
`replace()` and `close()` drop the marks and the badge column. The standalone viewer never sets marks.

### Source reading and search

`source_read(SourceRange)` prepares an original-side range with inclusive source-line coordinates.
Expand Down Expand Up @@ -216,6 +243,7 @@ Use full commit IDs to keep Git inputs fixed across repeated opens. A symbolic r

The left pane belongs to the host. Tab switches outer focus; `r` opens the next review, `c` closes the review without exiting, `o` reopens it, and `q` exits the host.
`m` marks the selection (Shift-click to make one) or the cursor line with a gutter marker, and `'` selects the last mark again with `select_range`.
`v` marks the cursor's file viewed, or unmarks it, with `set_file_marks`: a `✓` and a dimmed row in the Files sidebar, which `f` shows.
`--no-chrome` turns off the viewer's label row, footer and help; the host then draws its own status row from `status()` below the viewer.
These are example-host controls, not additional viewer bindings.
The example uses the current terminal and does not open windows or change preferences.
Expand Down
43 changes: 41 additions & 2 deletions examples/embedded_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crossterm::{
LeaveAlternateScreen,
},
};
use diffs_rs::engine::{Engine, Input, Layout, Marker, Options, Side, SidebarPreference};
use diffs_rs::engine::{
Badge, BadgeColor, Engine, FileMark, Input, Layout, Marker, Options, Side, SidebarPreference,
};
use diffs_rs::input::LocalInput;
use diffs_rs::reading::SourceRange;
use ratatui::{
Expand Down Expand Up @@ -191,7 +193,7 @@ fn host_text(engine: &Engine, review: usize, focus: bool) -> Result<String> {
Ok(format!(
"Host pane\nFocus: {focus}\nReview: {}\nState: {state}\n\n{location}\n\n\
Tab switch focus\nr next review\no reopen review\nc close review\n\
m mark selection\n' select last mark\nq exit host\n\n\
m mark selection\n' select last mark\nv mark file viewed\nq exit host\n\n\
Viewer keys stay local.",
review + 1
))
Expand Down Expand Up @@ -247,6 +249,35 @@ fn mark(engine: &mut Engine, marks: &mut Vec<Marker>) -> Result<()> {
engine.set_markers(marks)
}

/// Marks the cursor's file viewed, or unmarks it: a tick in the Files sidebar and a dimmed row.
fn toggle_viewed(engine: &mut Engine, viewed: &mut Vec<String>) -> Result<()> {
let Some(source) = engine.selected_source()? else {
return Ok(());
};
let Some(file) = engine.file(source.file) else {
return Ok(());
};
let path = file.path.to_string();
match viewed.iter().position(|p| *p == path) {
Some(index) => {
viewed.remove(index);
}
None => viewed.push(path),
}
let marks: Vec<FileMark<'_>> = viewed
.iter()
.map(|path| FileMark {
path,
badge: Some(Badge {
text: "✓",
color: BadgeColor::Added,
}),
dimmed: true,
})
.collect();
engine.set_file_marks(&marks)
}

fn run() -> Result<()> {
let Arguments { inputs, chrome } = review_inputs()?;
let (wake, ready) = mpsc::sync_channel(1);
Expand All @@ -271,6 +302,7 @@ fn run() -> Result<()> {
wake,
)?;
let mut marks = Vec::new();
let mut viewed = Vec::new();
let mut focus = true;
let mut dirty = true;
let mut resized = false;
Expand Down Expand Up @@ -343,6 +375,7 @@ fn run() -> Result<()> {
KeyCode::Char('c') => {
engine.close()?;
marks.clear();
viewed.clear();
dirty = true;
continue;
}
Expand All @@ -352,6 +385,7 @@ fn run() -> Result<()> {
}
engine.replace(inputs[review].open())?;
marks.clear();
viewed.clear();
dirty = true;
continue;
}
Expand All @@ -360,6 +394,11 @@ fn run() -> Result<()> {
dirty = true;
continue;
}
KeyCode::Char('v') if !engine.status().closed => {
toggle_viewed(&mut engine, &mut viewed)?;
dirty = true;
continue;
}
KeyCode::Char('\'') => {
// A mark on revealed context leaves the diff if its source is
// withdrawn; the error is recoverable, so drop that mark.
Expand Down
21 changes: 19 additions & 2 deletions src/app/sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use ratatui::layout::Rect;

use super::{App, Position};
use crate::tree::{FileId, Kind, NodeId, Tree};
use crate::{
engine::file_marks::FileMarks,
tree::{FileId, Kind, NodeId, Tree},
};

const TREE_WIDTH: u16 = 32;
const MIN_WIDTH: u16 = TREE_WIDTH + 1 + 64;
Expand Down Expand Up @@ -33,6 +36,8 @@ pub(crate) struct Sidebar {
pub area: Rect,
pub diff: Rect,
pub notice: Option<&'static str>,
/// The host's badges and dimming for file rows.
pub marks: FileMarks,
}

impl Sidebar {
Expand Down Expand Up @@ -117,6 +122,16 @@ impl App {
self.sidebar.preference = preference;
}

/// Replaces the host's file marks on the files already in the tree; later files take
/// theirs as they are added.
pub(crate) fn set_file_marks(&mut self, marks: FileMarks) {
let files = &self.doc.files;
self.sidebar
.tree
.mark_files(|id| marks.get(&files[id.0].path));
self.sidebar.marks = marks;
}

/// The pane height that view movement reasons about. It always counts the label and
/// footer rows, so without chrome the diff rows fill the whole supplied rectangle.
pub(crate) fn layout_height(&self, screen: u16) -> u16 {
Expand All @@ -129,7 +144,9 @@ impl App {

pub(crate) fn prepare_view(&mut self, screen: Rect) -> anyhow::Result<()> {
for file in &self.doc.files[self.sidebar.tree.len()..] {
self.sidebar.tree.append(file)?;
self.sidebar
.tree
.append(file, self.sidebar.marks.get(&file.path))?;
}
let current = self.row(self.cursor.row).map(|row| FileId(row.file));
if current != self.sidebar.current {
Expand Down
7 changes: 7 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A review owns its workers and view state, but never the host terminal.

pub(crate) mod file_marks;
pub(crate) mod markers;
mod reading;
mod selection;
Expand Down Expand Up @@ -29,6 +30,7 @@ use crate::{
text::{safe_label, safe_text},
theme::Appearance,
};
pub use file_marks::{Badge, BadgeColor, FileMark};
pub use markers::Marker;

pub enum Input {
Expand Down Expand Up @@ -123,6 +125,8 @@ pub struct Engine {
area: Rect,
/// Markers set before the first file arrived; the viewer state takes them when created.
markers: markers::Markers,
/// File marks set before the first file arrived, taken like `markers`.
file_marks: file_marks::FileMarks,
}

impl Engine {
Expand Down Expand Up @@ -166,6 +170,7 @@ impl Engine {
draw_pending: false,
area: Rect::default(),
markers: markers::Markers::default(),
file_marks: file_marks::FileMarks::default(),
})
}

Expand Down Expand Up @@ -317,6 +322,7 @@ impl Engine {
app.numbers = self.options.line_numbers;
app.chrome = self.options.chrome;
app.markers = std::mem::take(&mut self.markers);
app.set_file_marks(std::mem::take(&mut self.file_marks));
if self.full_sources {
app.sources = Some(Vec::new());
}
Expand Down Expand Up @@ -612,6 +618,7 @@ impl Engine {
let colors = self.app.as_mut().map(|app| app.worker.close()).transpose();
self.app = None;
self.markers = markers::Markers::default();
self.file_marks = file_marks::FileMarks::default();
self.loading = false;
self.error = None;
self.source_failed = false;
Expand Down
Loading
Loading