typist

Simple typing tutor
git clone git://git.alexkarle.com/typist.git
Log | Files | Refs | README | LICENSE

typist (1087B) [raw]


      1 #!/usr/bin/env perl
      2 # typist -- simple typing tutor
      3 use strict;
      4 use warnings;
      5 use List::Util qw(shuffle);
      6 
      7 main();
      8 
      9 sub main {
     10     # Load in lines
     11     my @lines;
     12     while (my $l = <ARGV>) {
     13         chomp $l;
     14         $l =~ s/^\s*//;
     15         $l =~ s/\s*$//;
     16         if ($l) {
     17             push(@lines, $l);
     18         }
     19     }
     20     @lines = shuffle @lines;
     21 
     22     # We've now EITHER hit the end of stdin or read in all the files
     23     # So we reopen /dev/tty as our user interface
     24     open(my $ttyfh, '<', '/dev/tty') or die "unable to open tty: $!";
     25     my $trys = 1;
     26     while (my $item = shift @lines) { 
     27         print "$item\n";
     28         my $t = time;
     29         my $ans = <$ttyfh>;
     30         last if !$ans;
     31         chomp $ans;
     32         if ($ans eq $item) {
     33             my $elap = time - $t;
     34             my $wpm =  scalar(split(' ', $item)) * 60.0 / $elap;
     35             printf "Correct! Took %d tries (%d seconds, %d WPM)\n", $trys, $elap, $wpm;
     36             $trys = 1;
     37         } else {
     38             print "Nope! try again...\n";
     39             unshift @lines, $item;
     40             $trys++;
     41         }
     42     }
     43 }