Print the diff in unified format.
(
&self,
file_diffs: &[FileDiff],
config: &DiffOutputConfig,
)
| 9 | impl Diff { |
| 10 | /// Print the diff in unified format. |
| 11 | pub(super) fn print_unified( |
| 12 | &self, |
| 13 | file_diffs: &[FileDiff], |
| 14 | config: &DiffOutputConfig, |
| 15 | ) -> CliResult<()> { |
| 16 | for file_diff in file_diffs { |
| 17 | // Print file header |
| 18 | let old_path = if config.show_path_prefix { |
| 19 | format!("a/{}", file_diff.old_path) |
| 20 | } else { |
| 21 | file_diff.old_path.clone() |
| 22 | }; |
| 23 | let new_path = if config.show_path_prefix { |
| 24 | format!("b/{}", file_diff.new_path) |
| 25 | } else { |
| 26 | file_diff.new_path.clone() |
| 27 | }; |
| 28 | |
| 29 | // Format line stats (e.g., "+2 -1") with colors |
| 30 | let line_stats = if file_diff.stats.insertions > 0 || file_diff.stats.deletions > 0 { |
| 31 | let ins = if file_diff.stats.insertions > 0 { |
| 32 | format!("+{}", file_diff.stats.insertions) |
| 33 | } else { |
| 34 | String::new() |
| 35 | }; |
| 36 | let del = if file_diff.stats.deletions > 0 { |
| 37 | format!("-{}", file_diff.stats.deletions) |
| 38 | } else { |
| 39 | String::new() |
| 40 | }; |
| 41 | (ins, del) |
| 42 | } else { |
| 43 | (String::new(), String::new()) |
| 44 | }; |
| 45 | |
| 46 | if config.color { |
| 47 | // Build colored line stats |
| 48 | let colored_stats = if !line_stats.0.is_empty() || !line_stats.1.is_empty() { |
| 49 | let ins_colored = if !line_stats.0.is_empty() { |
| 50 | added(&line_stats.0).to_string() |
| 51 | } else { |
| 52 | String::new() |
| 53 | }; |
| 54 | let del_colored = if !line_stats.1.is_empty() { |
| 55 | deleted(&line_stats.1).to_string() |
| 56 | } else { |
| 57 | String::new() |
| 58 | }; |
| 59 | if !ins_colored.is_empty() && !del_colored.is_empty() { |
| 60 | format!(" ({} {})", ins_colored, del_colored) |
| 61 | } else if !ins_colored.is_empty() { |
| 62 | format!(" ({})", ins_colored) |
| 63 | } else { |
| 64 | format!(" ({})", del_colored) |
| 65 | } |
| 66 | } else { |
| 67 | String::new() |
| 68 | }; |
no test coverage detected