Skip to content

Commit d2b921e

Browse files
committed
Fix multiline keyboard (#66)
1 parent 352ff09 commit d2b921e

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

modules/keyboard.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,25 @@ def set_text(self, text, redraw=False):
234234
elif redraw:
235235
self.draw_textarea()
236236

237-
def draw_cursor(self):
238-
lines = self.flow_lines(self.text)
237+
def get_cursor_xy(self):
238+
hard_wrapped_lines = self.text.split("\n")
239239
x = 0
240240
y = 0
241241
chars_walked = 0
242-
while chars_walked < self.cursor_pos:
243-
line_len = len(lines[y])
244-
if chars_walked + line_len >= self.cursor_pos:
245-
x = self.cursor_pos - chars_walked
246-
break
247-
chars_walked += line_len
248-
y = y + 1
242+
for text in hard_wrapped_lines:
243+
for line in self.flow_lines(text):
244+
line_len = len(line)
245+
if chars_walked + line_len >= self.cursor_pos:
246+
x = self.cursor_pos - chars_walked
247+
return (x, y)
248+
chars_walked += line_len
249+
y = y + 1
250+
chars_walked += 1 # For the newline char
251+
252+
return (x, y)
249253

254+
def draw_cursor(self):
255+
(x, y) = self.get_cursor_xy()
250256
self.display.fill_rect(x * self.font.WIDTH + 1, self.get_line_pos(y) - 2,
251257
2, self.font.HEIGHT + 4, self.CURSOR_COLOUR)
252258

modules/textwindow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ def flow_lines(self, text, font=None):
167167
max_len = self.width_chars(font)
168168
for line in lines:
169169
line_len = len(line)
170+
if line_len == 0:
171+
result.append(line)
172+
continue
170173
i = 0
171174
while i < line_len:
172175
n = min(line_len - i, max_len)

0 commit comments

Comments
 (0)