Skip to content

Commit 9790004

Browse files
committed
WIP allow text to be dynamic
by providing a function like so. ```python with Halo(text=lambda: f'date {datetime.datetime.now()}'): time.sleep(100) ```
1 parent 89c7934 commit 9790004

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

halo/halo.py

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

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

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

361364
frames = []
362365

@@ -366,18 +369,30 @@ def _get_text(self, text):
366369
Make the text bounce back and forth
367370
"""
368371
for x in range(0, text_length - terminal_width + 1):
369-
frames.append(stripped_text[x : terminal_width + x])
372+
if hasattr(text, '__call__'):
373+
frame = stripped_text()[x: terminal_width + x]
374+
else:
375+
frame = stripped_text[x: terminal_width + x]
376+
frames.append(frame)
370377
frames.extend(list(reversed(frames)))
371378
elif "marquee":
372379
"""
373380
Make the text scroll like a marquee
374381
"""
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])
382+
if hasattr(text, '__call__'):
383+
new_stripped_text = lambda: stripped_text() + " " + stripped_text()[:terminal_width]
384+
for x in range(0, text_length + 1):
385+
frames.append(new_stripped_text()[x: terminal_width + x])
386+
else:
387+
stripped_text = stripped_text + " " + stripped_text[:terminal_width]
388+
for x in range(0, text_length + 1):
389+
frames.append(stripped_text[x: terminal_width + x])
378390
elif terminal_width < text_length and not animation:
379391
# Add ellipsis if text is larger than terminal width and no animation was specified
380-
frames = [stripped_text[: terminal_width - 6] + " (...)"]
392+
if hasattr(text, '__call__'):
393+
frames = [lambda: stripped_text()[: terminal_width - 6] + " (...)"]
394+
else:
395+
frames = [stripped_text[: terminal_width - 6] + " (...)"]
381396
else:
382397
frames = [stripped_text]
383398

@@ -454,14 +469,19 @@ def text_frame(self):
454469
self
455470
"""
456471
if len(self._text["frames"]) == 1:
472+
frame = self._text["frames"][0]
473+
if hasattr(frame, '__call__'):
474+
frame = frame()
457475
if self._text_color:
458-
return colored_frame(self._text["frames"][0], self._text_color)
476+
return colored_frame(frame, self._text_color)
459477

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

463481
frames = self._text["frames"]
464482
frame = frames[self._text_index]
483+
if hasattr(frame, '__call__'):
484+
frame = frame()
465485

466486
self._text_index += 1
467487
self._text_index = self._text_index % len(frames)

0 commit comments

Comments
 (0)