From c89b76267509653a39a55927b428c20472c80822 Mon Sep 17 00:00:00 2001 From: Rahul Date: Fri, 28 Feb 2020 13:10:40 +0530 Subject: [PATCH] Allows line-break to be used in loaded Text (Issue #49) --- wpm/screen.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/wpm/screen.py b/wpm/screen.py index 3a586ea..3a8434e 100644 --- a/wpm/screen.py +++ b/wpm/screen.py @@ -24,6 +24,7 @@ from wpm.histogram import histogram, plot import wpm.devfeature as devfeature + class Screen(object): """Renders the terminal screen.""" @@ -101,21 +102,24 @@ def __init__(self, monochrome): def _word_wrap(text, width): """Returns lengths of lines that can be printed without wrapping.""" lengths = [] - while len(text) > width: - try: - end = text[:width + 1].rindex(" ") - except ValueError: - break + texts = text.splitlines() + for text in texts: + while len(text) > width: + try: + end = text[:width + 1].rindex(" ") + except ValueError: + break - # We can't divide the input nicely, so just display it as-is - if end == -1: - return [len(text)] + # We can't divide the input nicely, so just display it as-is + if end == -1: + lengths += [len(text)] + break - lengths.append(end) - text = text[end + 1:] + lengths.append(end) + text = text[end + 1:] - if text: - lengths.append(len(text)) + if text: + lengths.append(len(text)) return lengths @@ -199,6 +203,13 @@ def is_escape(key): return ord(key) == curses.ascii.ESC return False + @staticmethod + def is_newline(key): + """Checks for enter key.""" + if len(key) == 1: + return ord(key) == curses.ascii.NL + return False + @staticmethod def is_backspace(key): """Checks for backspace key.""" @@ -291,6 +302,7 @@ def chgat(self, x_pos, y_pos, length, color): def set_cursor(self, x_pos, y_pos): """Sets cursor position.""" + if (y_pos < self.lines) and (x_pos < self.columns): self.window.move(y_pos, x_pos) @@ -347,7 +359,7 @@ def set_quote(self, quote): # Remember (x, y) position for each quote offset. self.quote_coords = [] - for offset in range(len(self.quote)+1): + for offset in range(len(self.quote) + 1): x_pos, y_pos = Screen._screen_coords(self.quote_lengths, offset) self.quote_coords.append((x_pos, y_pos)) self.quote_coords = tuple(self.quote_coords) @@ -494,7 +506,6 @@ def highlight_progress(self, position, incorrect): def show_keystroke(self, head, position, incorrect, typed, key): """Updates the screen while typing.""" self.update_header(head) - if key and (position + incorrect) <= len(self.quote): self.highlight_progress(position, incorrect) self.update_prompt("> " + typed)