Skip to content

Commit 2213b7b

Browse files
committed
fix(ui): handle non-ascii chars correctly
Fixes both issues described in #77.
1 parent c7b7717 commit 2213b7b

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/ui.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,15 @@ impl ThemedWidget for &Test {
125125
.starts_with(&self.words[self.current_word].progress[..]);
126126

127127
let (typed, untyped) =
128-
self.words[self.current_word].text.split_at(progress_ind);
128+
self.words[self.current_word]
129+
.text
130+
.split_at(ceil_char_boundary(
131+
&self.words[self.current_word].text,
132+
progress_ind,
133+
));
129134

130-
let untyped_formatted = format!("{} ", untyped);
131-
let (cursor, remaining) = untyped_formatted.split_at(1);
135+
let mut remaining = untyped.chars().chain(iter::once(' '));
136+
let cursor = remaining.next().unwrap();
132137

133138
iter::once(vec![
134139
Span::styled(
@@ -140,10 +145,10 @@ impl ThemedWidget for &Test {
140145
},
141146
),
142147
Span::styled(
143-
cursor.to_owned(),
148+
cursor.to_string(),
144149
theme.prompt_current_untyped.patch(theme.prompt_cursor),
145150
),
146-
Span::styled(remaining.to_owned(), theme.prompt_current_untyped),
151+
Span::styled(remaining.collect::<String>(), theme.prompt_current_untyped),
147152
])
148153
})
149154
// remaining words
@@ -323,3 +328,12 @@ impl ThemedWidget for &results::Results {
323328
wpm_chart.render(res_chunks[1], buf);
324329
}
325330
}
331+
332+
// FIXME: replace with `str::ceil_char_boundary` when stable
333+
fn ceil_char_boundary(string: &str, index: usize) -> usize {
334+
if string.is_char_boundary(index) {
335+
index
336+
} else {
337+
ceil_char_boundary(string, index + 1)
338+
}
339+
}

0 commit comments

Comments
 (0)