typeracer 2.1.1

A terminal typing game. Race to see the fastest time you can get!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use crossbeam_channel::Sender;
use std::{io::stdin, thread};
use termion::{event::Key, input::TermRead};

pub fn capture(sender: Sender<Key>) {
    thread::spawn(|| capture_internal(sender));
}

fn capture_internal(sender: Sender<Key>) {
    let stdin = &mut stdin();
    loop {
        match sender.send(stdin.keys().find_map(Result::ok).unwrap()) {
            Ok(_) => (), // continue just fine
            Err(_) => return, // sender thread has died, either we have quit the game or the thread has died
                              // either way we need to exit
        };
    }
}