Print the diff in stat format.
(&self, stats: &DiffStats, config: &DiffOutputConfig)
| 365 | |
| 366 | /// Print the diff in stat format. |
| 367 | pub(super) fn print_stat(&self, stats: &DiffStats, config: &DiffOutputConfig) -> CliResult<()> { |
| 368 | if !stats.has_changes() { |
| 369 | return Ok(()); |
| 370 | } |
| 371 | |
| 372 | let max_path_len = stats.max_path_length(); |
| 373 | let max_changes = stats.max_change_count(); |
| 374 | let graph_width = cmp::min(config.stat_width, max_changes); |
| 375 | |
| 376 | for file_stats in stats.iter() { |
| 377 | let path = &file_stats.path; |
| 378 | let padding = max_path_len - path.len(); |
| 379 | let total = file_stats.total_changes(); |
| 380 | |
| 381 | // Calculate graph |
| 382 | let graph = if total > 0 && graph_width > 0 { |
| 383 | let scale = if max_changes > graph_width { |
| 384 | graph_width as f64 / max_changes as f64 |
| 385 | } else { |
| 386 | 1.0 |
| 387 | }; |
| 388 | let plus_count = ((file_stats.insertions as f64 * scale).round() as usize) |
| 389 | .max(if file_stats.insertions > 0 { 1 } else { 0 }); |
| 390 | let minus_count = ((file_stats.deletions as f64 * scale).round() as usize) |
| 391 | .max(if file_stats.deletions > 0 { 1 } else { 0 }); |
| 392 | format!("{}{}", "+".repeat(plus_count), "-".repeat(minus_count)) |
| 393 | } else { |
| 394 | String::new() |
| 395 | }; |
| 396 | |
| 397 | if config.color { |
| 398 | let plus_part = "+".repeat(file_stats.insertions.min(graph_width)); |
| 399 | let minus_part = "-".repeat(file_stats.deletions.min(graph_width)); |
| 400 | println!( |
| 401 | " {} {} | {} {}{}", |
| 402 | style_path(path), |
| 403 | " ".repeat(padding), |
| 404 | total, |
| 405 | added(&plus_part), |
| 406 | deleted(&minus_part) |
| 407 | ); |
| 408 | } else { |
| 409 | println!(" {} {} | {} {}", path, " ".repeat(padding), total, graph); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Print summary |
| 414 | let files_text = if stats.file_count() == 1 { |
| 415 | "file" |
| 416 | } else { |
| 417 | "files" |
| 418 | }; |
| 419 | let ins_text = if stats.total_insertions() == 1 { |
| 420 | "insertion" |
| 421 | } else { |
| 422 | "insertions" |
| 423 | }; |
| 424 | let del_text = if stats.total_deletions() == 1 { |
no test coverage detected