Skip to content

Commit

Permalink
fix(ui): handle non-ascii chars correctly
Browse files Browse the repository at this point in the history
Fixes both issues described in #77.
  • Loading branch information
max-niederman committed May 2, 2023
1 parent c7b7717 commit 2213b7b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ impl ThemedWidget for &Test {
.starts_with(&self.words[self.current_word].progress[..]);

let (typed, untyped) =
self.words[self.current_word].text.split_at(progress_ind);
self.words[self.current_word]
.text
.split_at(ceil_char_boundary(
&self.words[self.current_word].text,
progress_ind,
));

let untyped_formatted = format!("{} ", untyped);
let (cursor, remaining) = untyped_formatted.split_at(1);
let mut remaining = untyped.chars().chain(iter::once(' '));
let cursor = remaining.next().unwrap();

iter::once(vec![
Span::styled(
Expand All @@ -140,10 +145,10 @@ impl ThemedWidget for &Test {
},
),
Span::styled(
cursor.to_owned(),
cursor.to_string(),
theme.prompt_current_untyped.patch(theme.prompt_cursor),
),
Span::styled(remaining.to_owned(), theme.prompt_current_untyped),
Span::styled(remaining.collect::<String>(), theme.prompt_current_untyped),
])
})
// remaining words
Expand Down Expand Up @@ -323,3 +328,12 @@ impl ThemedWidget for &results::Results {
wpm_chart.render(res_chunks[1], buf);
}
}

// FIXME: replace with `str::ceil_char_boundary` when stable
fn ceil_char_boundary(string: &str, index: usize) -> usize {
if string.is_char_boundary(index) {
index
} else {
ceil_char_boundary(string, index + 1)
}
}

0 comments on commit 2213b7b

Please sign in to comment.