forked from pasky/speedread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedread
executable file
·289 lines (246 loc) · 6.62 KB
/
speedread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env perl
#
# speedread: A simple terminal-based open source spritz-alike
#
# Show input text as a per-word RSVP (rapid serial visual presentation)
# aligned on optimal reading points. This kind of input mode allows
# reading text at a much more rapid pace than usual as the eye can
# stay fixed on a single place.
#
# (c) Petr Baudis <[email protected]> 2014
# MIT licence
#
# Usage: cat file.txt | speedread [-w WORDSPERMINUTE] [-r RESUMEPOINT] [-m]
#
# The default of 250 words per minut is very timid, designed so that
# you get used to this. Be sure to try cranking this up, 500wpm
# should still be fairly easy to follow even for beginners.
#
# speedread can join short words together if you specify the -m switch.
# It did not work well for pasky so far, though.
#
# speedread is slightly interactive, with these controls accepted:
#
# [ - slow down by 10%
# ] - speed up by 10%
# space - pause (and show the last two lines)
use warnings;
use strict;
use autodie;
use v5.14;
my $wpm = 250;
my $resume = 0;
my $multiword = 0;
use utf8;
binmode(STDIN, ":encoding(UTF-8)");
binmode(STDOUT, ":encoding(UTF-8)");
use Term::ANSIColor;
use POSIX qw(ceil floor);
use Time::HiRes qw(time sleep gettimeofday tv_interval);
use Getopt::Long;
GetOptions("wpm|w=i" => \$wpm,
"resume|r=i" => \$resume,
"multiword|m" => \$multiword);
my $wordtime = 0.9; # relative to wpm
my $lentime = 0.04; # * sqrt(length $word), relative to wpm
my $commatime = 2; # relative to wpm
my $fstoptime = 3; # relative to wpm
my $multitime = 1.2; # relative to wpm
my $firsttime = 0.2; # [s]
my $ORPloc = 0.35;
my $ORPmax = 0.2;
my $ORPvisualpos = 20;
my $cursorpos = 64;
my $paused = 0;
my $current_word;
my $current_orp;
my $next_word_time = 0;
my $next_input_time = 0;
my $skipped = 0;
my @lastlines;
my $tty = rawinput->new();
$| = 1;
my $wordcounter = 0;
my $lettercounter = 0;
my $t0 = [gettimeofday];
sub print_stats {
my $elapsed = tv_interval($t0, [gettimeofday]);
my $truewpm = $wordcounter / $elapsed * 60;
printf("\n %.2fs, %d words, %d letters, %s%.2f%s true wpm\n",
$elapsed, $wordcounter, $lettercounter,
color('bold green'), $truewpm, color('reset'));
}
$SIG{INT} = sub {
print_stats;
my $resume_word = $wordcounter + $resume;
say " To resume from this point run with argument -r $resume_word";
exit;
};
main();
# ORP: Optical Recognition Point (the red-colored alignment pilot),
# the way Spritz probably does it.
sub find_ORP {
my ($word, $ORPloc) = @_;
return 4 if (length($word) > 13);
return (0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)[length($word)];
}
sub show_guide {
# Top visual guide
say(" "x$ORPvisualpos . color('red') . "v" . color('reset') . "\033[K");
}
sub show_word {
my ($word, $i) = @_;
my $pivotch = substr($word, $i, 1);
$pivotch = "·" if $pivotch eq ' ';
print("\r\033[K"
. " " x ($ORPvisualpos - $i)
. color("bold")
. substr($word, 0, $i)
. color("red")
. $pivotch
. color("reset")
. color("bold")
. substr($word, $i+1)
. color("reset")
. " " x ($cursorpos - (($ORPvisualpos - $i) + length($word)))
. "$wpm wpm"
. ($paused ? " ".color("yellow")."PAUSED".color("reset") : ""));
}
sub word_time {
my ($word) = @_;
my $time = $wordtime;
if ($word =~ /[.?!]\W*$/) {
$time = $fstoptime;
} elsif ($word =~ /[:;,]\W*$/) {
$time = $commatime;
} elsif ($word =~ / /) {
$time = $multitime;
}
$time += sqrt(length($word)) * $lentime;
$time *= 60 / $wpm;
# Give user some time to focus on the first word, even with high wpm.
$time = $firsttime if ($wordcounter == 0 and $time < $firsttime);
return $time;
}
sub print_context {
my ($wn) = @_;
# One line up and to its beginning
print "\r\033[K\033[A\033[K";
# First line of context
say $lastlines[1] if $lastlines[1];
# In second line of context, highlight our word
my $line0 = $lastlines[0];
my $c0 = color('yellow');
my $c1 = color('reset');
$line0 =~ s/^((?:.*?(?:-|\s)+){$wn})(.*?)(-|\s)/$1$c0$2$c1$3/;
say $line0;
}
sub process_keys {
my ($word, $i, $wn) = @_;
while ($tty->key_pressed()) {
my $ch = $tty->getch();
if ($ch eq '[') {
if ($wpm > 1) {
$wpm = int($wpm * 0.9);
} else {
$paused = 1;
}
} elsif ($ch eq ']') {
if ($wpm >= 10) {
$wpm = int($wpm * 1.1);
} else {
$wpm += 1;
}
} elsif ($ch eq ' ') {
$paused = not $paused;
if ($paused) {
# Print context.
print_context($wn);
show_guide();
show_word($word, $i);
}
else {
$next_word_time = time();
}
}
}
}
sub main {
show_guide();
$next_word_time = time();
$next_input_time = time();
while (<>) {
chomp;
unshift @lastlines, $_;
pop @lastlines if @lastlines > 2;
my (@words) = grep { /./ } split /(?:-|\s)+/;
if ($multiword) {
# Join adjacent short words
for (my $i = 0; $i < $#words - 1; $i++) {
if (length($words[$i]) <= 3 and length($words[$i+1]) <= 3) {
$words[$i] .= ' ' . $words[$i+1];
splice(@words, $i+1, 1);
}
}
}
my $wn = 0;
while (scalar(@words) > 0) {
if ($skipped < $resume) {
$skipped++;
shift @words;
next;
}
my $current_time = time();
if ($next_word_time <= $current_time and !$paused) {
$current_word = shift @words;
$current_orp = find_ORP($current_word, $ORPloc);
$next_word_time += word_time($current_word);
$wordcounter++;
$lettercounter += length($current_word);
$wn++;
}
if ($next_input_time <= $current_time) {
process_keys($current_word, $current_orp, $wn);
$next_input_time += 0.05; # checking for input 20 times / second seems to give a reasonably responsive UI
}
# redrawing the word on each "frame" gives a more responsive UI
# (we don't have to wait for the word to change to display changed stats like wpm)
show_word($current_word, $current_orp);
my $sleep_time = ($next_word_time < $next_input_time and !$paused) ? $next_word_time-$current_time : $next_input_time-$current_time;
sleep($sleep_time) if ($sleep_time > 0);
}
}
print_stats();
sleep(1);
}
package rawinput;
# An ad-hoc interface to interactive terminal input. Term::Screen *should*
# have been a natural choice here, unfortunately it is fixated at reading
# from stdin instead of /dev/tty. Tough.
sub new {
my $class = shift;
my $self;
open $self, '/dev/tty';
bless $self, $class;
stty('min 1', '-icanon', '-echo');
return $self;
}
sub DESTROY {
stty('cooked', 'echo');
}
sub stty {
my $self = shift;
eval { system('stty', $^O eq 'darwin' ? '-f' : '-F', '/dev/tty', @_); };
}
sub key_pressed {
my $self = shift;
my $readfields = '';
vec($readfields, fileno($self), 1) = 1;
my $ready = 0;
eval { $ready = select($readfields, undef, undef, 0); };
return $ready;
}
sub getch {
my $self = shift;
getc($self);
}