From f3c565c3590b91bcec24380c4d4511a71cd1af4b Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 16 Nov 2020 21:47:53 -0600 Subject: [PATCH 1/3] add text wrapping helper function and example that uses it. --- adafruit_display_text/__init__.py | 52 +++++++++++++++++++++++++++++++ examples/display_text_wraptest.py | 24 ++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 examples/display_text_wraptest.py diff --git a/adafruit_display_text/__init__.py b/adafruit_display_text/__init__.py index e69de29..3fff175 100644 --- a/adafruit_display_text/__init__.py +++ b/adafruit_display_text/__init__.py @@ -0,0 +1,52 @@ +""" +Display Text module helper functions +""" +# The MIT License (MIT) +# +# Copyright (c) 2020 Tim C for Adafruit Industries LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + + +def wrap_text_to_lines(string, max_chars): + """wrap_text_to_lines function + A helper that will return a list of lines with word-break wrapping + + :param str string: The text to be wrapped + :param int max_chars: The maximum number of characters on a line before wrapping + + :return list the_lines: A list of lines where each line is separated based on the amount + of max_chars provided + + """ + string = string.replace("\n", "").replace("\r", "") # Strip confusing newlines + words = string.split(" ") + the_lines = [] + the_line = "" + for w in words: + if len(the_line + " " + w) <= max_chars: + the_line += " " + w + else: + the_lines.append(the_line) + the_line = "" + w + if the_line: # Last line remaining + the_lines.append(the_line) + # Remove first space from first line: + the_lines[0] = the_lines[0][1:] + return the_lines diff --git a/examples/display_text_wraptest.py b/examples/display_text_wraptest.py new file mode 100644 index 0000000..071fe9c --- /dev/null +++ b/examples/display_text_wraptest.py @@ -0,0 +1,24 @@ +""" +This example illustrates how to use the wrap_text_to_lines +helper function. +""" +import board +import terminalio +from adafruit_display_text import label, wrap_text_to_lines + +# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.) +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus +display = board.DISPLAY + +text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \ + "sed do eiusmod tempor incididunt ut labore et dolore magna " \ + "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \ + "ullamco laboris nisi ut aliquip ex ea commodo consequat." +text = "\n".join(wrap_text_to_lines(text, 28)) +text_area = label.Label(terminalio.FONT, text=text) +text_area.x = 10 +text_area.y = 10 +display.show(text_area) +while True: + pass From 006fee4a6cdb1e00aac9fe04fe87785504649858 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 16 Nov 2020 22:03:05 -0600 Subject: [PATCH 2/3] black format --- examples/display_text_wraptest.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/display_text_wraptest.py b/examples/display_text_wraptest.py index 071fe9c..b60c100 100644 --- a/examples/display_text_wraptest.py +++ b/examples/display_text_wraptest.py @@ -11,10 +11,12 @@ # https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus display = board.DISPLAY -text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \ - "sed do eiusmod tempor incididunt ut labore et dolore magna " \ - "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \ - "ullamco laboris nisi ut aliquip ex ea commodo consequat." +text = ( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna " + "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " + "ullamco laboris nisi ut aliquip ex ea commodo consequat." +) text = "\n".join(wrap_text_to_lines(text, 28)) text_area = label.Label(terminalio.FONT, text=text) text_area.x = 10 From 256380c71ea7d870cde82f6e3aafac8d0fa7cbf1 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 21 Nov 2020 19:10:48 -0600 Subject: [PATCH 3/3] fix words longer than max_chars --- adafruit_display_text/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/adafruit_display_text/__init__.py b/adafruit_display_text/__init__.py index 3fff175..26bb3ca 100644 --- a/adafruit_display_text/__init__.py +++ b/adafruit_display_text/__init__.py @@ -1,6 +1,8 @@ """ Display Text module helper functions """ + + # The MIT License (MIT) # # Copyright (c) 2020 Tim C for Adafruit Industries LLC @@ -35,11 +37,25 @@ def wrap_text_to_lines(string, max_chars): of max_chars provided """ + + def chunks(lst, n): + """Yield successive n-sized chunks from lst.""" + for i in range(0, len(lst), n): + yield lst[i : i + n] + string = string.replace("\n", "").replace("\r", "") # Strip confusing newlines words = string.split(" ") the_lines = [] the_line = "" for w in words: + if len(w) > max_chars: + parts = [] + for part in chunks(w, max_chars - 1): + parts.append("{}-".format(part)) + the_lines.extend(parts[:-1]) + the_line = parts[-1][:-1] + continue + if len(the_line + " " + w) <= max_chars: the_line += " " + w else: @@ -48,5 +64,6 @@ def wrap_text_to_lines(string, max_chars): if the_line: # Last line remaining the_lines.append(the_line) # Remove first space from first line: - the_lines[0] = the_lines[0][1:] + if the_lines[0][0] == " ": + the_lines[0] = the_lines[0][1:] return the_lines