Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ def test_timer_finished():
sp._freeze("")

assert re.search(r"\(\d+:\d{2}:\d{2}.\d{2}\)", sp._last_frame) is not None


def test_timer_custom_format():
sp = yaspin(timer="#{}---{}#")

assert sp.elapsed_time == 0

sp._freeze("")

assert "#0:00:00---0#" in sp._last_frame
2 changes: 1 addition & 1 deletion yaspin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def yaspin(*args: Any, **kwargs: Any) -> Yaspin:
of the text string.
sigmap (dict, optional): Maps POSIX signals to their respective
handlers.
timer (bool, optional): Prints a timer showing the elapsed time.
timer (bool | str, optional): Prints a timer showing the elapsed time.

Returns:
core.Yaspin: instance of the Yaspin class.
Expand Down
8 changes: 5 additions & 3 deletions yaspin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__( # pylint: disable=too-many-arguments
reversal: bool = False,
side: str = "left",
sigmap: Optional[dict[signal.Signals, SignalHandlers]] = None,
timer: bool = False,
timer: Union[bool, str] = False,
) -> None:
# Spinner
self._spinner = self._set_spinner(spinner)
Expand Down Expand Up @@ -444,9 +444,11 @@ def _compose_out(self, frame: str, mode: Optional[str] = None) -> str:
if self._side == "right":
frame, text = text, frame
# Timer
if self._timer:
if self._timer is not False:
# TODO the format string should allow showing custom groupings (such as total seconds or HH:MM:SS)
timer_format = self._timer if isinstance(self._timer, str) else " ({}.{:02.0f})"
sec, fsec = divmod(round(100 * self.elapsed_time), 100)
text += " ({}.{:02.0f})".format( # pylint: disable=consider-using-f-string
text += timer_format.format( # pylint: disable=consider-using-f-string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if timer_format has no slots or only one slot (while .format() is called with 2 arguments) or more than 2 slots?

timedelta(seconds=sec), fsec
)
# Mode
Expand Down