CLI utilities - tmuxp.cli.utils

CLI utility helpers for tmuxp.

class tmuxp.cli.utils.ColorMode

Bases: Enum

Color output modes for CLI.

Examples

>>> ColorMode.AUTO.value
'auto'
>>> ColorMode.ALWAYS.value
'always'
>>> ColorMode.NEVER.value
'never'
class tmuxp.cli.utils.Colors

Bases: object

Semantic color utilities for CLI output.

Provides semantic color methods (success, warning, error, etc.) that conditionally apply ANSI colors based on the color mode and environment.

Parameters:

mode (ColorMode) – Color mode to use. Default is AUTO which detects TTY.

Examples

>>> colors = Colors(ColorMode.NEVER)
>>> colors.success("session loaded")
'session loaded'
>>> colors.error("failed to load")
'failed to load'
>>> colors = Colors(ColorMode.ALWAYS)
>>> result = colors.success("ok")

Check that result contains ANSI escape codes:

>>> "\033[" in result
True
exception tmuxp.cli.utils.UnknownStyleColor
exception[source]

Bases: Exception

Raised when encountering an unknown terminal style color.

Examples

>>> try:
...     raise UnknownStyleColor("invalid_color")
... except UnknownStyleColor as e:
...     "invalid_color" in str(e)
True
tmuxp.cli.utils.prompt(name, default=None, value_proc=None, *, color_mode=None)
function[source]

Return user input from command line.

Parameters:
  • name (str) – prompt text

  • default (str | None) – default value if no input provided.

  • color_mode (ColorMode | None) – color mode for prompt styling. Defaults to AUTO if not specified.

  • value_proc (Callable[[str], str] | None)

Return type:

str

See also

prompt(), prompt_bool(), :py:obj:``flask-script`, :py:obj:``flask-script`

tmuxp.cli.utils.prompt_bool(name, default=False, yes_choices=None, no_choices=None, *, color_mode=None)
function[source]

Return True / False by prompting user input from command line.

Parameters:
  • name (str) – prompt text

  • default (bool) – default value if no input provided.

  • yes_choices (Sequence[Any] | None) – default ‘y’, ‘yes’, ‘1’, ‘on’, ‘true’, ‘t’

  • no_choices (Sequence[Any] | None) – default ‘n’, ‘no’, ‘0’, ‘off’, ‘false’, ‘f’

  • color_mode (ColorMode | None) – color mode for prompt styling. Defaults to AUTO if not specified.

Return type:

bool

tmuxp.cli.utils.prompt_choices(name, choices, default=None, no_choice=('none',), *, color_mode=None)
function[source]

Return user input from command line from set of provided choices.

Parameters:
  • name (str) – prompt text

  • choices (Sequence[str | tuple[str, str]]) – Sequence of available choices. Each choice may be a single string or a (key, value) tuple where key is used for matching.

  • default (str | None) – default value if no input provided.

  • no_choice (Sequence[str]) – acceptable list of strings for “null choice”

  • color_mode (ColorMode | None) – color mode for prompt styling. Defaults to AUTO if not specified.

Return type:

str

tmuxp.cli.utils.prompt_yes_no(name, default=True, *, color_mode=None)
function[source]

prompt_bool() returning yes by default.

Parameters:
  • name (str) – prompt text

  • default (bool) – default value if no input provided.

  • color_mode (ColorMode | None) – color mode for prompt styling. Defaults to AUTO if not specified.

Return type:

bool

tmuxp.cli.utils.strip_ansi(value)
function[source]

Clear ANSI escape codes from a string value.

Parameters:

value (str) – String potentially containing ANSI escape codes.

Returns:

String with ANSI codes removed.

Return type:

str

Examples

>>> strip_ansi("\033[32mgreen\033[0m")
'green'
>>> strip_ansi("plain text")
'plain text'
tmuxp.cli.utils.style(text, fg=None, bg=None, bold=None, dim=None, underline=None, overline=None, italic=None, blink=None, reverse=None, strikethrough=None, reset=True)
function[source]

Apply ANSI styling to text.

Credit: click.

Parameters:
  • text (Any) – Text to style (will be converted to str).

  • fg (CLIColour | None) – Foreground color (name, 256-index, or RGB tuple).

  • bg (CLIColour | None) – Background color.

  • bold (bool | None) – Apply bold style.

  • dim (bool | None) – Apply dim style.

  • underline (bool | None) – Apply underline style.

  • overline (bool | None) – Apply overline style.

  • italic (bool | None) – Apply italic style.

  • blink (bool | None) – Apply blink style.

  • reverse (bool | None) – Apply reverse video style.

  • strikethrough (bool | None) – Apply strikethrough style.

  • reset (bool) – Append reset code at end. Default True.

Returns:

Styled text with ANSI escape codes.

Return type:

str

Examples

>>> style("hello", fg="green")
'\x1b[32m...'
>>> "hello" in style("hello", fg="green")
True
tmuxp.cli.utils.tmuxp_echo(message=None, file=None)
function[source]

Print user-facing CLI output.

Parameters:
  • message (str | None) – Message to print. If None, does nothing.

  • file (t.TextIO | None) – Output stream. Defaults to sys.stdout.

Return type:

None

Examples

>>> tmuxp_echo("Session loaded")
Session loaded
>>> tmuxp_echo("Warning message")
Warning message
tmuxp.cli.utils.unstyle(text)
function[source]

Remove ANSI styling information from a string.

Usually it’s not necessary to use this function as tmuxp_echo function will automatically remove styling if necessary.

Credit: click.

Parameters:

text (str) – Text to remove style information from.

Returns:

Text with ANSI codes removed.

Return type:

str

Examples

>>> unstyle("\033[32mgreen\033[0m")
'green'