diff --git a/astropy/utils/console.py b/astropy/utils/console.py index 797859d8ba6..3d3f48500ef 100644 --- a/astropy/utils/console.py +++ b/astropy/utils/console.py @@ -831,7 +831,7 @@ class Spinner: with Spinner("Reticulating splines", "green") as s: for item in enumerate(items): - s.next() + s.update() """ _default_unicode_chars = "◓◑◒◐" _default_ascii_chars = "-/|\\" @@ -880,6 +880,11 @@ def __init__(self, msg, color='default', file=None, step=1, self._silent = not isatty(file) + if self._silent: + self._iter = self._silent_iterator() + else: + self._iter = self._iterator() + def _iterator(self): chars = self._chars index = 0 @@ -912,10 +917,7 @@ def _iterator(self): index = (index + 1) % len(chars) def __enter__(self): - if self._silent: - return self._silent_iterator() - else: - return self._iterator() + return self def __exit__(self, exc_type, exc_value, traceback): file = self._file @@ -931,6 +933,24 @@ def __exit__(self, exc_type, exc_value, traceback): color_print(' [Failed]', 'red', file=file) flush() + def __iter__(self): + return self + + def __next__(self): + next(self._iter) + + def update(self, value=None): + """Update the spin wheel in the terminal. + + Parameters + ---------- + value : int, optional + Ignored (present just for compatibility with `ProgressBar.update`). + + """ + + next(self) + def _silent_iterator(self): color_print(self._msg, self._color, file=self._file, end='') self._file.flush() @@ -997,7 +1017,6 @@ def __init__(self, total, msg, color='default', file=None): self._obj = ProgressBar(total, file=file) def __enter__(self): - self._iter = self._obj.__enter__() return self def __exit__(self, exc_type, exc_value, traceback): @@ -1008,10 +1027,7 @@ def update(self, value): Update the progress bar to the given value (out of the total given to the constructor. """ - if self._is_spinner: - next(self._iter) - else: - self._obj.update(value) + self._obj.update(value) def print_code_line(line, col=None, file=None, tabwidth=8, width=70): diff --git a/docs/changes/utils/11772.api.rst b/docs/changes/utils/11772.api.rst new file mode 100644 index 00000000000..36942ee937c --- /dev/null +++ b/docs/changes/utils/11772.api.rst @@ -0,0 +1,3 @@ +Updated ``utils.console.Spinner`` to better resemble the API of +``utils.console.ProgressBar``, including an ``update()`` method and +iterator support.