Run in diff mode iff two positional arguments are supplied

Fixes #989
This commit is contained in:
Dan Davison 2022-03-01 08:53:51 -05:00
parent d13c41c3fe
commit 55287a827e
2 changed files with 19 additions and 23 deletions

View File

@ -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) {

View File

@ -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,
);