From 2a6c8e9a3a155773cf3a263b39e2a474a80309f2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 29 Nov 2022 22:25:15 -0600 Subject: [PATCH] fix(cli): Don't crash on brief output --- src/bin/typos-cli/report.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/bin/typos-cli/report.rs b/src/bin/typos-cli/report.rs index 02cc2d9..0185bd3 100644 --- a/src/bin/typos-cli/report.rs +++ b/src/bin/typos-cli/report.rs @@ -132,13 +132,9 @@ impl Report for PrintLong { } fn print_brief_correction(msg: &Typo, palette: Palette) -> 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(); + let start = String::from_utf8_lossy(&msg.buffer[0..msg.byte_offset]); + let column_number = + unicode_segmentation::UnicodeSegmentation::graphemes(start.as_ref(), true).count(); match &msg.corrections { typos::Status::Valid => {} typos::Status::Invalid => { @@ -148,7 +144,7 @@ fn print_brief_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::E "{}{}{}: {}", palette.info.paint(context_display(&msg.context)), palette.info.paint(divider), - palette.info.paint(column), + palette.info.paint(column_number), palette .strong .paint(format_args!("`{}` is disallowed:", msg.typo)), @@ -161,7 +157,7 @@ fn print_brief_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::E "{}{}{}: {}", palette.info.paint(context_display(&msg.context)), palette.info.paint(divider), - palette.info.paint(column), + palette.info.paint(column_number), palette.strong.paint(format_args!( "`{}` -> {}", msg.typo,