Skip to content

Commit

Permalink
Refactor status_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed May 15, 2023
1 parent d8d7fed commit d3c91f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
34 changes: 20 additions & 14 deletions sphinx/util/display.py
Expand Up @@ -5,7 +5,7 @@

from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import bold, colorize, term_width_line # type: ignore
from sphinx.util.console import bold # type: ignore

if False:
from types import TracebackType
Expand All @@ -32,20 +32,26 @@ def status_iterator(
verbosity: int = 0,
stringify_func: Callable[[Any], str] = display_chunk,
) -> Iterator[T]:
single_line = verbosity < 1
bold_summary = bold(summary)
if length == 0:
logger.info(bold(summary), nonl=True)
for i, item in enumerate(iterable, start=1):
item_str = colorize(color, stringify_func(item))
if length == 0:
logger.info(item_str, nonl=True)
logger.info(' ', nonl=True)
else:
s = f'{bold(summary)}[{int(100 * i / length): >3d}%] {item_str}'
if verbosity:
logger.info(s + '\n', nonl=True)
else:
logger.info(term_width_line(s), nonl=True)
yield item
logger.info(bold_summary, nonl=True)
for item in iterable:
logger.info(stringify_func(item) + ' ', nonl=True, color=color)
yield item
else:
for i, item in enumerate(iterable, start=1):
if single_line:
# clear the entire line ('Erase in Line')
logger.info('\x1b[2K', nonl=True)
logger.info(f'{bold_summary}[{i / length: >4.0%}] ', nonl=True) # NoQA: G004
# Emit the string representation of ``item``
logger.info(stringify_func(item), nonl=True, color=color)
# If in single-line mode, emit a carriage return to move the cursor
# to the start of the line.
# If not, emit a newline to move the cursor to the next line.
logger.info('\r' * single_line, nonl=single_line)
yield item
logger.info('')


Expand Down
37 changes: 22 additions & 15 deletions tests/test_util_display.py
@@ -1,7 +1,5 @@
"""Tests util functions."""

from unittest.mock import patch

import pytest

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


@pytest.mark.sphinx('dummy')
@patch('sphinx.util.console._tw', 40) # terminal width = 40
def test_status_iterator(app, status, warning):
def test_status_iterator_length_0(app, status, warning):
logging.setup(app, status, warning)

# # test for old_status_iterator
# status.seek(0)
# status.truncate(0)
# yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
# output = strip_escseq(status.getvalue())
# assert 'testing ... hello sphinx world \n' in output
# assert yields == ['hello', 'sphinx', 'world']
# test for status_iterator (length=0)
status.seek(0)
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... '))
output = strip_escseq(status.getvalue())
assert 'testing ... hello sphinx world \n' in output
assert yields == ['hello', 'sphinx', 'world']


@pytest.mark.sphinx('dummy')
def test_status_iterator_verbosity_0(app, status, warning):
logging.setup(app, status, warning)

# test for status_iterator (verbosity=0)
status.seek(0)
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
length=3, verbosity=0))
output = strip_escseq(status.getvalue())
assert 'testing ... [ 33%] hello \r' in output
assert 'testing ... [ 66%] sphinx \r' in output
assert 'testing ... [100%] world \r\n' in output
assert 'testing ... [ 33%] hello\r' in output
assert 'testing ... [ 67%] sphinx\r' in output
assert 'testing ... [100%] world\r\n' in output
assert yields == ['hello', 'sphinx', 'world']


@pytest.mark.sphinx('dummy')
def test_status_iterator_verbosity_1(app, status, warning):
logging.setup(app, status, warning)

# test for status_iterator (verbosity=1)
status.seek(0)
status.truncate(0)
yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
length=3, verbosity=1))
output = strip_escseq(status.getvalue())
assert 'testing ... [ 33%] hello\n' in output
assert 'testing ... [ 66%] sphinx\n' in output
assert 'testing ... [ 67%] sphinx\n' in output
assert 'testing ... [100%] world\n\n' in output
assert yields == ['hello', 'sphinx', 'world']

Expand Down

0 comments on commit d3c91f9

Please sign in to comment.