Skip to content

Commit

Permalink
Merge pull request #242 from pavdmyt/handle-ellipsis-edge-cases
Browse files Browse the repository at this point in the history
ellipsis: handle edge case when terminal is too narrow

#242
  • Loading branch information
pavdmyt authored Sep 18, 2024
2 parents 9af4817 + 1aeea8d commit a8076f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit a8076f6

Please sign in to comment.