Skip to content

Commit

Permalink
feat(ui): add cursor style
Browse files Browse the repository at this point in the history
  • Loading branch information
max-niederman committed Apr 6, 2023
1 parent 75355ae commit 5a37ce8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub struct Theme {
#[serde(deserialize_with = "deserialize_style")]
pub prompt_current_untyped: Style,

#[serde(deserialize_with = "deserialize_style")]
pub prompt_cursor: Style,

// results widget
#[serde(deserialize_with = "deserialize_style")]
pub results_overview: Style,
Expand Down Expand Up @@ -94,6 +97,8 @@ impl Default for Theme {
.fg(Color::Blue)
.add_modifier(Modifier::BOLD),

prompt_cursor: Style::default().add_modifier(Modifier::UNDERLINED),

results_overview: Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ impl Opt {
.language_file
.as_ref()
.map(fs::read)
.map(Result::ok)
.flatten()
.and_then(Result::ok)
.or_else(|| fs::read(self.language_dir().join(&lang_name)).ok())
.or_else(|| {
Resources::get(&format!("language/{}", &lang_name))
Expand Down
68 changes: 38 additions & 30 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ impl ThemedWidget for &Test {
input.render(buf);

let target_lines: Vec<Spans> = {
let progress_ind = self.words[self.current_word]
.progress
.len()
.min(self.words[self.current_word].text.len());
let words = iter::empty::<Vec<Span>>()
// already typed words
.chain(self.words[..self.current_word].iter().map(|w| {
vec![Span::styled(
w.text.clone() + " ",
Expand All @@ -116,32 +113,43 @@ impl ThemedWidget for &Test {
},
)]
}))
.chain(iter::once(vec![
Span::styled(
self.words[self.current_word]
.text
.chars()
.take(progress_ind)
.collect::<String>(),
if self.words[self.current_word]
.text
.starts_with(&self.words[self.current_word].progress[..])
{
theme.prompt_current_correct
} else {
theme.prompt_current_incorrect
},
),
Span::styled(
self.words[self.current_word]
.text
.chars()
.skip(progress_ind)
.collect::<String>()
+ " ",
theme.prompt_current_untyped,
),
]))
// current word
.chain({
let progress_ind = self.words[self.current_word]
.progress
.len()
.min(self.words[self.current_word].text.len());

let correct = self.words[self.current_word]
.text
.starts_with(&self.words[self.current_word].progress[..]);

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

let untyped_formatted = format!("{} ", untyped);
let (cursor, remaining) = untyped_formatted.split_at(1);

iter::once(vec![
Span::styled(
typed,
if correct {
theme.prompt_current_correct
} else {
theme.prompt_current_incorrect
},
),
Span::styled(
cursor.to_owned(),
theme.prompt_current_untyped.patch(theme.prompt_cursor),
),
Span::styled(
remaining.to_owned(),
theme.prompt_current_untyped,
),
])
})
// remaining words
.chain(
self.words[self.current_word + 1..]
.iter()
Expand Down

0 comments on commit 5a37ce8

Please sign in to comment.