Skip to content

Commit ca3c43a

Browse files
committed
Cherry pick PR cslarsen#56 to handle line breaks
1 parent 6e48d8b commit ca3c43a

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

wpm/screen.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from wpm.histogram import histogram, plot
2525
import wpm.devfeature as devfeature
2626

27+
2728
class Screen(object):
2829
"""Renders the terminal screen."""
2930

@@ -101,21 +102,24 @@ def __init__(self, monochrome):
101102
def _word_wrap(text, width):
102103
"""Returns lengths of lines that can be printed without wrapping."""
103104
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
109112

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
113117

114-
lengths.append(end)
115-
text = text[end + 1:]
118+
lengths.append(end)
119+
text = text[end + 1:]
116120

117-
if text:
118-
lengths.append(len(text))
121+
if text:
122+
lengths.append(len(text))
119123

120124
return lengths
121125

@@ -199,6 +203,13 @@ def is_escape(key):
199203
return ord(key) == curses.ascii.ESC
200204
return False
201205

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+
202213
@staticmethod
203214
def is_backspace(key):
204215
"""Checks for backspace key."""
@@ -291,6 +302,7 @@ def chgat(self, x_pos, y_pos, length, color):
291302

292303
def set_cursor(self, x_pos, y_pos):
293304
"""Sets cursor position."""
305+
294306
if (y_pos < self.lines) and (x_pos < self.columns):
295307
self.window.move(y_pos, x_pos)
296308

@@ -347,7 +359,7 @@ def set_quote(self, quote):
347359

348360
# Remember (x, y) position for each quote offset.
349361
self.quote_coords = []
350-
for offset in range(len(self.quote)+1):
362+
for offset in range(len(self.quote) + 1):
351363
x_pos, y_pos = Screen._screen_coords(self.quote_lengths, offset)
352364
self.quote_coords.append((x_pos, y_pos))
353365
self.quote_coords = tuple(self.quote_coords)
@@ -494,7 +506,6 @@ def highlight_progress(self, position, incorrect):
494506
def show_keystroke(self, head, position, incorrect, typed, key):
495507
"""Updates the screen while typing."""
496508
self.update_header(head)
497-
498509
if key and (position + incorrect) <= len(self.quote):
499510
self.highlight_progress(position, incorrect)
500511
self.update_prompt("> " + typed)
@@ -521,4 +532,4 @@ def deinit(self):
521532
curses.nocbreak()
522533
self.screen.keypad(False)
523534
curses.echo()
524-
curses.endwin()
535+
c

0 commit comments

Comments
 (0)