77import io
88import os
99import re
10+ import shutil
1011import sys
1112import time
1213import 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