diffs-rs 0.1.1

Fast terminal diff viewer and embeddable Rust diff engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use std::{
    fs::File,
    io::{self, BufReader, IsTerminal, Read, Write},
    os::fd::AsFd,
    path::{Path, PathBuf},
    sync::{Arc, mpsc},
    thread,
    time::Duration,
};

use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use crossterm::{
    cursor::{Hide, Show},
    event::{
        DisableBracketedPaste, DisableMouseCapture, EnableBracketedPaste, EnableMouseCapture,
        Event, KeyCode, KeyEventKind, KeyModifiers,
    },
    execute,
    terminal::{
        self, BeginSynchronizedUpdate, EndSynchronizedUpdate, EnterAlternateScreen,
        LeaveAlternateScreen,
    },
};
use diffs_rs::{
    app::{Layout, SidebarPreference},
    engine::{Engine, Input, Options},
    input::{self, LocalInput, SourceState},
    model::Document,
    storage,
    text::safe_text,
    theme::Appearance,
};
use ratatui::{Terminal, backend::CrosstermBackend};

mod cleanup;
mod clipboard;
mod reading_export;
mod reading_ui;
mod reading_worker;
mod terminal_input;
use terminal_input::TerminalInput;

#[derive(Parser)]
#[command(name = "diffs", version, about = "Fast terminal diff viewer")]
struct Cli {
    #[arg(long, value_enum, default_value = "auto", global = true)]
    layout: Layout,
    #[arg(long, value_enum, default_value = "dark", global = true)]
    theme: Appearance,
    /// Show changed files, including for a single-file patch.
    #[arg(long, global = true, conflicts_with = "no_sidebar")]
    sidebar: bool,
    /// Start with the changed-file sidebar hidden.
    #[arg(long, global = true)]
    no_sidebar: bool,
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    #[command(hide = true)]
    ClipboardHelper {
        #[arg(long)]
        bytes: u64,
    },
    /// View a patch file. Use - to read the patch from stdin.
    Patch { path: PathBuf },
    /// Compare two captured regular files with checked full-source coloring.
    Files {
        old: PathBuf,
        new: PathBuf,
        /// Check all source bytes and print a JSON report without opening the viewer.
        #[arg(long)]
        check: bool,
    },
    /// Compare two fixed Git commits directly, without changing the repository.
    Git {
        #[arg(long)]
        repo: PathBuf,
        #[arg(long)]
        base: String,
        #[arg(long)]
        head: String,
        /// Check all source bytes and print a JSON report without opening the viewer.
        #[arg(long)]
        check: bool,
    },
    /// Inspect the parsed source model as JSON, without a terminal.
    Inspect {
        path: PathBuf,
        #[arg(long)]
        summary: bool,
        #[arg(long, conflicts_with = "summary")]
        canonical: bool,
    },
}

fn with_input<T>(path: &Path, read: impl FnOnce(&mut dyn Read) -> Result<T>) -> Result<T> {
    if path.as_os_str() == "-" {
        read(&mut io::stdin())
    } else {
        let mut file =
            File::open(path).with_context(|| format!("Cannot open {}", path.display()))?;
        let before = file.metadata()?;
        let result = read(&mut file)?;
        let after = file.metadata()?;
        if before.is_file()
            && (before.len() != after.len() || before.modified()? != after.modified()?)
        {
            bail!("Input changed during reading. Open a stable patch.");
        }
        Ok(result)
    }
}

fn check_sources(input: LocalInput) -> Result<i32> {
    let cancel = input::Cancellation::default();
    let mut signals = signal_hook::iterator::Signals::new([
        signal_hook::consts::SIGINT,
        signal_hook::consts::SIGTERM,
        signal_hook::consts::SIGHUP,
    ])?;
    let handle = signals.handle();
    let stop = cancel.clone();
    let listener = thread::spawn(move || {
        if signals.forever().next().is_some() {
            stop.stop();
        }
    });
    let result = (|| -> Result<i32> {
        let mut review = input::Review::open(input, &cancel, |_| Ok(()))?;
        let report = review.report(&cancel)?;
        let mut output = io::BufWriter::new(io::stdout().lock());
        serde_json::to_writer_pretty(&mut output, &report)?;
        writeln!(output)?;
        output.flush()?;
        Ok(i32::from(
            review
                .sources()
                .iter()
                .any(|state| matches!(state, SourceState::Unavailable(_))),
        ))
    })();
    handle.close();
    let _ = listener.join();
    if cancel.check().is_err() {
        return Ok(130);
    }
    result
}

fn read_indexed(
    path: &Path,
    publish: impl FnMut(Arc<storage::FileIndex>) -> Result<()>,
) -> Result<Arc<storage::ByteStore>> {
    with_input(path, |input| storage::read(input, publish))
}

struct TerminalGuard {
    events: TerminalInput,
    restored: bool,
}
impl TerminalGuard {
    fn close(&mut self) -> Result<()> {
        let events = self.events.close();
        if self.restored {
            return events;
        }
        let screen = execute!(
            io::stdout(),
            EndSynchronizedUpdate,
            DisableMouseCapture,
            DisableBracketedPaste,
            Show,
            LeaveAlternateScreen
        )
        .map_err(Into::into);
        let mode = terminal::disable_raw_mode().map_err(Into::into);
        let restore = cleanup::finish(screen, mode);
        self.restored = restore.is_ok();
        cleanup::finish(events, restore)
    }
}
impl Drop for TerminalGuard {
    fn drop(&mut self) {
        let _ = self.close();
    }
}

fn run(cli: Cli) -> Result<i32> {
    if let Command::ClipboardHelper { bytes } = cli.command {
        clipboard::helper(bytes)?;
        return Ok(0);
    }
    let sidebar_preference = if cli.sidebar {
        SidebarPreference::Show
    } else if cli.no_sidebar {
        SidebarPreference::Hide
    } else {
        SidebarPreference::Auto
    };
    #[cfg(feature = "delivery-trace")]
    let _trace = diffs_rs::delivery_trace::init()?;
    let (local, check) = match &cli.command {
        Command::Files { old, new, check } => (
            Some(LocalInput::Files {
                old: old.clone(),
                new: new.clone(),
            }),
            *check,
        ),
        Command::Git {
            repo,
            base,
            head,
            check,
        } => (
            Some(LocalInput::Git {
                repo: repo.clone(),
                base: base.clone(),
                head: head.clone(),
            }),
            *check,
        ),
        _ => (None, false),
    };
    if check {
        return check_sources(local.context("Missing local input")?);
    }
    if let Command::Inspect {
        path,
        summary,
        canonical,
    } = &cli.command
    {
        if *canonical {
            if io::stdout().is_terminal() {
                bail!("Canonical source output is binary. Redirect it to a file or pipe.");
            }
            let mut output = io::BufWriter::new(io::stdout().lock());
            read_indexed(path, |file| {
                file.write_canonical(&mut output)?;
                output.flush()?;
                Ok(())
            })?;
            output.flush()?;
            return Ok(0);
        }
        let mut output = io::BufWriter::new(io::stdout().lock());
        if *summary {
            let mut doc = storage::Document::default();
            let spool = read_indexed(path, |file| doc.append(file))?;
            serde_json::to_writer(
                &mut output,
                &serde_json::json!({"files":doc.files.len(),"source_lines":doc.source_len(),"additions":doc.files.iter().map(|f|f.additions).sum::<usize>(),"deletions":doc.files.iter().map(|f|f.deletions).sum::<usize>(),
                    "storage": {"spool_bytes": spool.len(),
                        "source_index_bytes": doc.files.iter().map(|f| f.source_index_bytes()).sum::<usize>(),
                        "source_index_stored_bytes": doc.files.iter().map(|f| f.source_index_stored_bytes()).sum::<u64>(),
                        "projection_bytes": doc.files.iter().map(|f| f.projection_bytes()).sum::<usize>(),
                        "source_run_bytes": doc.files.iter().map(|f| f.source_run_bytes()).sum::<usize>(),
                        "shared_block_bytes": doc.files.iter().map(|f| f.shared_block_bytes()).sum::<usize>(),
                        "side_index_bytes": doc.files.iter().map(|f| f.side_index_bytes()).sum::<usize>(),
                        "marker_index_bytes": doc.files.iter().map(|f| f.marker_index_bytes()).sum::<usize>(),
                        "boundary_index_bytes": doc.files.iter().map(|f| f.boundary_index_bytes()).sum::<usize>(),
                        "metadata_allocation_bytes": doc.files.iter().map(|f| f.metadata_bytes()).sum::<usize>(),
                        "file_descriptor_bytes": doc.files.len() * size_of::<storage::FileIndex>(),
                        "file_list_capacity_bytes": doc.files.capacity() * size_of::<Arc<storage::FileIndex>>(),
                        "cached_source_bytes": spool.cached_bytes()}}),
            )?;
        } else {
            let doc = with_input(path, |input| Document::read(BufReader::new(input)))?;
            serde_json::to_writer(&mut output, &doc)?;
        }
        writeln!(output)?;
        return Ok(0);
    }
    if !io::stdout().is_terminal() {
        bail!("Interactive display requires a terminal. Use `inspect` for source diagnostics.");
    }
    if matches!(&cli.command, Command::Patch { path } if path.as_os_str() == "-")
        && io::stdin().is_terminal()
    {
        bail!(
            "Patch stdin must be piped or redirected; the controlling terminal is used for keyboard input."
        );
    }
    File::open("/dev/tty").context("No controlling terminal is available for keyboard input")?;
    #[cfg(feature = "owner-census")]
    let mut owner_census = diffs_rs::owner_census::Capture::from_env()?;
    let (wake, ready) = mpsc::sync_channel(1);
    let events = TerminalInput::start(wake.clone())?;
    terminal::enable_raw_mode()?;
    let mut guard = TerminalGuard {
        events,
        restored: false,
    };
    let result = (|| -> Result<i32> {
        execute!(
            io::stdout(),
            EnterAlternateScreen,
            Hide,
            EnableMouseCapture,
            EnableBracketedPaste
        )?;
        let mut terminal = Terminal::new(CrosstermBackend::new(io::stdout()))?;
        let input = if let Some(input) = local {
            Input::Local(input)
        } else if let Command::Patch { path } = cli.command {
            if path.as_os_str() == "-" {
                Input::Stream {
                    file: io::stdin().as_fd().try_clone_to_owned()?.into(),
                    label: "-".into(),
                }
            } else {
                Input::Patch(path)
            }
        } else {
            bail!("Missing interactive input");
        };
        let options = Options {
            layout: cli.layout,
            appearance: cli.theme,
            sidebar: sidebar_preference,
            ..Options::default()
        };
        let mut engine = Engine::open(input, options, wake.clone())?;
        let mut reading = reading_ui::Reading::new(wake.clone());
        let mut resized = false;
        let result = (|| -> Result<i32> {
            loop {
                // Clear before draining payloads; notifications arriving during a drain remain pending.
                let _ = ready.try_recv();
                let signal = guard.events.signal();
                if signal != 0 {
                    return Ok(128 + signal);
                }
                engine.poll()?;
                reading.poll(&mut engine)?;
                #[cfg(feature = "owner-census")]
                engine.poll_census(&mut owner_census)?;
                if engine.needs_draw() || reading.needs_draw() {
                    execute!(io::stdout(), BeginSynchronizedUpdate)?;
                    if resized {
                        // Coalesced resize signals can return to the previous dimensions after clipping cells.
                        terminal.resize(terminal.size()?.into())?;
                        resized = false;
                    }
                    let mut display_result = Ok(());
                    let drawn = terminal.draw(|frame| {
                        let controls = reading.controls(frame.area().width);
                        display_result =
                            engine.draw_with_controls(frame, frame.area(), Some(&controls));
                        if display_result.is_ok() {
                            reading.draw_prompt(frame);
                        }
                    });
                    execute!(io::stdout(), EndSynchronizedUpdate)?;
                    drawn?;
                    display_result?;
                    engine.after_draw();
                    reading.after_draw();
                }
                let event = match guard.events.try_event() {
                    Ok(event) => event?,
                    Err(mpsc::TryRecvError::Disconnected) => {
                        bail!("The terminal input worker stopped")
                    }
                    Err(mpsc::TryRecvError::Empty) => {
                        diffs_rs::delivery_event!("wait_ready", 0, 0);
                        let _ = ready.recv_timeout(Duration::from_millis(100));
                        match guard.events.try_event() {
                            Ok(event) => event?,
                            Err(mpsc::TryRecvError::Empty) => continue,
                            Err(mpsc::TryRecvError::Disconnected) => {
                                bail!("The terminal input worker stopped")
                            }
                        }
                    }
                };
                diffs_rs::delivery_event!("input", 0, 0);
                if let Event::Key(key) = event {
                    if key.kind == KeyEventKind::Release {
                        continue;
                    }
                    if key.code == KeyCode::Char('c')
                        && key.modifiers.contains(KeyModifiers::CONTROL)
                    {
                        return Ok(130);
                    }
                }
                if reading.handle(&event, &mut engine)? {
                    continue;
                }
                if matches!(event, Event::Key(key) if key.code == KeyCode::Char('q')) {
                    let status = engine.status();
                    return Ok(i32::from(status.error.is_some() || status.source_failed));
                }
                if matches!(event, Event::Resize(..)) {
                    resized = true;
                }
                #[cfg(feature = "owner-census")]
                if matches!(event, Event::Key(key) if key.code == KeyCode::F(12)) {
                    engine.request_census(&mut owner_census)?;
                    continue;
                }
                engine.handle_event(event, true)?;
            }
        })();
        let reading_closed = reading.close();
        let viewer_closed = engine.close();
        cleanup::finish(cleanup::finish(result, reading_closed), viewer_closed)
    })();
    let terminal_closed = guard.close();
    cleanup::finish(result, terminal_closed)
}

fn main() {
    let code = match run(Cli::parse()) {
        Ok(code) => code,
        Err(error) => {
            eprintln!("diffs: {}", safe_text(format!("{error:#}").as_bytes()));
            1
        }
    };
    std::process::exit(code);
}