-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrsvp.py
More file actions
executable file
·61 lines (52 loc) · 1.58 KB
/
rsvp.py
File metadata and controls
executable file
·61 lines (52 loc) · 1.58 KB
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
"""
Rapid Serial Visual Presentation
TODO:
* Split input into sentences.
A chunk shouldn't span a sentence boundary.
There should be a slight delay after a sentence.
* Try showing chunks above and below the current one.
* Speed control.
* A TeX-like optimization algorithm to pick chunk boundaries.
See https://github.com/pasky/speedread/blob/master/speedread
"""
from __future__ import division
import sys
import textwrap
import time
prefix = '\x1b['
home = prefix + 'H'
clear_screen = prefix + '2J' + home
def main(filename, width, cpm):
with open(filename) as f:
text = f.read()
paragraphs = text.split('\n\n')
chunks = enchunk(paragraphs, width)
print clear_screen
rsvp(chunks, 60/cpm)
def rsvp(chunks, interval):
for chunk in chunks:
print '\r', chunk.center(76),
sys.stdout.flush()
time.sleep(interval)
print
def enchunk(paragraphs, width):
wrapper = textwrap.TextWrapper(width=width, break_long_words=False)
for para in paragraphs:
para = ' '.join(para.split())
for chunk in wrapper.wrap(para):
yield chunk
yield ''
def enchunk_obsolete(paragraphs, width):
for para in paragraphs:
chunk = ''
for word in para.split():
more = chunk + ' ' + word if chunk else word
if chunk and width < len(more):
yield chunk
chunk = word
else:
chunk = more
if chunk: yield chunk
yield ''
if __name__ == '__main__':
main(sys.argv[1], int(sys.argv[2]), float(sys.argv[3]))