Browse Source

Check for StringIO object before calling split_by_char_size

PASS1-78
coreylakey 3 years ago
parent
commit
a322815a03
  1. 7
      ports/stm32/boards/Passport/modules/display.py
  2. 31
      ports/stm32/boards/Passport/modules/utils.py
  3. 7
      ports/stm32/boards/Passport/modules/ux.py

7
ports/stm32/boards/Passport/modules/display.py

@ -195,8 +195,11 @@ class Display:
def text_input(self, x, y, msg, font=FontSmall, invert=0, cursor_pos=None, visible_spaces=False, fixed_spacing=None, cursor_shape='line'):
from ux import word_wrap
from utils import split_by_char_size
lines = split_by_char_size(msg, font)
if hasattr(msg, 'readline'):
lines = split_by_char_size(msg.getvalue(), font)
else:
lines = split_by_char_size(msg, font)
# Special case to draw cursor by itself when no text is entered yet
if len(msg) == 0:

31
ports/stm32/boards/Passport/modules/utils.py

@ -887,31 +887,12 @@ def split_by_char_size(msg, font):
from common import dis
lines = []
# First case is used with StringIO objects
if hasattr(msg, 'readline'):
msg.seek(0)
for ln in msg:
if ln[-1] == '\n':
ln = ln[:-1]
if dis.width(ln, font) > MAX_WIDTH:
lines.extend(word_wrap(ln, font))
else:
# ok if empty string, just a blank line
lines.append(ln)
# no longer needed & rude to our caller, but let's save the memory
msg.close()
del msg
gc.collect()
else:
for ln in msg.split('\n'):
if dis.width(ln, font) > MAX_WIDTH:
lines.extend(word_wrap(ln, font))
else:
# ok if empty string, just a blank line
lines.append(ln)
for ln in msg.split('\n'):
if dis.width(ln, font) > MAX_WIDTH:
lines.extend(word_wrap(ln, font))
else:
# ok if empty string, just a blank line
lines.append(ln)
return lines
# EOF

7
ports/stm32/boards/Passport/modules/ux.py

@ -642,8 +642,11 @@ async def ux_show_story(msg, title='Passport', sensitive=False, font=FontSmall,
if clear_keys:
keypad.clear_keys()
lines = split_by_char_size(msg, font)
if hasattr(msg, 'readline'):
lines = split_by_char_size(msg.getvalue(), font)
else:
lines = split_by_char_size(msg, font)
# trim blank lines at end
while not lines[-1]:
lines = lines[:-1]

Loading…
Cancel
Save