24
24
from wpm .histogram import histogram , plot
25
25
import wpm .devfeature as devfeature
26
26
27
+
27
28
class Screen (object ):
28
29
"""Renders the terminal screen."""
29
30
@@ -101,21 +102,24 @@ def __init__(self, monochrome):
101
102
def _word_wrap (text , width ):
102
103
"""Returns lengths of lines that can be printed without wrapping."""
103
104
lengths = []
104
- while len (text ) > width :
105
- try :
106
- end = text [:width + 1 ].rindex (" " )
107
- except ValueError :
108
- break
105
+ texts = text .splitlines ()
106
+ for text in texts :
107
+ while len (text ) > width :
108
+ try :
109
+ end = text [:width + 1 ].rindex (" " )
110
+ except ValueError :
111
+ break
109
112
110
- # We can't divide the input nicely, so just display it as-is
111
- if end == - 1 :
112
- return [len (text )]
113
+ # We can't divide the input nicely, so just display it as-is
114
+ if end == - 1 :
115
+ lengths += [len (text )]
116
+ break
113
117
114
- lengths .append (end )
115
- text = text [end + 1 :]
118
+ lengths .append (end )
119
+ text = text [end + 1 :]
116
120
117
- if text :
118
- lengths .append (len (text ))
121
+ if text :
122
+ lengths .append (len (text ))
119
123
120
124
return lengths
121
125
@@ -199,6 +203,13 @@ def is_escape(key):
199
203
return ord (key ) == curses .ascii .ESC
200
204
return False
201
205
206
+ @staticmethod
207
+ def is_newline (key ):
208
+ """Checks for enter key."""
209
+ if len (key ) == 1 :
210
+ return ord (key ) == curses .ascii .NL
211
+ return False
212
+
202
213
@staticmethod
203
214
def is_backspace (key ):
204
215
"""Checks for backspace key."""
@@ -291,6 +302,7 @@ def chgat(self, x_pos, y_pos, length, color):
291
302
292
303
def set_cursor (self , x_pos , y_pos ):
293
304
"""Sets cursor position."""
305
+
294
306
if (y_pos < self .lines ) and (x_pos < self .columns ):
295
307
self .window .move (y_pos , x_pos )
296
308
@@ -347,7 +359,7 @@ def set_quote(self, quote):
347
359
348
360
# Remember (x, y) position for each quote offset.
349
361
self .quote_coords = []
350
- for offset in range (len (self .quote )+ 1 ):
362
+ for offset in range (len (self .quote ) + 1 ):
351
363
x_pos , y_pos = Screen ._screen_coords (self .quote_lengths , offset )
352
364
self .quote_coords .append ((x_pos , y_pos ))
353
365
self .quote_coords = tuple (self .quote_coords )
@@ -494,7 +506,6 @@ def highlight_progress(self, position, incorrect):
494
506
def show_keystroke (self , head , position , incorrect , typed , key ):
495
507
"""Updates the screen while typing."""
496
508
self .update_header (head )
497
-
498
509
if key and (position + incorrect ) <= len (self .quote ):
499
510
self .highlight_progress (position , incorrect )
500
511
self .update_prompt ("> " + typed )
@@ -521,4 +532,4 @@ def deinit(self):
521
532
curses .nocbreak ()
522
533
self .screen .keypad (False )
523
534
curses .echo ()
524
- curses . endwin ()
535
+ c
0 commit comments