From 6b9e4cbc63f9a101e08632049531ec62941f947f Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 28 May 2022 22:43:10 +0200 Subject: [PATCH] Ignore graphemes of width 0 when writing to buffer --- examples/text_wrapping.rs | 4 ++-- src/buffer.rs | 4 +++- src/widthdb.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/text_wrapping.rs b/examples/text_wrapping.rs index add9b5a..2c48dd6 100644 --- a/examples/text_wrapping.rs +++ b/examples/text_wrapping.rs @@ -10,8 +10,8 @@ fn draw(f: &mut Frame) { "After this sentence come two newlines, so it should always break here.\n", "\n", "Since the wrapping algorithm is aware of the Unicode Standard Annex #14, ", - "it understands things like nonbreaking spaces: ", - "This\u{00a0}sentence\u{00a0}is\u{00a0}separated\u{00a0}by\u{00a0}nonbreaking\u{00a0}spaces.\n", + "it understands things like non-breaking spaces and word joiners: ", + "This\u{00a0}sentence\u{00a0}is\u{00a0}separated\u{00a0}by\u{00a0}non-\u{2060}breaking\u{00a0}spaces.\n", "\n", "It can also properly handle wide graphemes (like emoji 🤔), ", "including ones usually displayed incorrectly by terminal emulators, like 👩‍🔬 (a female scientist emoji).", diff --git a/src/buffer.rs b/src/buffer.rs index 77756f3..4707b53 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -142,7 +142,9 @@ impl Buffer { for grapheme in content.graphemes(true) { let width = widthdb.grapheme_width(grapheme); - self.write_grapheme(pos.x, y, width, grapheme, style); + if width > 0 { + self.write_grapheme(pos.x, y, width, grapheme, style); + } pos.x += width as i32; } } diff --git a/src/widthdb.rs b/src/widthdb.rs index 9c91879..6af28c3 100644 --- a/src/widthdb.rs +++ b/src/widthdb.rs @@ -65,7 +65,7 @@ impl WidthDB { .queue(MoveTo(0, 0))? .queue(Print(&grapheme))?; out.flush()?; - let width = crossterm::cursor::position()?.0.max(1) as u8; + let width = crossterm::cursor::position()?.0 as u8; self.known.insert(grapheme, width); } Ok(())