.cargo/
crates/
brain/
diff/
benches/
src/
bin/
rustik-diff/
rustik-diff.rs
4.7 KiB
themes/
viewer/
builtins.rs
13.4 KiB
commit.rs
7.0 KiB
context.rs
10.1 KiB
decode.rs
31.0 KiB
decorate.rs
4.2 KiB
document.rs
24.7 KiB
encode.rs
4.9 KiB
highlight.rs
24.4 KiB
highlighter.rs
12.4 KiB
lib.rs
2.0 KiB
status.rs
2.0 KiB
viewer.rs
183.1 KiB
word.rs
16.8 KiB
tests/
Cargo.toml
867 B
README.md
696 B
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/diff/src/bin/rustik-diff.rs
raw
| 1 | //! Binary entry point for the `rustik-diff` pager. |
| 2 | |
| 3 | #[path = "rustik-diff/error.rs"] |
| 4 | mod error; |
| 5 | #[path = "rustik-diff/session.rs"] |
| 6 | mod session; |
| 7 | |
| 8 | use std::ffi::OsString; |
| 9 | use std::io::{self, IsTerminal as _, Read as _, Write as _}; |
| 10 | use std::path::Path; |
| 11 | use std::process::ExitCode; |
| 12 | use std::{env, str}; |
| 13 | |
| 14 | use rustik_diff::{Builtins, Decode, DiffDocument, DiffOptions, DiffViewer, FileTreeMode}; |
| 15 | use rustik_repo::{ReadRepository as _, git}; |
| 16 | use rustik_tui as tui; |
| 17 | use rustik_tui::Terminal; |
| 18 | use rustik_tui_termion as tui_termion; |
| 19 | |
| 20 | use error::Error; |
| 21 | |
| 22 | /// Default static render width for non-terminal output. |
| 23 | const STATIC_COLS: usize = 100; |
| 24 | /// Maximum static render rows for non-terminal output. |
| 25 | const STATIC_MAX_ROWS: usize = 10_000; |
| 26 | |
| 27 | /// Repository context available while rendering. |
| 28 | enum RenderRepo { |
| 29 | /// Input came from a Git command and can be refetched. |
| 30 | Git(git::Repository), |
| 31 | /// Input came from stdin and has no backing repository context. |
| 32 | Stdin, |
| 33 | } |
| 34 | |
| 35 | /// Program entry point. |
| 36 | fn main() -> ExitCode { |
| 37 | match run(env::args_os().skip(1).collect()) { |
| 38 | Ok(()) => ExitCode::SUCCESS, |
| 39 | Err(error) => { |
| 40 | let _ = writeln!(io::stderr(), "rustik-diff: {error}"); |
| 41 | ExitCode::FAILURE |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Runs the binary with forwarded Git arguments. |
| 47 | fn run(args: Vec<OsString>) -> Result<(), Error> { |
| 48 | let mut debug = false; |
| 49 | let mut paths = false; |
| 50 | let mut git_args = Vec::new(); |
| 51 | for arg in args { |
| 52 | if !paths && arg == "--debug" { |
| 53 | debug = true; |
| 54 | continue; |
| 55 | } |
| 56 | paths |= arg == "--"; |
| 57 | git_args.push(arg); |
| 58 | } |
| 59 | |
| 60 | let cwd = env::current_dir()?; |
| 61 | let input_from_git = io::stdin().is_terminal(); |
| 62 | let repo = if input_from_git { |
| 63 | let repository = git::Repository::new(&cwd, git_args.clone())?; |
| 64 | RenderRepo::Git(repository) |
| 65 | } else { |
| 66 | RenderRepo::Stdin |
| 67 | }; |
| 68 | let input = if let RenderRepo::Git(repository) = &repo { |
| 69 | let output = repository.diff_output(&git_args)?; |
| 70 | if !output.status.success() { |
| 71 | io::stderr().write_all(&output.stderr)?; |
| 72 | return Err(Error::Git(output.status.code().unwrap_or(1))); |
| 73 | } |
| 74 | output.stdout |
| 75 | } else { |
| 76 | let mut buf = Vec::new(); |
| 77 | io::stdin().read_to_end(&mut buf)?; |
| 78 | buf |
| 79 | }; |
| 80 | run_or_forward(&input, &cwd, repo, debug) |
| 81 | } |
| 82 | |
| 83 | /// Loads highlight context for files whose new side only exists in the worktree. |
| 84 | fn load_worktree_sources(viewer: &mut DiffViewer, repository: &git::Repository) { |
| 85 | if let Ok(root) = repository.workdir() { |
| 86 | viewer.load_worktree_sources(&root); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// Renders diff input or passes non-diff data through unchanged. |
| 91 | fn run_or_forward(input: &[u8], cwd: &Path, repo: RenderRepo, debug: bool) -> Result<(), Error> { |
| 92 | let document = match DiffDocument::decode_bytes(input) { |
| 93 | Ok(document) if !document.is_empty() => document, |
| 94 | Ok(_) => { |
| 95 | io::stdout().write_all(input)?; |
| 96 | return Ok(()); |
| 97 | } |
| 98 | Err(error) => return Err(Error::Parse(error)), |
| 99 | }; |
| 100 | let assets = Builtins::load()?; |
| 101 | let mut options = DiffOptions { |
| 102 | diff_palette: assets.palette, |
| 103 | ..DiffOptions::default() |
| 104 | }; |
| 105 | |
| 106 | let interactive = io::stdout().is_terminal(); |
| 107 | if interactive { |
| 108 | let root = cwd |
| 109 | .file_name() |
| 110 | .and_then(|name| name.to_str()) |
| 111 | .filter(|name| !name.is_empty()) |
| 112 | .map(str::to_owned) |
| 113 | .unwrap_or_else(|| cwd.display().to_string()); |
| 114 | |
| 115 | options.file_tree = FileTreeMode::Auto; |
| 116 | options.file_tree_root = root; |
| 117 | } |
| 118 | let mut viewer = DiffViewer::new(document, options); |
| 119 | match &repo { |
| 120 | RenderRepo::Git(repository) => { |
| 121 | let _ = viewer.load_file_sources(|oid| repository.cat_blob(oid)); |
| 122 | load_worktree_sources(&mut viewer, repository); |
| 123 | } |
| 124 | RenderRepo::Stdin => { |
| 125 | if let Ok(repository) = git::Repository::discover(cwd.to_path_buf()) { |
| 126 | let _ = viewer.load_file_sources(|oid| repository.cat_blob(oid)); |
| 127 | load_worktree_sources(&mut viewer, &repository); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if !interactive { |
| 133 | let constraints = tui::Constraint::max(tui::Size::new(STATIC_COLS, STATIC_MAX_ROWS)); |
| 134 | |
| 135 | tui_termion::term::print_constrained(&mut viewer, constraints)?; |
| 136 | return Ok(()); |
| 137 | } |
| 138 | |
| 139 | let backend = tui_termion::term::screen()?; |
| 140 | let mut terminal = Terminal::new(backend)?; |
| 141 | |
| 142 | terminal.clear()?; |
| 143 | terminal.set_cursor_visible(false)?; |
| 144 | |
| 145 | let repo = match repo { |
| 146 | RenderRepo::Git(repository) => Some(repository), |
| 147 | RenderRepo::Stdin => None, |
| 148 | }; |
| 149 | let result = session::run(&mut terminal, &mut viewer, repo, debug); |
| 150 | let cursor = terminal.set_cursor_visible(true); |
| 151 | |
| 152 | result?; |
| 153 | cursor.map_err(Error::Io) |
| 154 | } |