mirror of
https://github.com/crate-ci/typos.git
synced 2024-12-22 23:52:12 -05:00
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
This commit is contained in:
parent
2d662bf4e6
commit
d96de581f3
1 changed files with 24 additions and 22 deletions
|
@ -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)?;
|
||||
|
|
Loading…
Reference in a new issue