Skip to content

Commit e6040cf

Browse files
committed
format and lint
1 parent 203eac5 commit e6040cf

File tree

2 files changed

+88
-44
lines changed

2 files changed

+88
-44
lines changed

adafruit_display_text/text_box.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,9 @@
2929
import displayio
3030
from micropython import const
3131

32-
from adafruit_display_text import wrap_text_to_pixels, LabelBase
32+
from adafruit_display_text import wrap_text_to_pixels
3333
from adafruit_display_text import bitmap_label
3434

35-
36-
37-
try:
38-
import bitmaptools
39-
except ImportError:
40-
# We have a slower fallback for bitmaptools
41-
pass
42-
4335
try:
4436
from typing import Optional, Tuple
4537
from fontio import FontProtocol
@@ -49,21 +41,31 @@
4941

5042
# pylint: disable=too-many-instance-attributes
5143
class TextBox(bitmap_label.Label):
44+
"""
45+
TextBox has a constrained width and optionally height.
46+
You set the desired size when it's initialized it
47+
will automatically wrap text to fit it within the allotted
48+
size.
49+
50+
Left, Right, and Center alignment of the text within the
51+
box are supported.
52+
53+
:param font: The font to use for the TextBox.
54+
:param width: The width of the TextBox in pixels.
55+
:param height: The height of the TextBox in pixels.
56+
:param align: How to align the text within the box,
57+
valid values are `ALIGN_LEFT`, `ALIGN_CENTER`, `ALIGN_RIGHT`.
58+
"""
59+
5260
ALIGN_LEFT = const(0)
5361
ALIGN_CENTER = const(1)
5462
ALIGN_RIGHT = const(2)
5563

5664
DYNAMIC_HEIGHT = const(-1)
5765

58-
def __init__(self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT, **kwargs) -> None:
59-
"""
60-
61-
:param font:
62-
:param width:
63-
:param height:
64-
:param align:
65-
:param kwargs:
66-
"""
66+
def __init__(
67+
self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT, **kwargs
68+
) -> None:
6769
self._bitmap = None
6870
self._tilegrid = None
6971
self._prev_label_direction = None
@@ -75,12 +77,20 @@ def __init__(self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT
7577
else:
7678
self.dynamic_height = True
7779

78-
self.align = align
80+
if align not in (TextBox.ALIGN_LEFT, TextBox.ALIGN_CENTER, TextBox.ALIGN_RIGHT):
81+
raise ValueError(
82+
"Align must be one of: ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT"
83+
)
84+
self._align = align
7985

8086
self._padding_left = kwargs.get("padding_left", 0)
8187
self._padding_right = kwargs.get("padding_right", 0)
8288

83-
self.lines = wrap_text_to_pixels(kwargs.get("text", ""), self._width - self._padding_left - self._padding_right, font)
89+
self.lines = wrap_text_to_pixels(
90+
kwargs.get("text", ""),
91+
self._width - self._padding_left - self._padding_right,
92+
font,
93+
)
8494

8595
super(bitmap_label.Label, self).__init__(font, **kwargs)
8696

@@ -127,22 +137,25 @@ def _place_text(
127137
if self.align == self.ALIGN_RIGHT:
128138
unused_space = self._width - cur_line_width
129139
x_start = original_xposition + unused_space - self._padding_right
130-
xposition = x_start
140+
141+
xposition = x_start # pylint: disable=used-before-assignment
131142

132143
y_start = yposition
133-
#print(f"start loc {x_start}, {y_start}")
144+
# print(f"start loc {x_start}, {y_start}")
134145

135146
left = None
136147
right = x_start
137148
top = bottom = y_start
138149
line_spacing = self._line_spacing
139150

140-
#print(f"cur_line width: {cur_line_width}")
151+
# print(f"cur_line width: {cur_line_width}")
141152
for char in text:
142153
if char == "\n": # newline
143154
cur_line_index += 1
144-
cur_line_width = self._text_bounding_box(self.lines[cur_line_index], self.font)[0]
145-
#print(f"cur_line width: {cur_line_width}")
155+
cur_line_width = self._text_bounding_box(
156+
self.lines[cur_line_index], self.font
157+
)[0]
158+
# print(f"cur_line width: {cur_line_width}")
146159
if self.align == self.ALIGN_LEFT:
147160
x_start = original_xposition # starting x position (left margin)
148161
if self.align == self.ALIGN_CENTER:
@@ -225,7 +238,7 @@ def _place_text(
225238

226239
# bounding_box
227240
return left, top, right - left, bottom - top
228-
241+
229242
def _reset_text(
230243
self,
231244
font: Optional[FontProtocol] = None,
@@ -314,7 +327,9 @@ def _reset_text(
314327
or self._bitmap.width != self._width
315328
or self._bitmap.height != self._height
316329
):
317-
new_bitmap = displayio.Bitmap(self._width, self._height, len(self._palette))
330+
new_bitmap = displayio.Bitmap(
331+
self._width, self._height, len(self._palette)
332+
)
318333
self._bitmap = new_bitmap
319334
else:
320335
self._bitmap.fill(0)
@@ -397,10 +412,24 @@ def height(self, height: int) -> None:
397412

398413
@bitmap_label.Label.text.setter
399414
def text(self, text: str) -> None:
400-
self.lines = wrap_text_to_pixels(text, self._width - self._padding_left - self._padding_right,
401-
self.font)
415+
self.lines = wrap_text_to_pixels(
416+
text, self._width - self._padding_left - self._padding_right, self.font
417+
)
402418
self._text = self._replace_tabs(text)
403419
self._original_text = self._text
404420
self._text = "\n".join(self.lines)
405421

406422
self._set_text(self._text, self.scale)
423+
424+
@property
425+
def align(self):
426+
"""Alignment of the text within the TextBox"""
427+
return self._align
428+
429+
@align.setter
430+
def align(self, align: int) -> None:
431+
if align not in (TextBox.ALIGN_LEFT, TextBox.ALIGN_CENTER, TextBox.ALIGN_RIGHT):
432+
raise ValueError(
433+
"Align must be one of: ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT"
434+
)
435+
self._align = align

examples/display_text_text_box_simpletest.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,50 @@
99
main_group = displayio.Group()
1010

1111
left_text = ("Left left left left " * 2).rstrip()
12-
left_text_area = TextBox(terminalio.FONT, 190, TextBox.DYNAMIC_HEIGHT,
13-
align=TextBox.ALIGN_LEFT,
14-
text=left_text,
15-
background_color=0xff00ff,
16-
color=0x000000, scale=1)
12+
left_text_area = TextBox(
13+
terminalio.FONT,
14+
190,
15+
TextBox.DYNAMIC_HEIGHT,
16+
align=TextBox.ALIGN_LEFT,
17+
text=left_text,
18+
background_color=0xFF00FF,
19+
color=0x000000,
20+
scale=1,
21+
)
1722

1823
left_text_area.x = 10
1924
left_text_area.y = 10
2025
main_group.append(left_text_area)
2126

2227

2328
center_text = ("center center center " * 2).rstrip()
24-
center_text_area = TextBox(terminalio.FONT, 190, TextBox.DYNAMIC_HEIGHT,
25-
align=TextBox.ALIGN_CENTER,
26-
text=center_text,
27-
background_color=0x00ff00,
28-
color=0x000000, scale=1)
29+
center_text_area = TextBox(
30+
terminalio.FONT,
31+
190,
32+
TextBox.DYNAMIC_HEIGHT,
33+
align=TextBox.ALIGN_CENTER,
34+
text=center_text,
35+
background_color=0x00FF00,
36+
color=0x000000,
37+
scale=1,
38+
)
2939

3040
center_text_area.x = 10
3141
center_text_area.y = 10 + left_text_area.height + 10
3242
main_group.append(center_text_area)
3343

3444

3545
right_text = ("right right right right " * 2).rstrip()
36-
right_text_area = TextBox(terminalio.FONT, 190, TextBox.DYNAMIC_HEIGHT,
37-
align=TextBox.ALIGN_RIGHT,
38-
text=right_text,
39-
background_color=0xffff00,
40-
color=0x000000, scale=1)
46+
right_text_area = TextBox(
47+
terminalio.FONT,
48+
190,
49+
TextBox.DYNAMIC_HEIGHT,
50+
align=TextBox.ALIGN_RIGHT,
51+
text=right_text,
52+
background_color=0xFFFF00,
53+
color=0x000000,
54+
scale=1,
55+
)
4156

4257
right_text_area.x = 10
4358
right_text_area.y = center_text_area.y + center_text_area.height + 10

0 commit comments

Comments
 (0)