mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-19 22:18:13 +00:00
src.trezor.utils: split long words in split_words
This commit is contained in:
parent
13ffe75a48
commit
865070d083
@ -31,12 +31,20 @@ def chunks(items, size):
|
|||||||
def split_words(sentence, width, metric=len):
|
def split_words(sentence, width, metric=len):
|
||||||
line = []
|
line = []
|
||||||
for w in sentence.split(' '):
|
for w in sentence.split(' '):
|
||||||
|
# empty word -> skip
|
||||||
if not w:
|
if not w:
|
||||||
continue
|
continue
|
||||||
|
# new word will not fit -> break the line
|
||||||
if metric(' '.join(line + [w])) >= width:
|
if metric(' '.join(line + [w])) >= width:
|
||||||
yield ' '.join(line)
|
yield ' '.join(line)
|
||||||
line = [w]
|
line = []
|
||||||
else:
|
# 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)
|
line.append(w)
|
||||||
yield ' '.join(line)
|
yield ' '.join(line)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user