Skip to content

Commit 02a46b7

Browse files
committed
Add 'one_line' to info_count()
1 parent 6717ef0 commit 02a46b7

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

cli_ui/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io
88
import os
99
import re
10+
import shutil
1011
import sys
1112
import time
1213
import traceback
@@ -330,7 +331,14 @@ def dot(*, last: bool = False, fileobj: FileObj = sys.stdout) -> None:
330331
info(".", end=end, fileobj=fileobj)
331332

332333

333-
def info_count(i: int, n: int, *rest: Token, **kwargs: Any) -> None:
334+
def erase_last_line() -> None:
335+
terminal_size = shutil.get_terminal_size()
336+
info(" " * terminal_size.columns, end="\r")
337+
338+
339+
def info_count(
340+
i: int, n: int, *rest: Token, one_line: bool = False, **kwargs: Any
341+
) -> None:
334342
"""Display a counter before the rest of the message.
335343
336344
``rest`` and ``kwargs`` are passed to :func:`info`
@@ -343,6 +351,9 @@ def info_count(i: int, n: int, *rest: Token, **kwargs: Any) -> None:
343351
num_digits = len(str(n))
344352
counter_format = "(%{}d/%d)".format(num_digits)
345353
counter_str = counter_format % (i + 1, n)
354+
if one_line:
355+
kwargs["end"] = "\r"
356+
erase_last_line()
346357
info(green, "*", reset, counter_str, reset, *rest, **kwargs)
347358

348359

@@ -711,11 +722,21 @@ def main_demo() -> None:
711722
info()
712723
info_section(bold, "progress info")
713724

725+
info_2("3 things")
714726
list_of_things = ["foo", "bar", "baz"]
715727
for i, thing in enumerate(list_of_things):
716728
info_count(i, len(list_of_things), thing)
717729
info()
718730

731+
info_2("3 other things on one line")
732+
list_of_things = ["spam", "eggs", "butter"]
733+
for i, thing in enumerate(list_of_things):
734+
time.sleep(0.5)
735+
info_count(i, len(list_of_things), thing, one_line=True)
736+
info()
737+
738+
info()
739+
info_2("Doing someting that takes some time")
719740
time.sleep(0.5)
720741
info_progress("Doing something", 5, 20)
721742
time.sleep(0.5)

0 commit comments

Comments
 (0)