src.trezor.utils: split long words in split_words

pull/25/head
Pavol Rusnak 7 years ago
parent 13ffe75a48
commit 865070d083
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -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)

Loading…
Cancel
Save