mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 15:38:11 +00:00
src.trezor.utils: split long words in split_words
This commit is contained in:
parent
13ffe75a48
commit
865070d083
@ -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…
Reference in New Issue
Block a user