Mark fixable issues in printer output (#2500)

This commit is contained in:
Charlie Marsh 2023-02-03 16:26:06 -05:00 committed by GitHub
parent b9c1a3c5c1
commit b2be30cb07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 26 deletions

View File

@ -122,7 +122,7 @@ impl<'a> Printer<'a> {
if num_fixable > 0 { if num_fixable > 0 {
writeln!( writeln!(
stdout, stdout,
"{num_fixable} potentially fixable with the --fix option." "[*] {num_fixable} potentially fixable with the --fix option."
)?; )?;
} }
} }
@ -475,17 +475,31 @@ fn num_digits(n: usize) -> usize {
/// Print a single `Message` with full details. /// Print a single `Message` with full details.
fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> { fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
let label = format!( let label = if message.kind.fixable() {
"{}{}{}{}{}{} {} {}", format!(
relativize_path(Path::new(&message.filename)).bold(), "{}{}{}{}{}{} {} [*] {}",
":".cyan(), relativize_path(Path::new(&message.filename)).bold(),
message.location.row(), ":".cyan(),
":".cyan(), message.location.row(),
message.location.column(), ":".cyan(),
":".cyan(), message.location.column(),
message.kind.rule().code().red().bold(), ":".cyan(),
message.kind.body(), message.kind.rule().code().red().bold(),
); message.kind.body(),
)
} else {
format!(
"{}{}{}{}{}{} {} {}",
relativize_path(Path::new(&message.filename)).bold(),
":".cyan(),
message.location.row(),
":".cyan(),
message.location.column(),
":".cyan(),
message.kind.rule().code().red().bold(),
message.kind.body(),
)
};
writeln!(stdout, "{label}")?; writeln!(stdout, "{label}")?;
if let Some(source) = &message.source { if let Some(source) = &message.source {
let commit = message.kind.commit(); let commit = message.kind.commit();
@ -540,16 +554,29 @@ fn print_grouped_message<T: Write>(
row_length: usize, row_length: usize,
column_length: usize, column_length: usize,
) -> Result<()> { ) -> Result<()> {
let label = format!( let label = if message.kind.fixable() {
" {}{}{}{}{} {} {}", format!(
" ".repeat(row_length - num_digits(message.location.row())), " {}{}{}{}{} {} [*] {}",
message.location.row(), " ".repeat(row_length - num_digits(message.location.row())),
":".cyan(), message.location.row(),
message.location.column(), ":".cyan(),
" ".repeat(column_length - num_digits(message.location.column())), message.location.column(),
message.kind.rule().code().red().bold(), " ".repeat(column_length - num_digits(message.location.column())),
message.kind.body(), message.kind.rule().code().red().bold(),
); message.kind.body(),
)
} else {
format!(
" {}{}{}{}{} {} {}",
" ".repeat(row_length - num_digits(message.location.row())),
message.location.row(),
":".cyan(),
message.location.column(),
" ".repeat(column_length - num_digits(message.location.column())),
message.kind.rule().code().red().bold(),
message.kind.body(),
)
};
writeln!(stdout, "{label}")?; writeln!(stdout, "{label}")?;
if let Some(source) = &message.source { if let Some(source) = &message.source {
let commit = message.kind.commit(); let commit = message.kind.commit();

View File

@ -31,8 +31,10 @@ fn test_stdin_error() -> Result<()> {
.failure(); .failure();
assert_eq!( assert_eq!(
str::from_utf8(&output.get_output().stdout)?, str::from_utf8(&output.get_output().stdout)?,
"-:1:8: F401 `os` imported but unused\nFound 1 error.\n1 potentially fixable with the \ r#"-:1:8: F401 [*] `os` imported but unused
--fix option.\n" Found 1 error.
[*] 1 potentially fixable with the --fix option.
"#
); );
Ok(()) Ok(())
} }
@ -54,8 +56,10 @@ fn test_stdin_filename() -> Result<()> {
.failure(); .failure();
assert_eq!( assert_eq!(
str::from_utf8(&output.get_output().stdout)?, str::from_utf8(&output.get_output().stdout)?,
"F401.py:1:8: F401 `os` imported but unused\nFound 1 error.\n1 potentially fixable with \ r#"F401.py:1:8: F401 [*] `os` imported but unused
the --fix option.\n" Found 1 error.
[*] 1 potentially fixable with the --fix option.
"#
); );
Ok(()) Ok(())
} }