diff --git a/aloud.py b/aloud.py index ce500a1..9e92551 100644 --- a/aloud.py +++ b/aloud.py @@ -663,7 +663,7 @@ def _commit(self, idx): with self.out_lock: if self._above: sys.stdout.write(f"\x1b[{self._above}A") - sys.stdout.write("\x1b[J") + sys.stdout.write("\r\x1b[J") sys.stdout.write("".join(f"{l}\n" for l in para)) sys.stdout.flush() self._above = 0 diff --git a/tests/test_terminal_rendering.py b/tests/test_terminal_rendering.py new file mode 100644 index 0000000..3261a1a --- /dev/null +++ b/tests/test_terminal_rendering.py @@ -0,0 +1,29 @@ +import io +import threading +import unittest +from unittest.mock import patch + +from aloud import Player, Segment + + +class TerminalRenderingTests(unittest.TestCase): + def test_commit_returns_to_first_column_before_clearing_live_region(self): + player = Player.__new__(Player) + player.chunks = [Segment("one two three four five six")] + player.out_lock = threading.Lock() + player._above = 2 + player._para_lines = 2 + output = io.StringIO() + + with patch.object(Player, "_term_width", return_value=18), \ + patch("sys.stdout", output): + player._commit(0) + + self.assertEqual( + output.getvalue(), + "\x1b[2A\r\x1b[Jone two three four\nfive six\n", + ) + + +if __name__ == "__main__": + unittest.main()