diff --git a/lintcheck/Cargo.toml b/lintcheck/Cargo.toml index b0e4e3e3e573..597fc44461f2 100644 --- a/lintcheck/Cargo.toml +++ b/lintcheck/Cargo.toml @@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" strip-ansi-escapes = "0.2.0" tar = "0.4" +tabled = { version = "0.15", default-features = false, features = ["std"] } toml = "0.7.3" ureq = { version = "2.2", features = ["json"] } walkdir = "2.3" diff --git a/lintcheck/src/json.rs b/lintcheck/src/json.rs index 3a68f2c92435..e6fe624d769b 100644 --- a/lintcheck/src/json.rs +++ b/lintcheck/src/json.rs @@ -3,6 +3,8 @@ use std::path::Path; use itertools::{EitherOrBoth, Itertools}; use serde::{Deserialize, Serialize}; +use tabled::builder::Builder; +use tabled::settings::Style; use crate::ClippyWarning; @@ -93,7 +95,7 @@ pub(crate) fn diff(old_path: &Path, new_path: &Path, truncate: bool) { } } - print_summary_table(&lint_warnings); + println!("{}", get_summary_table(&lint_warnings)); println!(); if lint_warnings.is_empty() { @@ -145,19 +147,22 @@ fn print_lint_warnings(lint: &LintWarnings, truncate_after: usize) { print_changed_diff(&lint.changed, truncate_after / 3); } -fn print_summary_table(lints: &[LintWarnings]) { - println!("| Lint | Added | Removed | Changed |"); - println!("| ------------------------------------------ | ------: | ------: | ------: |"); +fn get_summary_table(lints: &[LintWarnings]) -> String { + let mut builder = Builder::default(); + builder.push_record(vec!["Lint", "Added", "Removed", "Changed"]); for lint in lints { - println!( - "| {:<62} | {:>7} | {:>7} | {:>7} |", - format!("[`{}`](#user-content-{})", lint.name, to_html_id(&lint.name)), - lint.added.len(), - lint.removed.len(), - lint.changed.len() - ); + let lint_name = format!("[`{}`](#user-content-{})", lint.name, to_html_id(&lint.name)); + builder.push_record(vec![ + lint_name, + lint.added.len().to_string(), + lint.removed.len().to_string(), + lint.changed.len().to_string(), + ]); } + let mut table = builder.build(); + + format!("{}", table.with(Style::markdown())) } fn print_warnings(title: &str, warnings: &[LintJson], truncate_after: usize) { @@ -254,3 +259,23 @@ fn count_string(lint: &str, label: &str, count: usize) -> String { format!("[{count} {label}](#user-content-{html_id}-{label})") } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_table_output() { + let lint_warnings = [LintWarnings { + name: "clippy::float_arithmetic".to_string(), + added: vec![], + removed: vec![], + changed: vec![], + }]; + + let expected = "| Lint | Added | Removed | Changed | +|--------------------------------------------------------------|-------|---------|---------| +| [`clippy::float_arithmetic`](#user-content-float-arithmetic) | 0 | 0 | 0 |"; + assert_eq!(expected, get_summary_table(&lint_warnings)); + } +}