diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 28 | ||||
-rw-r--r-- | src/paint.rs | 37 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 9 |
3 files changed, 41 insertions, 33 deletions
diff --git a/src/main.rs b/src/main.rs index 3fb223e2..755569bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,19 +159,8 @@ You can also use delta to diff two files: `delta file_A file_B`." } let mut diff_process = diff_process.unwrap(); - let exit_code = diff_process - .wait() - .unwrap_or_else(|_| { - delta_unreachable(&format!("'{}' process not running.", diff_command)); - }) - .code() - .unwrap_or_else(|| { - eprintln!("'{}' process terminated without exit status.", diff_command); - config.error_exit_code - }); - if let Err(error) = delta( - BufReader::new(diff_process.stdout.unwrap()).byte_lines(), + BufReader::new(diff_process.stdout.take().unwrap()).byte_lines(), writer, config, ) { @@ -183,7 +172,20 @@ You can also use delta to diff two files: `delta file_A file_B`." } } }; - exit_code + + // Return the exit code from the `git diff` processl, so that the exit code + // contract of `delta file_A file_B` is the same as that of `diff file_A + // file_B` (i.e. 0 if same, 1 if different, 2 if error). + diff_process + .wait() + .unwrap_or_else(|_| { + delta_unreachable(&format!("'{}' process not running.", diff_command)); + }) + .code() + .unwrap_or_else(|| { + eprintln!("'{}' process terminated without exit status.", diff_command); + config.error_exit_code + }) } fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Result<()> { diff --git a/src/paint.rs b/src/paint.rs index 3b5010b2..0e569232 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -381,9 +381,9 @@ impl<'a> Painter<'a> { painted_prefix: Option<ansi_term::ANSIString>, config: &config::Config, ) -> (String, bool) { - let output_line_numbers = config.line_numbers && line_numbers_data.is_some(); - let mut handled_prefix = false; let mut ansi_strings = Vec::new(); + + let output_line_numbers = config.line_numbers && line_numbers_data.is_some(); if output_line_numbers { ansi_strings.extend(line_numbers::format_and_paint_line_numbers( line_numbers_data.as_mut().unwrap(), @@ -407,27 +407,40 @@ impl<'a> Painter<'a> { } _ => {} } - let mut is_empty = true; - for (section_style, mut text) in superimpose_style_sections( + + let superimposed = superimpose_style_sections( syntax_sections, diff_sections, config.true_color, config.null_syntect_style, - ) { - if !handled_prefix { - if let Some(painted_prefix) = painted_prefix.clone() { - ansi_strings.push(painted_prefix); + ); + + let mut handled_prefix = false; + for (section_style, text) in &superimposed { + let text = if handled_prefix { + &text + } else { + // Remove what was originally the +/- prefix, see `prepare()`, after + // (if requested) re-inserting it with proper styling. + if let Some(ref painted_prefix) = painted_prefix { + ansi_strings.push(painted_prefix.clone()); } + if !text.is_empty() { - text.remove(0); + &text[1..] + } else { + &text } - handled_prefix = true; - } + }; + if !text.is_empty() { ansi_strings.push(section_style.paint(text)); - is_empty = false; } + handled_prefix = true; } + + // Only if syntax is empty (implies diff empty) can a line actually be empty. + let is_empty = syntax_sections.is_empty(); (ansi_term::ANSIStrings(&ansi_strings).to_string(), is_empty) } diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index c605715e..3f28d200 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1472,14 +1472,7 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ .to_string() ); } else { - let style = style::Style::from_str(empty_line_marker_style, None, None, true, false); - assert_eq!( - line, - &style - .ansi_term_style - .paint(ansi::ANSI_CSI_CLEAR_TO_BOL) - .to_string() - ); + assert_eq!(line, ""); } } |