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
//! A terminal diff viewer engine for Ratatui applications.
//!
//! This crate provides the `diffs` command-line viewer and the [`engine::Engine`] it is built
//! on. An `Engine` opens one review (a Git patch, two files, or two Git commits), loads and
//! colors it on background threads, and draws into any [`ratatui::layout::Rect`]. The host keeps
//! ownership of the terminal, input, and layout.
//!
//! ```no_run
//! use std::{sync::mpsc, time::Duration};
//!
//! use diffs_rs::engine::{Engine, Input, Options};
//! use ratatui::crossterm::event::{self, Event, KeyCode};
//!
//! fn main() -> anyhow::Result<()> {
//! let mut terminal = ratatui::init();
//! // The engine sends a hint on this channel when background work is ready.
//! let (wake, woken) = mpsc::sync_channel(1);
//! let input = Input::Patch("changes.patch".into());
//! let mut engine = Engine::open(input, Options::default(), wake)?;
//!
//! let result = (|| -> anyhow::Result<()> {
//! loop {
//! let _ = woken.try_recv();
//! engine.poll()?;
//! if engine.needs_draw() {
//! let mut drawn = Ok(());
//! terminal.draw(|frame| {
//! let area = frame.area();
//! drawn = engine.draw(frame, area);
//! })?;
//! drawn?;
//! engine.after_draw();
//! }
//! if event::poll(Duration::from_millis(16))? {
//! let event = event::read()?;
//! if matches!(&event, Event::Key(key) if key.code == KeyCode::Char('q')) {
//! return Ok(());
//! }
//! engine.handle_event(event, true)?;
//! }
//! }
//! })();
//!
//! // Joins the background workers and reports cleanup errors.
//! let closed = engine.close();
//! ratatui::restore();
//! result.and(closed)
//! }
//! ```
//!
//! The [embedding guide](https://github.com/plannotator/diffs-rs/blob/main/docs/embedding.md)
//! covers the host loop, focus, source coordinates, reading tools, and shutdown.