Skip to content

Commit 07877ec

Browse files
committed
WIP allow text to be dynamic
by providing a function.
1 parent 89c7934 commit 07877ec

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

halo/halo.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,15 @@ def _get_text(self, text):
348348
self
349349
"""
350350
animation = self._animation
351-
stripped_text = text.strip()
351+
stripped_text = lambda: text().strip() if hasattr(text, '__call__') else text.strip()
352352

353353
# Check which frame of the animation is the widest
354354
max_spinner_length = max([len(i) for i in self._spinner["frames"]])
355355

356-
# Subtract to the current terminal size the max spinner length
356+
# Subtract from the current terminal size the max spinner length
357357
# (-1 to leave room for the extra space between spinner and text)
358358
terminal_width = get_terminal_columns() - max_spinner_length - 1
359-
text_length = len(stripped_text)
359+
text_length = len(stripped_text()) if hasattr(text, '__call__') else len(stripped_text)
360360

361361
frames = []
362362

@@ -366,18 +366,30 @@ def _get_text(self, text):
366366
Make the text bounce back and forth
367367
"""
368368
for x in range(0, text_length - terminal_width + 1):
369-
frames.append(stripped_text[x : terminal_width + x])
369+
if hasattr(text, '__call__'):
370+
frame = stripped_text()[x: terminal_width + x]
371+
else:
372+
frame = stripped_text[x: terminal_width + x]
373+
frames.append(frame)
370374
frames.extend(list(reversed(frames)))
371375
elif "marquee":
372376
"""
373377
Make the text scroll like a marquee
374378
"""
375-
stripped_text = stripped_text + " " + stripped_text[:terminal_width]
376-
for x in range(0, text_length + 1):
377-
frames.append(stripped_text[x : terminal_width + x])
379+
if hasattr(text, '__call__'):
380+
new_stripped_text = lambda: stripped_text() + " " + stripped_text()[:terminal_width]
381+
for x in range(0, text_length + 1):
382+
frames.append(new_stripped_text()[x: terminal_width + x])
383+
else:
384+
stripped_text = stripped_text + " " + stripped_text[:terminal_width]
385+
for x in range(0, text_length + 1):
386+
frames.append(stripped_text[x: terminal_width + x])
378387
elif terminal_width < text_length and not animation:
379388
# Add ellipsis if text is larger than terminal width and no animation was specified
380-
frames = [stripped_text[: terminal_width - 6] + " (...)"]
389+
if hasattr(text, '__call__'):
390+
frames = [lambda: stripped_text()[: terminal_width - 6] + " (...)"]
391+
else:
392+
frames = [stripped_text[: terminal_width - 6] + " (...)"]
381393
else:
382394
frames = [stripped_text]
383395

@@ -454,14 +466,19 @@ def text_frame(self):
454466
self
455467
"""
456468
if len(self._text["frames"]) == 1:
469+
frame = self._text["frames"][0]
470+
if hasattr(frame, '__call__'):
471+
frame = frame()
457472
if self._text_color:
458-
return colored_frame(self._text["frames"][0], self._text_color)
473+
return colored_frame(frame, self._text_color)
459474

460475
# Return first frame (can't return original text because at this point it might be ellipsed)
461-
return self._text["frames"][0]
476+
return frame
462477

463478
frames = self._text["frames"]
464479
frame = frames[self._text_index]
480+
if hasattr(frame, '__call__'):
481+
frame = frame()
465482

466483
self._text_index += 1
467484
self._text_index = self._text_index % len(frames)

0 commit comments

Comments
 (0)