Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: wrap_text_to_pixels truncate argument #205

Open
DJDevon3 opened this issue Apr 7, 2024 · 0 comments
Open

Feature Request: wrap_text_to_pixels truncate argument #205

DJDevon3 opened this issue Apr 7, 2024 · 0 comments

Comments

@DJDevon3
Copy link

DJDevon3 commented Apr 7, 2024

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant