Skip to content

madbot_cli.progress

Show what a command is doing while it runs.

In a terminal, one line says the step under way and is written again in place while madbot works in the background (madbot reads the data: 2/4, 35 s). Elsewhere (a file, a CI log), each step is one line, a wait writes a new line only when what it says changes, and no control character is written.

Attributes

NameDescription
CLEAR_LINETakes the cursor back to the start of the line and erases it, in a terminal.

Classes

NameDescription
ProgressThe step under way, and the lines a command writes around it.

CLEAR_LINEattributemodule attribute#

CLEAR_LINE = '\r\x1b[2K'

Takes the cursor back to the start of the line and erases it, in a terminal.

Progressclass#

class Progress(stream: TextIO, *, steps: bool = True, live: bool | None = None, clock: Callable[[], float] = time.monotonic, columns: Callable[[], int] | None = None)

The step under way, and the lines a command writes around it.

Parameters

NameTypeDefaultDescription
streamTextIOrequiredWhere to write.
stepsboolTrueSay the steps; False (--quiet) writes only the lines that stay.
liveboolNoneWrite the step again in place; by default, when stream is a terminal.
clockcallabletime.monotonicGives the time in seconds, to say how long a step lasts.
columnscallableNoneGives the width of the terminal: a longer line would wrap, and only its last row would be erased. The terminal’s by default.

clockattributeinstance attribute#

clock = clock

columnsattributeinstance attribute#

columns = columns or lambda : shutil.get_terminal_size().columns

liveattributeinstance attribute#

live = live

saidattributeinstance attribute#

said = ''

The last step written, out of a terminal: a wait writes it once.

shownattributeinstance attribute#

shown = ''

The step written in place, to erase before another line.

startedattributeinstance attribute#

started = clock()

stepsattributeinstance attribute#

steps = steps

streamattributeinstance attribute#

stream = stream

clearmethod#

def clear() -> None

Erase the step written in place, once the command is over.

linemethod#

def line(text: str) -> None

Write a line that stays, such as a change, above the step under way.

Parameters

NameTypeDescription
textstrThe line.

stepmethod#

def step(text: str) -> None

Say a new step has started.

Parameters

NameTypeDescription
textstrWhat it does, such as reading what madbot holds.

updatemethod#

def update(text: str, elapsed: float | None = None) -> None

Say where the step under way stands, with the time it has taken.

Out of a terminal, the line is written only when text changes.

Parameters

NameTypeDefaultDescription
textstrrequiredWhere it stands, such as madbot reads the data: 2/4.
elapsedfloatNoneThe seconds it has taken; since the last step by default.