Skip to content

Commit b6c4b1e

Browse files
authored
Merge pull request #132 from jposada202020/tab_replacement
Tab replacement corrections
2 parents 815354d + 2b4f1ad commit b6c4b1e

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

adafruit_display_text/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ class LabelBase(Group):
174174
:param bool save_text: Set True to save the text string as a constant in the
175175
label structure. Set False to reduce memory use.
176176
:param: bool base_alignment: when True allows to align text label to the baseline.
177-
This is helpful when two or more labels need to be aligned to the same baseline"""
177+
This is helpful when two or more labels need to be aligned to the same baseline
178+
:param: (int,str) tab_replacement: tuple with tab character replace information. When
179+
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
180+
tab character"""
178181

179182
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
180183
def __init__(
@@ -198,6 +201,7 @@ def __init__(
198201
save_text=True, # can reduce memory use if save_text = False
199202
scale=1,
200203
base_alignment=False,
204+
tab_replacement=(4, " "),
201205
**kwargs,
202206
):
203207
super().__init__(max_size=1, x=x, y=y, scale=1)

adafruit_display_text/bitmap_label.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ class Label(LabelBase):
7070
:param bool save_text: Set True to save the text string as a constant in the
7171
label structure. Set False to reduce memory use.
7272
:param: bool base_alignment: when True allows to align text label to the baseline.
73-
This is helpful when two or more labels need to be aligned to the same baseline"""
73+
This is helpful when two or more labels need to be aligned to the same baseline
74+
:param: (int,str) tab_replacement: tuple with tab character replace information. When
75+
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
76+
tab character"""
7477

7578
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals, too-many-arguments
7679
# pylint: disable=too-many-branches, no-self-use, too-many-statements
@@ -88,7 +91,10 @@ def __init__(self, font, **kwargs):
8891
self.local_group
8992
) # the local_group will always stay in the self Group
9093

91-
self._text = kwargs.get("text", "")
94+
self._tab_replacement = kwargs.get("tab_replacement", (4, " "))
95+
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]
96+
text = kwargs.get("text", "")
97+
self._text = self._tab_text.join(text.split("\t"))
9298

9399
# Create the two-color palette
94100

@@ -114,6 +120,7 @@ def __init__(self, font, **kwargs):
114120
save_text=kwargs.get("save_text", True),
115121
scale=kwargs.get("scale", 1),
116122
base_alignment=kwargs.get("base_alignment", False),
123+
tab_replacement=kwargs.get("tab_replacement", (4, " ")),
117124
)
118125

119126
def _reset_text(
@@ -133,6 +140,7 @@ def _reset_text(
133140
save_text=None,
134141
scale=None,
135142
base_alignment=None,
143+
tab_replacement=None,
136144
):
137145

138146
# Store all the instance variables
@@ -162,13 +170,15 @@ def _reset_text(
162170
self._save_text = save_text
163171
if base_alignment is not None:
164172
self.base_alignment = base_alignment
173+
if tab_replacement is not None:
174+
self._tab_replacement = tab_replacement
165175

166176
# if text is not provided as a parameter (text is None), use the previous value.
167177
if (text is None) and self._save_text:
168178
text = self._text
169179

170180
if self._save_text: # text string will be saved
171-
self._text = text
181+
self._text = self._tab_text.join(text.split("\t"))
172182
else:
173183
self._text = None # save a None value since text string is not saved
174184

@@ -203,7 +213,7 @@ def _reset_text(
203213
loose_box_y,
204214
loose_y_offset,
205215
) = self._text_bounding_box(
206-
text,
216+
self._text,
207217
self._font,
208218
self._line_spacing,
209219
) # calculate the box size for a tight and loose backgrounds
@@ -226,7 +236,7 @@ def _reset_text(
226236
# Place the text into the Bitmap
227237
self._place_text(
228238
self.bitmap,
229-
text,
239+
self._text,
230240
self._font,
231241
self._line_spacing,
232242
self._padding_left - x_offset,
@@ -542,4 +552,5 @@ def _set_font(self, new_font):
542552
raise RuntimeError("font is immutable when save_text is False")
543553

544554
def _set_text(self, new_text, scale):
555+
new_text = self._tab_text.join(new_text.split("\t"))
545556
self._reset_text(text=new_text, scale=self.scale)

adafruit_display_text/label.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ class Label(LabelBase):
6363
containing x,y pixel coordinates.
6464
:param int scale: Integer value of the pixel scaling
6565
:param bool base_alignment: when True allows to align text label to the baseline.
66-
This is helpful when two or more labels need to be aligned to the same baseline"""
66+
This is helpful when two or more labels need to be aligned to the same baseline
67+
:param: (int,str) tab_replacement: tuple with tab character replace information. When
68+
(4, " ") will indicate a tab replacement of 4 spaces, defaults to 4 spaces by
69+
tab character"""
6770

6871
# pylint: disable=too-many-instance-attributes, too-many-locals
6972
# This has a lot of getters/setters, maybe it needs cleanup.
@@ -76,6 +79,9 @@ def __init__(self, font, **kwargs):
7679

7780
if not max_glyphs and not text:
7881
raise RuntimeError("Please provide a max size, or initial text")
82+
self._tab_replacement = kwargs.get("tab_replacement", (4, " "))
83+
self._tab_text = self._tab_replacement[1] * self._tab_replacement[0]
84+
text = self._tab_text.join(text.split("\t"))
7985
if not max_glyphs:
8086
max_glyphs = len(text)
8187
# add one to max_size for the background bitmap tileGrid
@@ -300,6 +306,7 @@ def _update_text(
300306
self._update_background_color(self._background_color)
301307

302308
def _reset_text(self, new_text):
309+
new_text = self._tab_text.join(new_text.split("\t"))
303310
try:
304311
current_anchored_position = self.anchored_position
305312
self._update_text(str(new_text))

0 commit comments

Comments
 (0)