From 3b5b2c8077caaf0d3bf2762b23b1c8e0b9a15cd7 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Tue, 1 Mar 2022 08:53:51 -0500 Subject: [PATCH] Run in diff mode iff two positional arguments are supplied Fixes #989 --- src/main.rs | 23 +++++++++++++++-------- src/subcommands/diff.rs | 19 ++++--------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 980502fb6..cfa076ee4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,14 +131,21 @@ fn run_app() -> std::io::Result { OutputType::from_mode(config.paging_mode, config.pager.clone(), &config).unwrap(); let mut writer = output_type.handle().unwrap(); - if atty::is(atty::Stream::Stdin) { - let exit_code = subcommands::diff::diff( - config.minus_file.as_ref(), - config.plus_file.as_ref(), - &config, - &mut writer, - ); - return Ok(exit_code); + match (config.minus_file.as_ref(), config.plus_file.as_ref()) { + (None, None) => {} + (Some(minus_file), Some(plus_file)) => { + let exit_code = subcommands::diff::diff(minus_file, plus_file, &config, &mut writer); + return Ok(exit_code); + } + _ => { + eprintln!( + "\ + The main way to use delta is to configure it as the pager for git: \ + see https://github.com/dandavison/delta#configuration. \ + You can also use delta to diff two files: `delta file_A file_B`." + ); + return Ok(config.error_exit_code); + } } if let Err(error) = delta(io::stdin().lock().byte_lines(), &mut writer, &config) { diff --git a/src/subcommands/diff.rs b/src/subcommands/diff.rs index 0cd7b5d3b..6c7fbf004 100644 --- a/src/subcommands/diff.rs +++ b/src/subcommands/diff.rs @@ -9,23 +9,12 @@ use crate::delta; /// Run `git diff` on the files provided on the command line and display the output. pub fn diff( - minus_file: Option<&PathBuf>, - plus_file: Option<&PathBuf>, + minus_file: &Path, + plus_file: &Path, config: &config::Config, writer: &mut dyn Write, ) -> i32 { use std::io::BufReader; - if minus_file.is_none() || plus_file.is_none() { - eprintln!( - "\ -The main way to use delta is to configure it as the pager for git: \ -see https://github.com/dandavison/delta#configuration. \ -You can also use delta to diff two files: `delta file_A file_B`." - ); - return config.error_exit_code; - } - let minus_file = minus_file.unwrap(); - let plus_file = plus_file.unwrap(); // When called as `delta <(echo foo) <(echo bar)`, then git as of version 2.34 just prints the // diff of the filenames which were created by the process substitution and does not read their @@ -126,8 +115,8 @@ mod main_tests { let config = integration_test_utils::make_config_from_args(&[]); let mut writer = Cursor::new(vec![]); let exit_code = diff( - Some(&PathBuf::from(file_a)), - Some(&PathBuf::from(file_b)), + &PathBuf::from(file_a), + &PathBuf::from(file_b), &config, &mut writer, );