1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-19 14:08:11 +00:00

src.trezor.utils: split long words in split_words

This commit is contained in:
Pavol Rusnak 2018-02-27 14:52:21 +01:00
parent 13ffe75a48
commit 865070d083
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -31,13 +31,21 @@ def chunks(items, size):
def split_words(sentence, width, metric=len):
line = []
for w in sentence.split(' '):
# empty word -> skip
if not w:
continue
# new word will not fit -> break the line
if metric(' '.join(line + [w])) >= width:
yield ' '.join(line)
line = [w]
else:
line.append(w)
line = []
# word is too wide -> split the word
while metric(w) >= width:
for i in range(1, len(w) + 1):
if metric(w[:-i]) < width:
yield w[:-i] + '-'
w = w[-i:]
break
line.append(w)
yield ' '.join(line)