Skip to content

Commit

Permalink
Add instructions for manually printing counters
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Sep 21, 2023
1 parent 9b46f8e commit 9eaabb2
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions doc/patterns.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,64 @@ these fields will be :py:class:`prefixed.Float` instead.
counter.update()
For more information, see the :ref:`Counter Format <counter_format>`
and the `Prefixed`_ documentation.

.. _SI (metric): https://en.wikipedia.org/wiki/Metric_prefix
.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix
.. _Prefixed: https://prefixed.readthedocs.io/en/stable/index.html
.. _Prefixed: https://prefixed.readthedocs.io/en/stable/index.html

Manually Printing
-----------------

By default, if the manager's stream is connected to a TTY, bars and counters are automatically
printed and updated. There may, however be cases where manual output is desired in addition to or
instead of the automatic output. For example, to send to other streams or print to a file.

The output for an individual bar can be retrieved from the :py:meth:`~Counter.format` method. This
supports optional arguments to specify width and elapsed time.

.. code-block:: python
import enlighten
manager = enlighten.get_manager(enabled=False)
pbar = manager.counter(desc='Progress', total=10)
pbar.update()
print(pbar.format(width=100))
As a shortcut, the counter object will call the :py:meth:`~Counter.format` method with the default
arguments when coerced to a string.

.. code-block:: python
import enlighten
manager = enlighten.get_manager(enabled=False)
pbar = manager.counter(desc='Progress', total=10)
pbar.update()
print(pbar)
While Enlighten's default output provides more advanced capability, A basic refreshing progress bar
can be created like so.

.. code-block:: python
import enlighten
import time
manager = enlighten.get_manager(enabled=False)
pbar = manager.counter(desc='Progress', total=10)
print()
for num in range(10):
time.sleep(0.2)
pbar.update()
print(f'\r{pbar}', end='', flush=True)
print()

0 comments on commit 9eaabb2

Please sign in to comment.