adafruit / Adafruit_CircuitPython_Display_Text

Library to display text using displayio
MIT License
57 stars 38 forks source link

Feature Request: wrap_text_to_pixels truncate argument #205

Open DJDevon3 opened 6 months ago

DJDevon3 commented 6 months ago

I'm working on a project with wrap_text_to_pixels where I'd like a built in way to truncate a block of text. Word wrap is great but on a small display there needs to be a way to limit the returned paragraph length. Appending "..." to the end would be a nice touch.

There is a max_width argument which works very well. This would be more of a max_length for the total length of a paragraph.

One problem I've run into is "\n" newlines take up an entire line but does not count against the normal length of a line. I normally expect about 200 characters (just an example) per line but with carriage returns those lines return 0 characters against the len(). I'm not sure of a good way to count lines to account for this. Maybe a regex or replace for any found \n in the string?

Here's what I have so far using a snippet from stack overflow (labels and other stuff missing)

import os
import time

import adafruit_connection_manager
import board
import displayio
import fourwire
import terminalio
import wifi

from adafruit_bitmap_font import bitmap_font
import adafruit_requests
from adafruit_display_text import label, wrap_text_to_pixels
from circuitpython_st7796s import ST7796S

spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10
tft_rst = board.D17

# 3.5" ST7796S Display
DISPLAY_WIDTH = 480
DISPLAY_HEIGHT = 320
wordwrap = wrap_text_to_pixels

displayio.release_displays()
display_bus = fourwire.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=tft_rst)
display = ST7796S(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, rotation=180)

# Quick Colors for Labels
TEXT_BLACK = 0x000000
TEXT_BLUE = 0x0000FF
TEXT_CYAN = 0x00FFFF
TEXT_GRAY = 0x8B8B8B
TEXT_GREEN = 0x00FF00
TEXT_LIGHTBLUE = 0x90C7FF
TEXT_MAGENTA = 0xFF00FF
TEXT_ORANGE = 0xFFA500
TEXT_PURPLE = 0x800080
TEXT_RED = 0xFF0000
TEXT_WHITE = 0xFFFFFF
TEXT_YELLOW = 0xFFFF00

Arial12 = bitmap_font.load_font("/fonts/Arial-12.bdf")

title_value_label = label.Label(terminalio.FONT)
title_value_label.anchor_point = (0.0, 0.0)
title_value_label.anchored_position = (5, 60)
title_value_label.scale = (1)
title_value_label.color = TEXT_LIGHTBLUE

desc_key_label = label.Label(Arial12)
desc_key_label.anchor_point = (0.0, 0.0)
desc_key_label.anchored_position = (5, 130)
desc_key_label.scale = (1)
desc_key_label.color = TEXT_WHITE

desc_value_label = label.Label(terminalio.FONT)
desc_value_label.anchor_point = (0.0, 0.0)
desc_value_label.anchored_position = (5, 150)
desc_value_label.scale = (1)
desc_value_label.color = TEXT_LIGHTBLUE

intro2 = "Orbiting this \nat a distance of roughly ninety-two million miles \n \n(this is a problem for len) \nis an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea."

print(f"intro2 length: {len(intro2)}")
info = (intro2[:300] + '...') if len(intro2) > 300 else intro2
title_value_label.text = "\n".join(wordwrap(info, DISPLAY_WIDTH-2, terminalio.FONT))

print(f"intro2 length: {len(intro2)}")
info2 = (intro2[:500] + '...') if len(intro2) > 500 else intro2
desc_key_label.text = "Description:"
desc_value_label.text = "\n".join(wordwrap(info2, DISPLAY_WIDTH-2, terminalio.FONT))