initial commit
This commit is contained in:
commit
dfc5cdd7cf
6 files changed
+3797
No files matched your search
+549
@@ -0,0 +1,549 @@
|
||||
use pancurses::{
|
||||
A_BOLD, A_REVERSE, COLOR_PAIR, Input, Window, curs_set, endwin, init_pair, initscr, noecho,
|
||||
start_color,
|
||||
};
|
||||
use rand::seq::SliceRandom;
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
const TYPED_COLOR: i16 = 1;
|
||||
const UNTYPED_COLOR: i16 = 2;
|
||||
const MISTAKE_COLOR: i16 = 3;
|
||||
const SELECTED_COLOR: i16 = 4;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum TestType {
|
||||
Time(f64), // Duration in seconds
|
||||
Words(usize), // Number of words
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Test {
|
||||
pos: usize,
|
||||
start_time: u128,
|
||||
finish_time: Option<u128>,
|
||||
keystrokes_count: usize,
|
||||
correct_keystrokes: usize,
|
||||
type_tape: VecDeque<char>,
|
||||
tape: VecDeque<char>,
|
||||
mistakes: usize,
|
||||
test_type: TestType,
|
||||
words_typed: usize, // For word count mode
|
||||
}
|
||||
|
||||
impl Test {
|
||||
fn new(test_type: TestType) -> Self {
|
||||
Test {
|
||||
pos: 0,
|
||||
start_time: SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis(),
|
||||
finish_time: None,
|
||||
keystrokes_count: 0,
|
||||
correct_keystrokes: 0,
|
||||
type_tape: VecDeque::with_capacity(256),
|
||||
tape: VecDeque::with_capacity(256),
|
||||
mistakes: 0,
|
||||
test_type,
|
||||
words_typed: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self) {
|
||||
self.finish_time = Some(
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis(),
|
||||
);
|
||||
}
|
||||
|
||||
fn duration(&self) -> f64 {
|
||||
let end_time = match self.finish_time {
|
||||
Some(t) => t,
|
||||
None => SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis(),
|
||||
};
|
||||
(end_time - self.start_time) as f64 / 1000.0
|
||||
}
|
||||
|
||||
fn wpm(&self) -> f64 {
|
||||
let duration = self.duration();
|
||||
if duration == 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
(self.correct_keystrokes as f64 / duration) * 60.0 / 5.0
|
||||
}
|
||||
|
||||
fn accuracy(&self) -> f64 {
|
||||
if self.keystrokes_count == 0 {
|
||||
return 100.0;
|
||||
}
|
||||
(self.correct_keystrokes as f64 / self.keystrokes_count as f64) * 100.0
|
||||
}
|
||||
|
||||
fn write(&mut self, ch: char) {
|
||||
if self.finish_time.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.keystrokes_count += 1;
|
||||
|
||||
if let Some(&expected) = self.tape.front() {
|
||||
if ch == expected {
|
||||
self.correct_keystrokes += 1;
|
||||
self.type_tape.push_back(ch);
|
||||
self.tape.pop_front();
|
||||
self.pos += 1;
|
||||
|
||||
// Check if we completed a word (space character)
|
||||
if ch == ' ' {
|
||||
self.words_typed += 1;
|
||||
}
|
||||
|
||||
// Check completion condition
|
||||
match self.test_type {
|
||||
TestType::Time(_) => {
|
||||
// Time-based test - no early completion
|
||||
}
|
||||
TestType::Words(word_count) => {
|
||||
if self.words_typed >= word_count {
|
||||
self.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.mistakes += 1;
|
||||
// Don't finish test on mistake, just count it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn backspace(&mut self) {
|
||||
if self.finish_time.is_some() || self.type_tape.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(last_char) = self.type_tape.pop_back() {
|
||||
self.tape.push_front(last_char);
|
||||
self.pos -= 1;
|
||||
// Don't decrease keystrokes_count for backspace, but adjust correct count
|
||||
if self.correct_keystrokes > 0 {
|
||||
self.correct_keystrokes -= 1;
|
||||
}
|
||||
|
||||
// If we're removing a space, decrement words_typed
|
||||
if last_char == ' ' && self.words_typed > 0 {
|
||||
self.words_typed -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_running(&self) -> bool {
|
||||
if self.finish_time.is_some() {
|
||||
return false;
|
||||
}
|
||||
|
||||
match self.test_type {
|
||||
TestType::Time(duration) => self.duration() < duration,
|
||||
TestType::Words(word_count) => self.words_typed < word_count,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_progress(&self) -> (String, String) {
|
||||
match self.test_type {
|
||||
TestType::Time(duration) => {
|
||||
let remaining = duration - self.duration();
|
||||
let progress = format!("Time: {:.1}s", remaining.max(0.0));
|
||||
let target = format!("Target: {:.0}s", duration);
|
||||
(progress, target)
|
||||
}
|
||||
TestType::Words(word_count) => {
|
||||
let progress = format!("Words: {}/{}", self.words_typed, word_count);
|
||||
let target = format!("Target: {} words", word_count);
|
||||
(progress, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render(&self, window: &Window) {
|
||||
let (_max_y, max_x) = window.get_max_yx();
|
||||
let cc = (max_x / 2) as i32;
|
||||
|
||||
// Show progress based on test type
|
||||
let (progress_str, target_str) = self.get_progress();
|
||||
window.mv(5, cc - (progress_str.len() as i32 / 2));
|
||||
window.addstr(&progress_str);
|
||||
|
||||
window.mv(4, cc - (target_str.len() as i32 / 2));
|
||||
window.addstr(&target_str);
|
||||
|
||||
let typed_len = self.type_tape.len().min(cc as usize - 1);
|
||||
let untyped_len = (cc - 1) as usize;
|
||||
|
||||
let typed_str: String = self
|
||||
.type_tape
|
||||
.iter()
|
||||
.skip(self.type_tape.len().saturating_sub(typed_len))
|
||||
.take(typed_len)
|
||||
.collect();
|
||||
|
||||
let untyped_str: String = self.tape.iter().take(untyped_len).collect();
|
||||
|
||||
window.attron(COLOR_PAIR(UNTYPED_COLOR as u32));
|
||||
window.mv(10, cc);
|
||||
window.addstr(&untyped_str);
|
||||
window.attroff(COLOR_PAIR(UNTYPED_COLOR as u32));
|
||||
|
||||
window.attron(COLOR_PAIR(TYPED_COLOR as u32));
|
||||
let typed_pos = cc - typed_len as i32;
|
||||
window.mv(10, typed_pos);
|
||||
window.addstr(&typed_str);
|
||||
window.attroff(COLOR_PAIR(TYPED_COLOR as u32));
|
||||
|
||||
// Show current character with highlight
|
||||
window.attron(A_REVERSE | A_BOLD);
|
||||
if let Some(&ch) = self.tape.front() {
|
||||
window.mv(9, cc);
|
||||
window.addch('|');
|
||||
window.mv(10, cc);
|
||||
window.addch(ch);
|
||||
window.mv(11, cc);
|
||||
window.addch('|');
|
||||
}
|
||||
window.attroff(A_REVERSE | A_BOLD);
|
||||
|
||||
// Show stats
|
||||
let wpm_str = format!("WPM: {:.1}", self.wpm());
|
||||
let accuracy_str = format!("Accuracy: {:.1}%", self.accuracy());
|
||||
let mistakes_str = format!("Mistakes: {}", self.mistakes);
|
||||
|
||||
window.mv(7, cc - 15);
|
||||
window.addstr(&wpm_str);
|
||||
window.mv(7, cc + 2);
|
||||
window.addstr(&accuracy_str);
|
||||
window.mv(8, cc - 8);
|
||||
window.addstr(&mistakes_str);
|
||||
|
||||
// Show instructions
|
||||
window.mv(12, cc - 15);
|
||||
window.addstr("Press ESC to return to menu");
|
||||
}
|
||||
|
||||
fn show_result(&self, window: &Window) {
|
||||
window.clear();
|
||||
let (max_y, max_x) = window.get_max_yx();
|
||||
let cc = (max_x / 2) as i32;
|
||||
let lc = (max_y / 2) as i32;
|
||||
|
||||
// Draw box
|
||||
for i in 0..max_x {
|
||||
window.mv(0, i);
|
||||
window.addch('#');
|
||||
window.mv(max_y - 1, i);
|
||||
window.addch('#');
|
||||
}
|
||||
for i in 0..max_y {
|
||||
window.mv(i, 0);
|
||||
window.addch('#');
|
||||
window.mv(i, max_x - 1);
|
||||
window.addch('#');
|
||||
}
|
||||
|
||||
let wpm_str = format!("{:.1}", self.wpm());
|
||||
let time_str = format!("{:.1}", self.duration());
|
||||
let keystrokes_str = format!("{}", self.keystrokes_count);
|
||||
let accuracy_str = format!("{:.1}%", self.accuracy());
|
||||
let mistakes_str = format!("{}", self.mistakes);
|
||||
|
||||
// Show test type in results
|
||||
let test_type_str = match self.test_type {
|
||||
TestType::Time(duration) => format!("{:.0} seconds", duration),
|
||||
TestType::Words(word_count) => format!("{} words", word_count),
|
||||
};
|
||||
|
||||
window.mv(lc - 4, cc - 3);
|
||||
window.addstr("Test Type: ");
|
||||
window.mv(lc - 4, cc + 3);
|
||||
window.addstr(&test_type_str);
|
||||
|
||||
window.mv(lc - 3, cc - 3);
|
||||
window.addstr("WPM : ");
|
||||
window.mv(lc - 3, cc + 3);
|
||||
window.addstr(&wpm_str);
|
||||
|
||||
window.mv(lc - 2, cc - 4);
|
||||
window.addstr("Time : ");
|
||||
window.mv(lc - 2, cc + 3);
|
||||
window.addstr(&time_str);
|
||||
|
||||
window.mv(lc - 1, cc - 10);
|
||||
window.addstr("Keystrokes : ");
|
||||
window.mv(lc - 1, cc + 3);
|
||||
window.addstr(&keystrokes_str);
|
||||
|
||||
window.mv(lc, cc - 10);
|
||||
window.addstr("Accuracy : ");
|
||||
window.mv(lc, cc + 3);
|
||||
window.addstr(&accuracy_str);
|
||||
|
||||
window.mv(lc + 1, cc - 10);
|
||||
window.addstr("Mistakes : ");
|
||||
window.mv(lc + 1, cc + 3);
|
||||
window.addstr(&mistakes_str);
|
||||
|
||||
window.mv(lc + 3, cc - 15);
|
||||
window.addstr("Press 'R' to restart or 'M' to return to menu");
|
||||
|
||||
window.mv(lc + 4, cc - 10);
|
||||
window.addstr("Press 'Q' to quit the program");
|
||||
|
||||
window.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
fn read_words() -> io::Result<Vec<String>> {
|
||||
let file = File::open("words.txt")?;
|
||||
let words: Vec<String> = io::BufReader::new(file)
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect();
|
||||
Ok(words)
|
||||
}
|
||||
|
||||
fn show_test_menu(window: &Window) -> Option<TestType> {
|
||||
let time_options = [
|
||||
("15 seconds", TestType::Time(15.0)),
|
||||
("30 seconds", TestType::Time(30.0)),
|
||||
("1 minute", TestType::Time(60.0)),
|
||||
("2 minutes", TestType::Time(120.0)),
|
||||
("5 minutes", TestType::Time(300.0)),
|
||||
];
|
||||
|
||||
let word_options = [
|
||||
("15 words", TestType::Words(15)),
|
||||
("30 words", TestType::Words(30)),
|
||||
("60 words", TestType::Words(60)),
|
||||
("120 words", TestType::Words(120)),
|
||||
("240 words", TestType::Words(240)),
|
||||
];
|
||||
|
||||
let mut selected = 2; // Default to 1 minute
|
||||
let mut in_time_mode = true;
|
||||
|
||||
loop {
|
||||
window.clear();
|
||||
let (max_y, max_x) = window.get_max_yx();
|
||||
let cc = (max_x / 2) as i32;
|
||||
let lc = (max_y / 2) as i32;
|
||||
|
||||
// Draw box
|
||||
for i in 0..max_x {
|
||||
window.mv(0, i);
|
||||
window.addch('#');
|
||||
window.mv(max_y - 1, i);
|
||||
window.addch('#');
|
||||
}
|
||||
for i in 0..max_y {
|
||||
window.mv(i, 0);
|
||||
window.addch('#');
|
||||
window.mv(i, max_x - 1);
|
||||
window.addch('#');
|
||||
}
|
||||
|
||||
window.mv(lc - 7, cc - 10);
|
||||
window.addstr("Select Test Type and Duration");
|
||||
|
||||
// Show mode selection
|
||||
window.mv(lc - 5, cc - 15);
|
||||
if in_time_mode {
|
||||
window.attron(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
window.addstr("> Time Mode <");
|
||||
window.attroff(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
window.mv(lc - 5, cc + 3);
|
||||
window.addstr("Word Mode");
|
||||
} else {
|
||||
window.mv(lc - 5, cc - 15);
|
||||
window.addstr("Time Mode");
|
||||
window.attron(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
window.mv(lc - 5, cc + 3);
|
||||
window.addstr("> Word Mode <");
|
||||
window.attroff(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
}
|
||||
|
||||
// Show options based on mode
|
||||
let options = if in_time_mode {
|
||||
&time_options
|
||||
} else {
|
||||
&word_options
|
||||
};
|
||||
|
||||
for (i, (name, _)) in options.iter().enumerate() {
|
||||
let y = lc - 3 + i as i32;
|
||||
if i == selected {
|
||||
window.attron(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
window.mv(y, cc - 8);
|
||||
window.addstr(&format!("> {} <", name));
|
||||
window.attroff(COLOR_PAIR(SELECTED_COLOR as u32));
|
||||
} else {
|
||||
window.mv(y, cc - 6);
|
||||
window.addstr(name);
|
||||
}
|
||||
}
|
||||
|
||||
window.mv(lc + 3, cc - 20);
|
||||
window.addstr("Use LEFT/RIGHT to switch mode, UP/DOWN to select");
|
||||
window.mv(lc + 4, cc - 15);
|
||||
window.addstr("ENTER to confirm, Q to quit");
|
||||
|
||||
window.refresh();
|
||||
|
||||
match window.getch() {
|
||||
Some(Input::KeyUp) => {
|
||||
if selected > 0 {
|
||||
selected -= 1;
|
||||
}
|
||||
}
|
||||
Some(Input::KeyDown) => {
|
||||
if selected < options.len() - 1 {
|
||||
selected += 1;
|
||||
}
|
||||
}
|
||||
Some(Input::KeyLeft) | Some(Input::KeyRight) => {
|
||||
in_time_mode = !in_time_mode;
|
||||
selected = 2; // Reset to middle option when switching modes
|
||||
}
|
||||
Some(Input::Character('\n')) | Some(Input::Character('\r')) => {
|
||||
let options = if in_time_mode {
|
||||
&time_options
|
||||
} else {
|
||||
&word_options
|
||||
};
|
||||
return Some(options[selected].1);
|
||||
}
|
||||
Some(Input::Character('q')) | Some(Input::Character('Q')) => {
|
||||
return None;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_typing_test(
|
||||
words: &Vec<String>,
|
||||
window: &Window,
|
||||
test_type: TestType,
|
||||
) -> io::Result<Option<bool>> {
|
||||
let mut test = Test::new(test_type);
|
||||
|
||||
// Fill initial tape
|
||||
while test.tape.len() < 200 {
|
||||
if let Some(word) = words.choose(&mut rand::thread_rng()) {
|
||||
test.tape.extend(word.chars());
|
||||
test.tape.push_back(' ');
|
||||
}
|
||||
}
|
||||
|
||||
while test.is_running() {
|
||||
window.clear();
|
||||
|
||||
// Add more words if needed (only for time-based tests)
|
||||
if let TestType::Time(_) = test.test_type {
|
||||
while test.tape.len() < 100 {
|
||||
if let Some(word) = words.choose(&mut rand::thread_rng()) {
|
||||
test.tape.extend(word.chars());
|
||||
test.tape.push_back(' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test.render(&window);
|
||||
window.refresh();
|
||||
|
||||
match window.getch() {
|
||||
Some(Input::Character(ch)) => {
|
||||
match ch {
|
||||
'\x1b' => return Ok(Some(false)), // ESC to return to menu
|
||||
'\x7f' | '\x08' => test.backspace(), // backspace
|
||||
_ => test.write(ch),
|
||||
}
|
||||
}
|
||||
Some(Input::KeyResize) => {
|
||||
// Handle terminal resize
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Test finished - show results
|
||||
test.show_result(&window);
|
||||
|
||||
// Wait for user input to restart or quit
|
||||
loop {
|
||||
match window.getch() {
|
||||
Some(Input::Character('r')) | Some(Input::Character('R')) => return Ok(Some(true)),
|
||||
Some(Input::Character('m')) | Some(Input::Character('M')) => return Ok(Some(false)),
|
||||
Some(Input::Character('q')) | Some(Input::Character('Q')) => return Ok(None),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let words = read_words()?;
|
||||
if words.is_empty() {
|
||||
eprintln!("No words found in words.txt");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let window = initscr();
|
||||
noecho();
|
||||
curs_set(0);
|
||||
window.keypad(true);
|
||||
start_color();
|
||||
init_pair(TYPED_COLOR, pancurses::COLOR_WHITE, pancurses::COLOR_BLACK);
|
||||
init_pair(
|
||||
UNTYPED_COLOR,
|
||||
pancurses::COLOR_GREEN,
|
||||
pancurses::COLOR_BLACK,
|
||||
);
|
||||
init_pair(MISTAKE_COLOR, pancurses::COLOR_RED, pancurses::COLOR_WHITE);
|
||||
init_pair(
|
||||
SELECTED_COLOR,
|
||||
pancurses::COLOR_BLACK,
|
||||
pancurses::COLOR_WHITE,
|
||||
);
|
||||
|
||||
// Main loop - show menu and run tests
|
||||
loop {
|
||||
// Show test selection menu
|
||||
let test_type = match show_test_menu(&window) {
|
||||
Some(test_type) => test_type,
|
||||
None => break, // User chose to quit
|
||||
};
|
||||
|
||||
// Run typing test
|
||||
match run_typing_test(&words, &window, test_type)? {
|
||||
Some(true) => {
|
||||
// Restart with same test type
|
||||
continue;
|
||||
}
|
||||
Some(false) => {
|
||||
// Return to menu
|
||||
continue;
|
||||
}
|
||||
None => {
|
||||
// Quit
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user