topmark.cli.console package index¶
Console runtime support for the TopMark CLI.
This package contains the concrete console implementations, protocol types, color-resolution helpers, and small runtime utilities used by CLI commands to print user-facing output.
Responsibilities: - Define the framework-agnostic console protocol used by commands. - Provide the Click-backed console implementation and a stdlib fallback. - Resolve terminal color behavior from CLI flags, environment, and output mode. - Expose small runtime helpers such as active-console resolution and terminal width inspection.
This package is intentionally distinct from topmark.presentation, which owns
human-facing report preparation and pure TEXT / MARKDOWN rendering.
ClickStyleKwargs ¶
Bases: TypedDict
Documented subset of keyword arguments accepted by click.style().
Console ¶
Bases: ConsoleProtocol
Program-output console, independent from the logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable_color
|
bool
|
If True, enables ANSI color codes in the output. Otherwise, all output is plain text. |
True
|
out
|
TextIO | None
|
The text stream to use for standard output. Defaults to |
None
|
err
|
TextIO | None
|
The text stream to use for error output. Defaults to |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
enable_color |
bool
|
Whether to emit ANSI color codes. |
out |
TextIO | None
|
Stream for standard output (defaults to sys.stdout). |
err |
TextIO | None
|
Stream for error output (defaults to sys.stderr). |
Source code in src/topmark/cli/console/click_console.py
print ¶
Write a message to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Message text. |
''
|
nl
|
bool
|
If True, append a newline. |
True
|
warn ¶
Write a warning message to stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Warning text. |
required |
nl
|
bool
|
If True, append a newline. |
True
|
Source code in src/topmark/cli/console/click_console.py
error ¶
Write an error message to stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Error text. |
required |
nl
|
bool
|
If True, append a newline. |
True
|
Source code in src/topmark/cli/console/click_console.py
ColorMode ¶
Bases: str, Enum
User intent for colorized terminal output.
Attributes:
| Name | Type | Description |
|---|---|---|
AUTO |
Enable color only when appropriate (typically when stdout is a TTY). |
|
ALWAYS |
Force-enable color regardless of TTY status. |
|
NEVER |
Disable color entirely. |
Typical usage
- Parse
--color=auto|always|neverasColorMode. - Pass the parsed value to
resolve_color_mode()along with the current output format (e.g.,"json"or"ndjson") to obtain a finalboolindicating whether to emit ANSI styles.
Example
resolve_color_mode(color_mode_override=ColorMode.AUTO, output_format=None) True # on an interactive terminal resolve_color_mode(color_mode_override=ColorMode.AUTO, output_format="json") False # machine-readable formats are always colorless
ConsoleProtocol ¶
Bases: Protocol
Minimal behavioral protocol for user-facing CLI console output.
Implementations may be backed by Click, Rich, or plain stdlib streams. The protocol intentionally stays small so commands depend only on core output behavior, not on framework-specific details. Console instances are created during CLI state initialization and passed explicitly to commands and emitters; this protocol does not imply ownership of global stdout/stderr.
StdConsole ¶
Bases: ConsoleProtocol
Simple console implementation backed by stdlib text streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable_color
|
bool
|
Ignored for this implementation. Present only to keep the constructor shape aligned with the Click-backed console. |
False
|
out
|
TextIO | None
|
Stream for normal output. Defaults to |
None
|
err
|
TextIO | None
|
Stream for warning/error output. Defaults to |
None
|
Source code in src/topmark/cli/console/standard_console.py
resolve_color_mode ¶
Determine whether color output should be enabled.
Decision precedence
- Machine-readable formats: If
output_formatis"json"or"ndjson", return False. - CLI override: If
color_mode_overrideisALWAYS→ True; ifNEVER→ False. - Environment:
FORCE_COLOR(set and not equal to"0") → TrueNO_COLOR(set to any value) → False
- Auto: If none of the above decide, return
stdout.isatty().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_mode_override
|
ColorMode | None
|
Parsed |
required |
output_format
|
str | None
|
Structured output mode; |
required |
stdout_isatty
|
bool | None
|
Optional override for TTY detection. When |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if ANSI color should be enabled; False otherwise. |
Examples:
>>> resolve_color_mode(color_mode_override=ColorMode.NEVER, output_format=None)
False
>>> resolve_color_mode(color_mode_override=None, output_format="ndjson")
False
>>> resolve_color_mode(color_mode_override=None, output_format=None, stdout_isatty=True)
True
Source code in src/topmark/cli/console/color.py
get_console_line_width ¶
Return a safe line width for human-facing console rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
int
|
Fallback terminal width when no terminal size is available. |
80
|
max_width
|
int
|
Upper bound applied to wide terminals to keep output readable. |
100
|
Returns:
| Type | Description |
|---|---|
int
|
The smaller of the detected terminal width and |
int
|
size detection fails, |
Source code in src/topmark/cli/console/utils.py
Immediate children in this package¶
- topmark.cli.console.click_console
- Click-backed console implementation for TopMark CLI output.
- topmark.cli.console.color
- Color-resolution helpers for TopMark CLI console output.
- topmark.cli.console.protocols
- Framework-agnostic console protocol for user-facing CLI output.
- topmark.cli.console.standard_console
- Stdlib-based console implementation used outside Click contexts.
- topmark.cli.console.utils
- Small terminal-layout helpers for CLI console output.