From d96de581f3602cdaa723a64dad591447d8b8b47c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 24 Nov 2020 18:52:19 -0600 Subject: [PATCH] fix(report): Rendering issues with errors - We aren't consistent in quoting words - We used byte offsets rather than column counts - We mixed styles between disallowed and corrections Fixes #165 --- crates/typos/src/report.rs | 46 ++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/crates/typos/src/report.rs b/crates/typos/src/report.rs index e3acf89..d2d7ce9 100644 --- a/crates/typos/src/report.rs +++ b/crates/typos/src/report.rs @@ -300,25 +300,32 @@ impl Report for PrintLong { } fn print_brief_correction(msg: &Typo) -> Result<(), std::io::Error> { + let line = String::from_utf8_lossy(msg.buffer.as_ref()); + let line = line.replace("\t", " "); + let column = unicode_segmentation::UnicodeSegmentation::graphemes( + line.get(0..msg.byte_offset).unwrap(), + true, + ) + .count(); match &msg.corrections { crate::Status::Valid => {} crate::Status::Invalid => { writeln!( io::stdout(), - "{}:{}: {} is disallowed", + "{}:{}: `{}` is disallowed", context_display(&msg.context), - msg.byte_offset, + column, msg.typo, )?; } crate::Status::Corrections(corrections) => { writeln!( io::stdout(), - "{}:{}: {} -> {}", + "{}:{}: `{}` -> {}", context_display(&msg.context), - msg.byte_offset, + column, msg.typo, - itertools::join(corrections.iter(), ", ") + itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ") )?; } } @@ -329,42 +336,37 @@ fn print_brief_correction(msg: &Typo) -> Result<(), std::io::Error> { fn print_long_correction(msg: &Typo) -> Result<(), std::io::Error> { let stdout = io::stdout(); let mut handle = stdout.lock(); + + let line = String::from_utf8_lossy(msg.buffer.as_ref()); + let line = line.replace("\t", " "); + let column = unicode_segmentation::UnicodeSegmentation::graphemes( + line.get(0..msg.byte_offset).unwrap(), + true, + ) + .count(); match &msg.corrections { crate::Status::Valid => {} crate::Status::Invalid => { - writeln!( - handle, - "{}:{}: {} is disallowed", - context_display(&msg.context), - msg.byte_offset, - msg.typo, - )?; + writeln!(handle, "error: `{}` is disallowed`", msg.typo,)?; } crate::Status::Corrections(corrections) => { writeln!( handle, "error: `{}` should be {}", msg.typo, - itertools::join(corrections.iter(), ", ") + itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ") )?; } } - writeln!( - handle, - " --> {}:{}", - context_display(&msg.context), - msg.byte_offset - )?; + writeln!(handle, " --> {}:{}", context_display(&msg.context), column)?; if let Some(Context::File(context)) = &msg.context { let line_num = context.line_num.to_string(); let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect(); - let hl_indent: String = itertools::repeat_n(" ", msg.byte_offset).collect(); + let hl_indent: String = itertools::repeat_n(" ", column).collect(); let hl: String = itertools::repeat_n("^", msg.typo.len()).collect(); - let line = String::from_utf8_lossy(msg.buffer.as_ref()); - let line = line.replace("\t", " "); writeln!(handle, "{} |", line_indent)?; writeln!(handle, "{} | {}", line_num, line.trim_end())?; writeln!(handle, "{} | {}{}", line_indent, hl_indent, hl)?;