Skip to content

UI

Interactive components (prompts, confirmations, continue/quit, wait spinner). Standard library only and portable: copy clak/ui/ into any project.

See the UI guide for usage.

clak.ui

Interactive UI components (prompts, confirmations, pauses, waits).

Standard library only, self-contained and portable: relative imports only and no clak dependency, so this folder can be copied as-is into any project. Prompts go to stderr by default so stdout stays clean for piped data.

UIComponent

Base class: holds input/output streams so components are testable.

Prompts and messages go to stderr by default so stdout stays clean for piped data.

interactive property

True when prompting is allowed (see policy.detect_interactive).

Explicit interactive wins, then the policy, then autodetection on this component's streams (TTY on input and output, outside CI).

preanswer(kind, message, **fields)

Ask the policy first. Returns an answer or NO_ANSWER.

Without a policy nothing is decided here: the component reads input. Raises UINoInputError when prompting is forbidden and nobody answers.

readline(prompt='')

Read one line (without newline). Raises UIAbort on EOF/Ctrl-C.

write(text='', end='\n')

Write text to the UI stream.

UIAbort

Bases: Exception

Raised when the user aborts an interaction (Ctrl-C / Ctrl-D / quit).

TextPrompt

Bases: UIComponent

Ask for a free text value, with default, cast and validation.

ask()

Return the answer. Raises UIAbort on abort or too many bad answers.

ConfirmPrompt

Bases: UIComponent

Yes / no question.

__init__(message, *, default=None, dangerous=False, **kwargs)

dangerous: never accepted from defaults, only an explicit yes.

ask()

Return True/False. Raises UIAbort on abort.

ChoicePrompt

Bases: UIComponent

Pick one item from a numbered list (number or exact label).

ask()

Return the selected item. Raises UIAbort on abort / bad answers.

ContinuePrompt

Bases: UIComponent

Pause until the user presses Enter; q quits (raises UIAbort).

Non-interactive sessions never block: ask() returns True immediately.

ask()

Return True to continue, False to quit (or raise UIAbort).

WaitSpinner

Bases: UIComponent

Show a message (with a spinner on TTY) while work runs.

Use as a context manager, or call sleep(seconds)::

with WaitSpinner("Loading"):
    do_work()

sleep(seconds)

Wait for seconds with the indicator displayed.

start()

Start displaying the wait indicator.

stop(done='done')

Stop the indicator and print the final status.

UIPolicy dataclass

Interactivity switch plus ordered responders.

always(assume_yes=False) classmethod

Always prompt.

answer(question)

Return an answer, NO_ANSWER (go read input), or raise.

Raises:

Type Description
UINoInputError

nobody answered and prompting is forbidden.

auto(assume_yes=False, **detect) classmethod

Policy from TTY / CI detection (see :func:detect_interactive).

build(interactive, assume_yes=False, **kwargs) classmethod

Standard policy: [Yes?] then [Defaults] when not interactive.

never(assume_yes=False) classmethod

Never prompt: defaults or fail.

UISession

Factory shortcuts sharing a policy and component kwargs (streams...).

choice(message, choices, **kwargs)

Ask for one item (see ChoicePrompt).

confirm(message, **kwargs)

Ask yes/no (see ConfirmPrompt).

pause(message=None, **kwargs)

Continue / quit pause (see ContinuePrompt).

spinner(message='Please wait', **kwargs)

Wait spinner context manager (see WaitSpinner).

text(message, **kwargs)

Ask for text (see TextPrompt).

Question dataclass

What a component is about to ask.

UINoInputError

Bases: UIAbort

Input was required but the policy forbids prompting.

YesResponder

Answer every confirmation with yes (including dangerous ones).

DefaultsResponder

Answer with the question default; refuse dangerous confirmations.

A non-required question without default is answered None.

MappingResponder

Preseeded answers keyed by question message (tests, answer files).

detect_interactive(stdin=None, stderr=None, env=None)

Autodetect: True only for a TTY on stdin and stderr, outside CI.

CI set to anything but 0/false/no/off forces False.

Runtime flags

See the unattended runs guide.

clak.comp.runtime

General runtime flags: attended vs unattended execution.

RuntimeOptMixin adds three flags describing how the process runs, not what it shows, so any mixin or command can read them:

  • --interactive WHEN: auto (default) / always / never, like ls --color=WHEN. auto means TTY on stdin and stderr, outside CI.
  • --no-input: alias of --interactive never (pip, gcloud, Django).
  • -y/--yes: answer confirmations with yes.

Precedence for interactivity: CLI > $CLAK_INTERACTIVE > $CI > TTY. There is deliberately no environment variable for --yes.

Resolved values are published on the context as ctx.interactive and ctx.assume_yes (also ctx.plugins["runtime"]), and fed to the generic UI settings (:func:clak.comp.ui.set_ui_settings), which :func:clak.comp.ui.ui_session reads.

RuntimeOptMixin

--interactive, --no-input and -y/--yes flags.

cli_hook__runtime(instance, ctx, **_)

Resolve runtime flags and publish them on ctx.

resolve_runtime(args, env=None, stdin=None, stderr=None)

Resolve runtime flags (pure function, no side effects).

Parameters:

Name Type Description Default
args Mapping[str, Any]

parsed CLI values (interactive, no_input, yes).

required
env Mapping[str, str] | None

environment (default os.environ).

None
stdin IO[str] | None

input stream used for TTY detection (default sys.stdin).

None
stderr IO[str] | None

error stream used for TTY detection (default sys.stderr).

None

Returns:

Type Description
dict

{"interactive": bool, "assume_yes": bool, "mode": str, "source": str}

Raises:

Type Description
ClakUserError

--no-input combined with --interactive other than never.

set_ui_settings(ctx, **values)

Merge UI settings into ctx.plugins["ui_settings"] and return them.

Any producer (RuntimeOptMixin, your own flags, tests) can call this. Policy keys: interactive, assume_yes, hint, yes_hint and responders (appended, not replaced). Other keys (stdin, stderr...) are passed to components as kwargs.