File tree 1 file changed +18
-1
lines changed
1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
Display Text module helper functions
3
3
"""
4
+
5
+
4
6
# The MIT License (MIT)
5
7
#
6
8
# Copyright (c) 2020 Tim C for Adafruit Industries LLC
@@ -35,11 +37,25 @@ def wrap_text_to_lines(string, max_chars):
35
37
of max_chars provided
36
38
37
39
"""
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
+
38
46
string = string .replace ("\n " , "" ).replace ("\r " , "" ) # Strip confusing newlines
39
47
words = string .split (" " )
40
48
the_lines = []
41
49
the_line = ""
42
50
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
+
43
59
if len (the_line + " " + w ) <= max_chars :
44
60
the_line += " " + w
45
61
else :
@@ -48,5 +64,6 @@ def wrap_text_to_lines(string, max_chars):
48
64
if the_line : # Last line remaining
49
65
the_lines .append (the_line )
50
66
# 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 :]
52
69
return the_lines
You can’t perform that action at this time.
0 commit comments