File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 11"""
22Display 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
You can’t perform that action at this time.
0 commit comments