From 3cc26d7dd908659d34b9a6f5d91c6e3702c27155 Mon Sep 17 00:00:00 2001 From: Teoh Han Hui Date: Thu, 11 Jul 2024 00:35:11 +0800 Subject: [PATCH] Fix wrong line length counting of ascii art Count Unicode grapheme clusters instead of bytes. --- crates/hyfetch/src/neofetch_util.rs | 7 ++++++- crates/hyfetch/src/presets.rs | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/hyfetch/src/neofetch_util.rs b/crates/hyfetch/src/neofetch_util.rs index dadedff6..7612767e 100644 --- a/crates/hyfetch/src/neofetch_util.rs +++ b/crates/hyfetch/src/neofetch_util.rs @@ -16,6 +16,7 @@ use normpath::PathExt as _; use serde::{Deserialize, Serialize}; use tempfile::NamedTempFile; use tracing::debug; +use unicode_segmentation::UnicodeSegmentation; use crate::color_util::{ color, ForegroundBackground, NeofetchAsciiIndexedColor, PresetIndexedColor, ToAnsiString, @@ -455,7 +456,11 @@ where ac.replace_all(asc, &REPLACEMENTS) }; - let Some(width) = asc.split('\n').map(|line| line.len()).max() else { + let Some(width) = asc + .split('\n') + .map(|line| line.graphemes(true).count()) + .max() + else { unreachable!(); }; let width: u8 = width.try_into().expect("`width` should fit in `u8`"); diff --git a/crates/hyfetch/src/presets.rs b/crates/hyfetch/src/presets.rs index ac92547d..29e84187 100644 --- a/crates/hyfetch/src/presets.rs +++ b/crates/hyfetch/src/presets.rs @@ -482,6 +482,8 @@ impl ColorProfile { { let txt = txt.as_ref(); + let txt: Vec<&str> = txt.graphemes(true).collect(); + let ColorProfile { colors } = { let length = txt.len(); let length: u8 = length.try_into().expect("`length` should fit in `u8`"); @@ -490,7 +492,6 @@ impl ColorProfile { }; let mut buf = String::new(); - let txt: Vec<&str> = txt.graphemes(true).collect(); for (i, &gr) in txt.iter().enumerate() { if space_only && gr != " " { if i > 0 && txt[i - 1] == " " {