From f423f901137796c7c4141a83ae64818de900335c Mon Sep 17 00:00:00 2001 From: matt rice Date: Fri, 9 Sep 2022 07:03:22 -0700 Subject: [PATCH] Add support for unicode width --- Cargo.toml | 1 + src/lib.rs | 3 ++- src/write.rs | 9 +++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4c0d3be..08029b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ exclude = [ [dependencies] yansi = "0.5" +unicode-width = "0.1.9" diff --git a/src/lib.rs b/src/lib.rs index 0139b14..7cc79cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ use std::{ cmp::{PartialEq, Eq}, fmt, }; +use unicode_width::UnicodeWidthChar; /// A trait implemented by spans within a character-based source. pub trait Span { @@ -369,7 +370,7 @@ impl Config { (' ', tab_end - col) }, c if c.is_whitespace() => (' ', 1), - _ => (c, 1), + _ => (c, c.width().unwrap_or(1)), } } } diff --git a/src/write.rs b/src/write.rs index 39c5190..9326f84 100644 --- a/src/write.rs +++ b/src/write.rs @@ -459,9 +459,14 @@ impl Report<'_, S> { self.config.unimportant_color() }; let (c, width) = self.config.char_width(c, col); - for _ in 0..width { + if c.is_whitespace() { + for _ in 0..width { + write!(w, "{}", c.fg(color))?; + } + } else { write!(w, "{}", c.fg(color))?; - } + }; + } } write!(w, "\n")?;