(
iterable: Iterable[T],
summary: str,
color: str = 'darkgreen',
length: int = 0,
verbosity: int = 0,
stringify_func: Callable[[Any], str] = display_chunk,
)
| 24 | |
| 25 | |
| 26 | def status_iterator[T]( |
| 27 | iterable: Iterable[T], |
| 28 | summary: str, |
| 29 | color: str = 'darkgreen', |
| 30 | length: int = 0, |
| 31 | verbosity: int = 0, |
| 32 | stringify_func: Callable[[Any], str] = display_chunk, |
| 33 | ) -> Iterator[T]: |
| 34 | # printing on a single line requires ANSI control sequences |
| 35 | single_line = verbosity < 1 and terminal_supports_colour() |
| 36 | bold_summary = bold(summary) |
| 37 | if length == 0: |
| 38 | logger.info(bold_summary, nonl=True) |
| 39 | for item in iterable: |
| 40 | logger.info('%s ', 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 |
| 55 | logger.info('') |
| 56 | |
| 57 | |
| 58 | class SkipProgressMessage(Exception): |
searching dependent graphs…