You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
importosimporttimeimportadafruit_connection_managerimportboardimportdisplayioimportfourwireimportterminalioimportwififromadafruit_bitmap_fontimportbitmap_fontimportadafruit_requestsfromadafruit_display_textimportlabel, wrap_text_to_pixelsfromcircuitpython_st7796simportST7796Sspi=board.SPI()
tft_cs=board.D9tft_dc=board.D10tft_rst=board.D17# 3.5" ST7796S DisplayDISPLAY_WIDTH=480DISPLAY_HEIGHT=320wordwrap=wrap_text_to_pixelsdisplayio.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 LabelsTEXT_BLACK=0x000000TEXT_BLUE=0x0000FFTEXT_CYAN=0x00FFFFTEXT_GRAY=0x8B8B8BTEXT_GREEN=0x00FF00TEXT_LIGHTBLUE=0x90C7FFTEXT_MAGENTA=0xFF00FFTEXT_ORANGE=0xFFA500TEXT_PURPLE=0x800080TEXT_RED=0xFF0000TEXT_WHITE=0xFFFFFFTEXT_YELLOW=0xFFFF00Arial12=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_LIGHTBLUEdesc_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_WHITEdesc_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_LIGHTBLUEintro2="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] +'...') iflen(intro2) >300elseintro2title_value_label.text="\n".join(wordwrap(info, DISPLAY_WIDTH-2, terminalio.FONT))
print(f"intro2 length: {len(intro2)}")
info2= (intro2[:500] +'...') iflen(intro2) >500elseintro2desc_key_label.text="Description:"desc_value_label.text="\n".join(wordwrap(info2, DISPLAY_WIDTH-2, terminalio.FONT))
The text was updated successfully, but these errors were encountered:
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)
The text was updated successfully, but these errors were encountered: