1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

fix sized_string chopping string at wrong boundaries

This commit is contained in:
rabite 2020-01-28 01:29:58 +01:00
parent 025a40044c
commit 049b1d1c74

View File

@ -221,24 +221,25 @@ pub fn cell_ratio() -> HResult<f32> {
} }
pub fn sized_string(string: &str, xsize: u16) -> &str { pub fn sized_string(string: &str, xsize: u16) -> &str {
let len = string.char_indices() let len = string.chars()
.map(|(i, ch)| { .map(|ch| {
if ch.is_ascii() { if ch.is_ascii() {
(i, 1) (1, 1)
} else { } else {
(i, UnicodeWidthChar::width(ch).unwrap_or(0)) (UnicodeWidthChar::width(ch).unwrap_or(0), ch.len_utf8())
} }
}) })
.scan(0, |slen, (i, chlen)| { .scan((0,0), |(str_width, str_len), (ch_width, ch_len)| {
*slen += chlen; *str_width += ch_width;
Some((i, *slen)) *str_len += ch_len;
Some((*str_width, *str_len))
}) })
.take_while(|(_, slen)| slen < &(xsize as usize)) .take_while(|(str_width, _)| *str_width < xsize as usize)
.map(|(i,_)| i) .map(|(_, str_len)| str_len)
.last() .last()
.unwrap_or(0); .unwrap_or(0);
&string[0..len+1] &string[0..len]
} }
#[derive(Debug)] #[derive(Debug)]