zabgrab.ui.console

Terminal implementation of the UI port.

Diagnostics and prompts go to stderr; data (tables, TOML, …) goes to stdout via RichUI.print_data(), so piping stdout always gives clean output.

Functions

is_interactive(variables, *[, no_input, ...])

Tell whether the user can be prompted.

non_interactive_answer(question, default, fix)

Make the warning given when a yes/no question is answered by its default.

Classes

MessageHighlighter()

Highlights the specific, useful details in plain-text messages.

RichUI(*, interactive[, verbosity, color, debug])

User interaction via rich, implementing UI.

TomlHighlighter()

Highlights TOML: sections, keys, strings, numbers and booleans.

class zabgrab.ui.console.MessageHighlighter[source]

Bases: RegexHighlighter

Highlights the specific, useful details in plain-text messages.

Later patterns take precedence where matches overlap.

class zabgrab.ui.console.TomlHighlighter[source]

Bases: RegexHighlighter

Highlights TOML: sections, keys, strings, numbers and booleans.

Use it on lines that aren’t comments (see highlighted_toml).

Later patterns take precedence, so e.g. text inside strings is only a string.

zabgrab.ui.console.non_interactive_answer(question, default, fix)[source]

Make the warning given when a yes/no question is answered by its default.

Parameters:
  • question (str) – The question.

  • default (bool) – Its default answer.

  • fix (str) – How to answer it without being asked.

Returns:

str – The warning.

Return type:

str

zabgrab.ui.console.is_interactive(variables, *, no_input=False, stdin=None, stderr=None)[source]

Tell whether the user can be prompted.

Requires stdin and stderr to be terminals, and none of --no-input, $ZABGRAB_NO_INPUT or $CI to be set.

Parameters:
  • variables (mapping of str to str) – The environment variables.

  • no_input (bool, optional) – --no-input.

  • stdin (TextIO, optional) – The streams to check (by default, the process’s).

  • stderr (TextIO, optional) – The streams to check (by default, the process’s).

Returns:

bool – Whether the session is interactive.

Return type:

bool

class zabgrab.ui.console.RichUI(*, interactive, verbosity=0, color=None, debug=False)[source]

Bases: object

User interaction via rich, implementing UI.

Parameters:
  • interactive (bool) – Whether the user can be asked questions.

  • verbosity (int, optional) – 0 shows info and above, each -v adds a level (1 shows debug), each -q removes one (-1 hides info, -2 hides warnings). Errors are always shown.

  • color (bool, optional) – Force colour on or off; by default, it’s used on terminals.

  • debug (bool, optional) – --debug: show debug messages whatever the verbosity.

err

The console for messages, on stderr.

Type:

rich.console.Console

out

The console for data, on stdout.

Type:

rich.console.Console

property interactive: bool

Whether the user can be asked questions.

highlight(message, style='')[source]

Make a message into plain text (never markup), highlighting its details.

Parameters:
  • message (str) – The message.

  • style (str, optional) – A style for all of it.

Returns:

rich.text.Text – The highlighted text.

Return type:

Text

debug(message)[source]

Show a message only with -v or --debug.

Parameters:

message (str)

Return type:

None

info(message)[source]

Show a message (hidden by -q).

Parameters:

message (str)

Return type:

None

success(message)[source]

Show that something worked (hidden by -q).

Parameters:

message (str)

Return type:

None

warning(message)[source]

Show a warning (hidden by -qq).

Parameters:

message (str)

Return type:

None

error(message, *, details=(), hint=None)[source]

Show an error, with optional detail lines and a hint on how to fix it.

Parameters:
Return type:

None

print_exception()[source]

Print the current exception’s traceback (for --debug).

Return type:

None

print_data(data)[source]

Print command output (not diagnostics) to stdout.

Parameters:

data (ConsoleRenderable | RichCast | str)

Return type:

None

confirm(question, *, fix, default=True)[source]

Ask a yes/no question (see zabgrab.services.ports.UI.confirm()).

Parameters:
Return type:

bool

prompt(question, *, fix, parse, default=None, secret=False)[source]

Ask for free text (see zabgrab.services.ports.UI.prompt()).

Parameters:
Return type:

T

choose(question, options, *, fix, default=None)[source]

Ask for one option (see zabgrab.services.ports.UI.choose()).

Parameters:
Return type:

T

choose_many(question, options, *, fix)[source]

Ask for options (see zabgrab.services.ports.UI.choose_many()).

Parameters:
Return type:

list[T]

progress(description, total)[source]

Show progress bars, unless quiet or not on a terminal.

Parameters:
  • description (str) – The overall bar’s label.

  • total (int) – The overall number of steps.

Yields:

Progress – Adds and advances a bar per item.

Return type:

Iterator[Progress]