Skip to content

Commit d3c91f9

Browse files
committed
Refactor status_iterator
1 parent d8d7fed commit d3c91f9

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

sphinx/util/display.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from sphinx.locale import __
77
from sphinx.util import logging
8-
from sphinx.util.console import bold, colorize, term_width_line # type: ignore
8+
from sphinx.util.console import bold # type: ignore
99

1010
if False:
1111
from types import TracebackType
@@ -32,20 +32,26 @@ def status_iterator(
3232
verbosity: int = 0,
3333
stringify_func: Callable[[Any], str] = display_chunk,
3434
) -> Iterator[T]:
35+
single_line = verbosity < 1
36+
bold_summary = bold(summary)
3537
if length == 0:
36-
logger.info(bold(summary), nonl=True)
37-
for i, item in enumerate(iterable, start=1):
38-
item_str = colorize(color, stringify_func(item))
39-
if length == 0:
40-
logger.info(item_str, nonl=True)
41-
logger.info(' ', nonl=True)
42-
else:
43-
s = f'{bold(summary)}[{int(100 * i / length): >3d}%] {item_str}'
44-
if verbosity:
45-
logger.info(s + '\n', nonl=True)
46-
else:
47-
logger.info(term_width_line(s), nonl=True)
48-
yield item
38+
logger.info(bold_summary, nonl=True)
39+
for item in iterable:
40+
logger.info(stringify_func(item) + ' ', nonl=True, color=color)
41+
yield item
42+
else:
43+
for i, item in enumerate(iterable, start=1):
44+
if single_line:
45+
# clear the entire line ('Erase in Line')
46+
logger.info('\x1b[2K', nonl=True)
47+
logger.info(f'{bold_summary}[{i / length: >4.0%}] ', nonl=True) # NoQA: G004
48+
# Emit the string representation of ``item``
49+
logger.info(stringify_func(item), nonl=True, color=color)
50+
# If in single-line mode, emit a carriage return to move the cursor
51+
# to the start of the line.
52+
# If not, emit a newline to move the cursor to the next line.
53+
logger.info('\r' * single_line, nonl=single_line)
54+
yield item
4955
logger.info('')
5056

5157

tests/test_util_display.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Tests util functions."""
22

3-
from unittest.mock import patch
4-
53
import pytest
64

75
from sphinx.testing.util import strip_escseq
@@ -23,37 +21,46 @@ def test_display_chunk():
2321

2422

2523
@pytest.mark.sphinx('dummy')
26-
@patch('sphinx.util.console._tw', 40) # terminal width = 40
27-
def test_status_iterator(app, status, warning):
24+
def test_status_iterator_length_0(app, status, warning):
2825
logging.setup(app, status, warning)
2926

30-
# # test for old_status_iterator
31-
# status.seek(0)
32-
# status.truncate(0)
33-
# yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
34-
# output = strip_escseq(status.getvalue())
35-
# assert 'testing ... hello sphinx world \n' in output
36-
# assert yields == ['hello', 'sphinx', 'world']
27+
# test for status_iterator (length=0)
28+
status.seek(0)
29+
status.truncate(0)
30+
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
31+
output = strip_escseq(status.getvalue())
32+
assert 'testing ... hello sphinx world \n' in output
33+
assert yields == ['hello', 'sphinx', 'world']
34+
35+
36+
@pytest.mark.sphinx('dummy')
37+
def test_status_iterator_verbosity_0(app, status, warning):
38+
logging.setup(app, status, warning)
3739

3840
# test for status_iterator (verbosity=0)
3941
status.seek(0)
4042
status.truncate(0)
4143
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
4244
length=3, verbosity=0))
4345
output = strip_escseq(status.getvalue())
44-
assert 'testing ... [ 33%] hello \r' in output
45-
assert 'testing ... [ 66%] sphinx \r' in output
46-
assert 'testing ... [100%] world \r\n' in output
46+
assert 'testing ... [ 33%] hello\r' in output
47+
assert 'testing ... [ 67%] sphinx\r' in output
48+
assert 'testing ... [100%] world\r\n' in output
4749
assert yields == ['hello', 'sphinx', 'world']
4850

51+
52+
@pytest.mark.sphinx('dummy')
53+
def test_status_iterator_verbosity_1(app, status, warning):
54+
logging.setup(app, status, warning)
55+
4956
# test for status_iterator (verbosity=1)
5057
status.seek(0)
5158
status.truncate(0)
5259
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
5360
length=3, verbosity=1))
5461
output = strip_escseq(status.getvalue())
5562
assert 'testing ... [ 33%] hello\n' in output
56-
assert 'testing ... [ 66%] sphinx\n' in output
63+
assert 'testing ... [ 67%] sphinx\n' in output
5764
assert 'testing ... [100%] world\n\n' in output
5865
assert yields == ['hello', 'sphinx', 'world']
5966

0 commit comments

Comments
 (0)