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 1 commit
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
67 changes: 40 additions & 27 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,40 +115,53 @@ impl ThemedWidget for &Test {
}))
// 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();
let text = &self.words[self.current_word].text;
let prog = &self.words[self.current_word].progress;
let text_count = text.chars().count();
let prog_count = prog.chars().count();

// Find the index for the first incorrect character
let mut incorrect_ind = 0;
for (idx, chr) in prog.char_indices() {
if !text.get(idx..).unwrap().starts_with(chr) {
break;
}
incorrect_ind = idx + chr.len_utf8();
}

let (cursor_ind, chr) = text.char_indices()
.nth(prog_count.min(text_count))
.unwrap_or((text.len(), '\0'));
let untyped_ind = cursor_ind + chr.len_utf8();

iter::once(vec![
Span::styled(
typed,
if correct {
theme.prompt_current_correct
} else {
theme.prompt_current_incorrect
},
text.get(..incorrect_ind).unwrap(),
theme.prompt_current_correct,
),
Span::styled(
text.get(incorrect_ind..cursor_ind).unwrap(),
theme.prompt_current_incorrect,
),
Span::styled(
cursor.to_string(),
text.get(cursor_ind..untyped_ind).unwrap_or(""),
theme.prompt_current_untyped.patch(theme.prompt_cursor),
),
Span::styled(remaining.collect::<String>(), theme.prompt_current_untyped),
Span::styled(
text.get(untyped_ind..).unwrap_or(""),
theme.prompt_current_untyped,
),
Span::styled(
" ", // Delimiting space
if prog_count > text_count {
// There are redundant letters at the end
theme.prompt_current_incorrect.patch(theme.prompt_cursor)
} else if prog_count == text_count {
theme.prompt_current_untyped.patch(theme.prompt_cursor)
} else {
theme.prompt_current_untyped
},
),
])
})
// remaining words
Expand Down