Skip to content

Commit 256380c

Browse files
committed
fix words longer than max_chars
1 parent 006fee4 commit 256380c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

adafruit_display_text/__init__.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Display Text module helper functions
33
"""
4+
5+
46
# The MIT License (MIT)
57
#
68
# Copyright (c) 2020 Tim C for Adafruit Industries LLC
@@ -35,11 +37,25 @@ def wrap_text_to_lines(string, max_chars):
3537
of max_chars provided
3638
3739
"""
40+
41+
def chunks(lst, n):
42+
"""Yield successive n-sized chunks from lst."""
43+
for i in range(0, len(lst), n):
44+
yield lst[i : i + n]
45+
3846
string = string.replace("\n", "").replace("\r", "") # Strip confusing newlines
3947
words = string.split(" ")
4048
the_lines = []
4149
the_line = ""
4250
for w in words:
51+
if len(w) > max_chars:
52+
parts = []
53+
for part in chunks(w, max_chars - 1):
54+
parts.append("{}-".format(part))
55+
the_lines.extend(parts[:-1])
56+
the_line = parts[-1][:-1]
57+
continue
58+
4359
if len(the_line + " " + w) <= max_chars:
4460
the_line += " " + w
4561
else:
@@ -48,5 +64,6 @@ def wrap_text_to_lines(string, max_chars):
4864
if the_line: # Last line remaining
4965
the_lines.append(the_line)
5066
# Remove first space from first line:
51-
the_lines[0] = the_lines[0][1:]
67+
if the_lines[0][0] == " ":
68+
the_lines[0] = the_lines[0][1:]
5269
return the_lines

0 commit comments

Comments
 (0)