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 {
let len = string.char_indices()
.map(|(i, ch)| {
let len = string.chars()
.map(|ch| {
if ch.is_ascii() {
(i, 1)
(1, 1)
} else {
(i, UnicodeWidthChar::width(ch).unwrap_or(0))
(UnicodeWidthChar::width(ch).unwrap_or(0), ch.len_utf8())
}
})
.scan(0, |slen, (i, chlen)| {
*slen += chlen;
Some((i, *slen))
.scan((0,0), |(str_width, str_len), (ch_width, ch_len)| {
*str_width += ch_width;
*str_len += ch_len;
Some((*str_width, *str_len))
})
.take_while(|(_, slen)| slen < &(xsize as usize))
.map(|(i,_)| i)
.take_while(|(str_width, _)| *str_width < xsize as usize)
.map(|(_, str_len)| str_len)
.last()
.unwrap_or(0);
&string[0..len+1]
&string[0..len]
}
#[derive(Debug)]