Fix wrong line length counting of ascii art

Count Unicode grapheme clusters instead of bytes.
This commit is contained in:
Teoh Han Hui 2024-07-11 00:35:11 +08:00
parent 85a85ad4d3
commit 3cc26d7dd9
No known key found for this signature in database
GPG key ID: D43E2BABAF97DCAE
2 changed files with 8 additions and 2 deletions

View file

@ -16,6 +16,7 @@ use normpath::PathExt as _;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tracing::debug; use tracing::debug;
use unicode_segmentation::UnicodeSegmentation;
use crate::color_util::{ use crate::color_util::{
color, ForegroundBackground, NeofetchAsciiIndexedColor, PresetIndexedColor, ToAnsiString, color, ForegroundBackground, NeofetchAsciiIndexedColor, PresetIndexedColor, ToAnsiString,
@ -455,7 +456,11 @@ where
ac.replace_all(asc, &REPLACEMENTS) 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!(); unreachable!();
}; };
let width: u8 = width.try_into().expect("`width` should fit in `u8`"); let width: u8 = width.try_into().expect("`width` should fit in `u8`");

View file

@ -482,6 +482,8 @@ impl ColorProfile {
{ {
let txt = txt.as_ref(); let txt = txt.as_ref();
let txt: Vec<&str> = txt.graphemes(true).collect();
let ColorProfile { colors } = { let ColorProfile { colors } = {
let length = txt.len(); let length = txt.len();
let length: u8 = length.try_into().expect("`length` should fit in `u8`"); let length: u8 = length.try_into().expect("`length` should fit in `u8`");
@ -490,7 +492,6 @@ impl ColorProfile {
}; };
let mut buf = String::new(); let mut buf = String::new();
let txt: Vec<&str> = txt.graphemes(true).collect();
for (i, &gr) in txt.iter().enumerate() { for (i, &gr) in txt.iter().enumerate() {
if space_only && gr != " " { if space_only && gr != " " {
if i > 0 && txt[i - 1] == " " { if i > 0 && txt[i - 1] == " " {