Merge pull request #178 from epage/unicode

fix(report): Rendering issues with errors
This commit is contained in:
Ed Page 2020-11-24 20:11:43 -06:00 committed by GitHub
commit 373ef92d4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -300,25 +300,32 @@ impl Report for PrintLong {
} }
fn print_brief_correction(msg: &Typo) -> Result<(), std::io::Error> { 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 { match &msg.corrections {
crate::Status::Valid => {} crate::Status::Valid => {}
crate::Status::Invalid => { crate::Status::Invalid => {
writeln!( writeln!(
io::stdout(), io::stdout(),
"{}:{}: {} is disallowed", "{}:{}: `{}` is disallowed",
context_display(&msg.context), context_display(&msg.context),
msg.byte_offset, column,
msg.typo, msg.typo,
)?; )?;
} }
crate::Status::Corrections(corrections) => { crate::Status::Corrections(corrections) => {
writeln!( writeln!(
io::stdout(), io::stdout(),
"{}:{}: {} -> {}", "{}:{}: `{}` -> {}",
context_display(&msg.context), context_display(&msg.context),
msg.byte_offset, column,
msg.typo, 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> { fn print_long_correction(msg: &Typo) -> Result<(), std::io::Error> {
let stdout = io::stdout(); let stdout = io::stdout();
let mut handle = stdout.lock(); 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 { match &msg.corrections {
crate::Status::Valid => {} crate::Status::Valid => {}
crate::Status::Invalid => { crate::Status::Invalid => {
writeln!( writeln!(handle, "error: `{}` is disallowed`", msg.typo,)?;
handle,
"{}:{}: {} is disallowed",
context_display(&msg.context),
msg.byte_offset,
msg.typo,
)?;
} }
crate::Status::Corrections(corrections) => { crate::Status::Corrections(corrections) => {
writeln!( writeln!(
handle, handle,
"error: `{}` should be {}", "error: `{}` should be {}",
msg.typo, msg.typo,
itertools::join(corrections.iter(), ", ") itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
)?; )?;
} }
} }
writeln!( writeln!(handle, " --> {}:{}", context_display(&msg.context), column)?;
handle,
" --> {}:{}",
context_display(&msg.context),
msg.byte_offset
)?;
if let Some(Context::File(context)) = &msg.context { if let Some(Context::File(context)) = &msg.context {
let line_num = context.line_num.to_string(); let line_num = context.line_num.to_string();
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect(); 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 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_indent)?;
writeln!(handle, "{} | {}", line_num, line.trim_end())?; writeln!(handle, "{} | {}", line_num, line.trim_end())?;
writeln!(handle, "{} | {}{}", line_indent, hl_indent, hl)?; writeln!(handle, "{} | {}{}", line_indent, hl_indent, hl)?;