From 3913811d1344dafb4255ca063b81a60d99bb6a78 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Mon, 7 Feb 2022 09:14:51 -0600 Subject: [PATCH 1/3] Add a space for special case of wrapping of long words --- adafruit_display_text/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/adafruit_display_text/__init__.py b/adafruit_display_text/__init__.py index 9ab6725..97f37de 100644 --- a/adafruit_display_text/__init__.py +++ b/adafruit_display_text/__init__.py @@ -62,6 +62,7 @@ def measure(text): swidth = measure(" ") firstword = True for line_in_input in string.split("\n"): + newline = True for index, word in enumerate(line_in_input.split(" ")): wwidth = measure(word) word_parts = [] @@ -69,16 +70,28 @@ def measure(text): if wwidth > max_width: for char in word: + if newline: + extraspace = 0 + else: + extraspace = swidth if ( measure("".join(partial)) + measure(cur_part) + measure(char) + measure("-") + + extraspace > max_width ): - word_parts.append("".join(partial) + cur_part + "-") + if cur_part: + if newline: + word_parts.append("".join(partial) + cur_part + "-") + else: + word_parts.append("".join(partial) + " " + cur_part + "-") + else: + word_parts.append("".join(partial)) cur_part = char partial = [indent1] + newline = True else: cur_part += char if cur_part: @@ -103,6 +116,8 @@ def measure(text): lines.append("".join(partial)) partial = [indent1, word] width = measure(indent1) + wwidth + if newline: + newline = False lines.append("".join(partial)) partial = [indent1] From 3d6043192caf87030f5dd2177a8b5b48e6bfc2bc Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Mon, 7 Feb 2022 09:23:47 -0600 Subject: [PATCH 2/3] pylint fixes, disabled too-many-nested-blocks --- adafruit_display_text/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/__init__.py b/adafruit_display_text/__init__.py index 97f37de..e47b022 100644 --- a/adafruit_display_text/__init__.py +++ b/adafruit_display_text/__init__.py @@ -24,7 +24,7 @@ def wrap_text_to_pixels( indent0: str = "", indent1: str = "", ) -> List[str]: - # pylint: disable=too-many-branches, too-many-locals + # pylint: disable=too-many-branches, too-many-locals, too-many-nested-blocks, too-many-statements """wrap_text_to_pixels function A helper that will return a list of lines with word-break wrapping. @@ -86,7 +86,9 @@ def measure(text): if newline: word_parts.append("".join(partial) + cur_part + "-") else: - word_parts.append("".join(partial) + " " + cur_part + "-") + word_parts.append( + "".join(partial) + " " + cur_part + "-" + ) else: word_parts.append("".join(partial)) cur_part = char From 8f8a8a741226cd526861b5c5ec68ee15bbabd36d Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Mon, 7 Feb 2022 17:27:08 -0600 Subject: [PATCH 3/3] remove one if level --- adafruit_display_text/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_display_text/__init__.py b/adafruit_display_text/__init__.py index e47b022..86079bf 100644 --- a/adafruit_display_text/__init__.py +++ b/adafruit_display_text/__init__.py @@ -72,8 +72,10 @@ def measure(text): for char in word: if newline: extraspace = 0 + leadchar = "" else: extraspace = swidth + leadchar = " " if ( measure("".join(partial)) + measure(cur_part) @@ -83,12 +85,10 @@ def measure(text): > max_width ): if cur_part: - if newline: - word_parts.append("".join(partial) + cur_part + "-") - else: - word_parts.append( - "".join(partial) + " " + cur_part + "-" - ) + word_parts.append( + "".join(partial) + leadchar + cur_part + "-" + ) + else: word_parts.append("".join(partial)) cur_part = char