diff options
author | Björn Steinbrink <bsteinbr@gmail.com> | 2022-06-22 14:37:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-22 08:37:10 -0400 |
commit | 37ea9521dcf7ccebc5724401091ce4b2764a5b62 (patch) | |
tree | 1f95b88167e894cd0823446e4b193cf2cfa1150e /src | |
parent | 3f527e0d9c6100119c86a5cd4adb21f3c51651c6 (diff) |
Avoid lockup when running without arguments and stdin is connected to tty (#1112)
Up until commit 55287a8 "Run in diff mode iff two positional arguments
are supplied" diff mode was only supported when stdin is connected to a
tty, which meant that, for example, running `delta a b` in a git hook
would do the wrong thing.
As a fix, the tty check was removed, so now `delta a b` always works,
but the only error condition is now to pass only a single file. When
passing no files at all, we try parse to stdin even when connected to a
tty. Usually that wouldn't be that bad, because the user could press
Ctrl-C or Ctrl-D (EOF) to quit, unfortunately something in the pager
setup breaks this, and the only way to exit `delta` is to kill it.
Running `delta` with stdin connected to a tty would mean that the user
has to manually enter a diff (or paste it), which is not really an
expected scenario, as demonstrated by the error message that's currently
only emitted when running `delta` with only a single file argument. So
we can avoid the lockup by also emitting the error message when running
on a tty and there are no files to be diffed.
Fixes #1039, fixes #1090
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index 1d2b7633..625b5131 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,21 +136,19 @@ fn run_app() -> std::io::Result<i32> { OutputType::from_mode(&env, config.paging_mode, config.pager.clone(), &config).unwrap(); let mut writer = output_type.handle().unwrap(); - 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!( - "\ + if let (Some(minus_file), Some(plus_file)) = (&config.minus_file, &config.plus_file) { + let exit_code = subcommands::diff::diff(minus_file, plus_file, &config, &mut writer); + return Ok(exit_code); + } + + if atty::is(atty::Stream::Stdin) { + 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); - } + ); + return Ok(config.error_exit_code); } if let Err(error) = delta(io::stdin().lock().byte_lines(), &mut writer, &config) { |