Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ellipsis: handle edge case when terminal is too narrow #242

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
16 changes: 16 additions & 0 deletions tests/test_ellipsis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import shutil
import threading
import time
from unittest.mock import patch

import pytest

from yaspin import yaspin


Expand Down Expand Up @@ -32,3 +35,16 @@ def long_running_function():

long_running_function()
mock_get_terminal_size.assert_called_once()


@patch("shutil.get_terminal_size")
def test_raises_when_term_is_too_small(mock_get_terminal_size):
mock_get_terminal_size.return_value.columns = 10
sp = yaspin(ellipsis="..." * 5)

with pytest.raises(ValueError):
# Some low-level trickery is needed here because Yaspin._compose_out()
# is called in a separate thread and the exception is not propagated.
# Pytest indicates that by warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))
sp._stop_spin = threading.Event()
sp._spin()
4 changes: 4 additions & 0 deletions yaspin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ def _compose_out(self, frame: str, mode: Optional[str] = None) -> str:

# Truncate
max_text_len = self._get_max_text_length(len(frame), len(timer))
if max_text_len < 1:
raise ValueError(
f"Terminal size {self._terminal_width} is too small to display spinner with the given settings."
)
text = text[:max_text_len] + self._ellipsis if len(text) > max_text_len else text

# Colors
Expand Down
Loading