Skip to content

Commit

Permalink
WIP allow text to be dynamic
Browse files Browse the repository at this point in the history
by providing a function.
  • Loading branch information
davidxia committed Nov 11, 2021
1 parent 89c7934 commit 07877ec
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions halo/halo.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ def _get_text(self, text):
self
"""
animation = self._animation
stripped_text = text.strip()
stripped_text = lambda: text().strip() if hasattr(text, '__call__') else text.strip()

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

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

frames = []

Expand All @@ -366,18 +366,30 @@ def _get_text(self, text):
Make the text bounce back and forth
"""
for x in range(0, text_length - terminal_width + 1):
frames.append(stripped_text[x : terminal_width + x])
if hasattr(text, '__call__'):
frame = stripped_text()[x: terminal_width + x]
else:
frame = stripped_text[x: terminal_width + x]
frames.append(frame)
frames.extend(list(reversed(frames)))
elif "marquee":
"""
Make the text scroll like a marquee
"""
stripped_text = stripped_text + " " + stripped_text[:terminal_width]
for x in range(0, text_length + 1):
frames.append(stripped_text[x : terminal_width + x])
if hasattr(text, '__call__'):
new_stripped_text = lambda: stripped_text() + " " + stripped_text()[:terminal_width]
for x in range(0, text_length + 1):
frames.append(new_stripped_text()[x: terminal_width + x])
else:
stripped_text = stripped_text + " " + stripped_text[:terminal_width]
for x in range(0, text_length + 1):
frames.append(stripped_text[x: terminal_width + x])
elif terminal_width < text_length and not animation:
# Add ellipsis if text is larger than terminal width and no animation was specified
frames = [stripped_text[: terminal_width - 6] + " (...)"]
if hasattr(text, '__call__'):
frames = [lambda: stripped_text()[: terminal_width - 6] + " (...)"]
else:
frames = [stripped_text[: terminal_width - 6] + " (...)"]
else:
frames = [stripped_text]

Expand Down Expand Up @@ -454,14 +466,19 @@ def text_frame(self):
self
"""
if len(self._text["frames"]) == 1:
frame = self._text["frames"][0]
if hasattr(frame, '__call__'):
frame = frame()
if self._text_color:
return colored_frame(self._text["frames"][0], self._text_color)
return colored_frame(frame, self._text_color)

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

frames = self._text["frames"]
frame = frames[self._text_index]
if hasattr(frame, '__call__'):
frame = frame()

self._text_index += 1
self._text_index = self._text_index % len(frames)
Expand Down

0 comments on commit 07877ec

Please sign in to comment.