Skip to content

Allows line-break to be used in loaded Text #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions wpm/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from wpm.histogram import histogram, plot
import wpm.devfeature as devfeature


class Screen(object):
"""Renders the terminal screen."""

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down