From 8556fd8176d234df6910037c51561203c19ff149 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 25 Apr 2024 19:45:35 +0200 Subject: [PATCH] Fix control character width measurement --- CHANGELOG.md | 3 +++ src/widthdb.rs | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e012aa..a01f614 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Procedure when bumping the version number: ## Unreleased +### Fixed +- Width measurements of ASCII control characters + ## v0.2.2 - 2024-01-14 ### Fixed diff --git a/src/widthdb.rs b/src/widthdb.rs index 7d18570..200765f 100644 --- a/src/widthdb.rs +++ b/src/widthdb.rs @@ -101,6 +101,15 @@ impl WidthDb { return Ok(()); } for grapheme in self.requested.drain() { + if grapheme.chars().any(|c|c.is_ascii_control()){ + // ASCII control characters like the escape character or the + // bell character tend to be interpreted specially by terminals. + // This may break width measurements. To avoid this, we just + // assign each control character a with of 0. + self.known.insert(grapheme, 0); + continue; + } + out.queue(Clear(ClearType::All))? .queue(MoveTo(0, 0))? .queue(Print(&grapheme))?;