summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2022-03-01 08:53:51 -0500
committerDan Davison <dandavison7@gmail.com>2022-03-01 19:01:33 -0500
commit55287a827e8f2527df938a1b85e23290f8692607 (patch)
treea28cedf9e491ca16033453265b0c5dd78abb4382
parentd13c41c3fe67f30daf97307883f6cadca6edac63 (diff)
Run in diff mode iff two positional arguments are supplied
Fixes #989
-rw-r--r--src/main.rs23
-rw-r--r--src/subcommands/diff.rs19
2 files changed, 19 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index 980502fb..cfa076ee 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -131,14 +131,21 @@ fn run_app() -> std::io::Result<i32> {
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 0cd7b5d3..6c7fbf00 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,
);