Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark individual incorrect characters #92

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 84 additions & 50 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,52 +104,95 @@ impl ThemedWidget for &Test {
let words = iter::empty::<Vec<Span>>()
// already typed words
.chain(self.words[..self.current_word].iter().map(|w| {
vec![Span::styled(
w.text.clone() + " ",
if w.progress == w.text {
theme.prompt_correct
let text = &mut w.text.chars();
let prog = &mut w.progress.chars();
let mut display = vec![];
'checking: loop {
if let Some(t) = text.next() {
if let Some(p) = prog.next() {
display.push(
Span::styled(
t.to_string(),
if t == p {
theme.prompt_correct
} else {
theme.prompt_incorrect
}
),
);
} else {
display.push(
Span::styled(t.to_string(), theme.prompt_untyped),
);
}
} else {
theme.prompt_incorrect
},
)]
if let Some(p) = prog.next() {
display.push(
Span::styled(p.to_string(), theme.prompt_incorrect),
);
} else {
display.push(
Span::styled(" ", theme.prompt_untyped),
);
break 'checking;
}
}
}
display
}))
// 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(ceil_char_boundary(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in cargo throwing the warning:

warning: function `ceil_char_boundary` is never used
   --> src/ui.rs:346:4
    |
346 | fn ceil_char_boundary(string: &str, index: usize) -> usize {
    |    ^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the unused code.

&self.words[self.current_word].text,
progress_ind,
));

let mut remaining = untyped.chars().chain(iter::once(' '));
let cursor = remaining.next().unwrap();

iter::once(vec![
Span::styled(
typed,
if correct {
theme.prompt_current_correct
let text = &mut self.words[self.current_word].text.chars();
let prog = &mut self.words[self.current_word].progress.chars();
let mut display = vec![];
let mut cursor_showed = false;
'checking: loop {
if let Some(t) = text.next() {
if let Some(p) = prog.next() {
display.push(
Span::styled(
t.to_string(),
if t == p {
theme.prompt_current_correct
} else {
theme.prompt_current_incorrect
}
),
);
} else {
display.push(
Span::styled(
t.to_string(),
if cursor_showed {
theme.prompt_current_untyped
} else {
cursor_showed = true;
theme.prompt_current_untyped.patch(theme.prompt_cursor)
}
),
);
}
} else {
if let Some(p) = prog.next() {
display.push(
Span::styled(p.to_string(), theme.prompt_current_incorrect),
);
} else {
theme.prompt_current_incorrect
},
),
Span::styled(
cursor.to_string(),
theme.prompt_current_untyped.patch(theme.prompt_cursor),
),
Span::styled(remaining.collect::<String>(), theme.prompt_current_untyped),
])
display.push(
Span::styled(
" ",
if cursor_showed {
theme.prompt_current_untyped
} else {
theme.prompt_current_untyped.patch(theme.prompt_cursor)
}
),
);
break 'checking;
}
}
};
iter::once(display)
})
// remaining words
.chain(
Expand Down Expand Up @@ -328,12 +371,3 @@ 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)
}
}