Views
Add a view mixin to auto-render command results and expose matching CLI options.
from clak import ListViewMixin, Parser
class App(ListViewMixin, Parser):
class Meta:
view_cli_options = True # or False, or ("columns", "add_index")
def cli_run(self, **_):
return [{"name": "ada", "role": "admin"}]
See the Views guide for usage, options, and override rules.
clak.comp.views
View mixins for automatic CLI rendering and view options.
Every view option is declared once, as a field of a typed settings class
(:mod:clak.views.settings), and exposed here as a :class:ViewOption (flag,
help, CLI parser, Meta.view_<name> normalizer). A concrete mixin only
names its view (view = ListView): its options and Meta.cli_view are
derived from view.settings_class. The --flag Argument and the
meta__config__view_<name> declaration are generated from them, and
settings collection iterates the same registry. Point view at your own
ClakView subclass to get a mixin for it.
Option layers (mirror ClakView hierarchy):
- ClakViewOptMixin — generic (no CLI flags)
- TableViewOptMixin — table (Show/List):
width,columns,add_index,format,sort_columns,sort_mode,wrap,yaml_cells - ListViewMixin — list-only:
expand_keys - TextLayoutOptMixin — text wrap:
line_length - TextViewOptMixin — text (Markdown/Rst):
format(view/raw) plusline_length - PprintViewMixin / RawViewMixin —
line_lengthonly - DataViewMixin — structured dump:
format,compact,color,anchors - CompositeViewMixin — table opts +
expand_keys+format_scope+line_length(return aCompositeView; no autocli_view)
Example:
class App(ListViewMixin, Parser):
class Meta:
view_cli_options = True # or False, or ("columns", "add_index")
view_columns = ("name", "role")
view_column_names = ("name", "role", "city")
view_sort_columns = 1
view_format = "view"
view_width = "fit"
view_wrap = "last"
def cli_run(self, **_):
return [{"name": "a"}, {"name": "b"}]
ClakViewOptMixin
Bases: _ViewMixinBase
Layer 1: generic ClakView (no CLI flags).
TableViewOptMixin
TextLayoutOptMixin
TextViewOptMixin
ShowViewMixin
Bases: TableViewOptMixin
Auto-render command results with :class:~clak.views.ShowView.
Adds --columns, --add-index / --no-add-index,
--format, --sort-columns, --sort-mode, --width,
and --wrap.
Configure exposed flags with Meta.view_cli_options.
ListViewMixin
Bases: TableViewOptMixin
Auto-render command results with :class:~clak.views.ListView.
Adds --columns, --add-index / --no-add-index,
--expand-keys / --no-expand-keys, --format,
--sort-columns, --sort-mode, --width, and --wrap.
Configure exposed flags with Meta.view_cli_options.
PprintViewMixin
Bases: TextLayoutOptMixin
Auto-render command results with :class:~clak.views.PprintView.
Adds --line-length. Configure exposed flags with Meta.view_cli_options.
DataViewMixin
Bases: _ViewMixinBase
Auto-render command results with :class:~clak.views.DataView.
Adds --format (json / yaml), --compact / --no-compact,
--color / --no-color, and --anchors / --no-anchors.
Syntax theme: Meta.view_syntax_theme or CLAK_SYNTAX_THEME, else
ansi_dark. Configure exposed flags with Meta.view_cli_options.
RawViewMixin
Bases: TextLayoutOptMixin
Auto-render command results with :class:~clak.views.RawView.
Adds --line-length. Configure exposed flags with Meta.view_cli_options.
MarkdownViewMixin
Bases: TextViewOptMixin
Auto-render command results with :class:~clak.views.MarkdownView.
Adds --format (view / raw) and --line-length.
Syntax theme: Meta.view_syntax_theme or CLAK_SYNTAX_THEME, else
ansi_dark. Configure exposed flags with Meta.view_cli_options.
RstViewMixin
Bases: TextViewOptMixin
Auto-render command results with :class:~clak.views.RstView.
Adds --format (view / raw) and --line-length.
Configure exposed flags with Meta.view_cli_options.
CompositeViewMixin
Bases: TextLayoutOptMixin, TableViewOptMixin
CLI flags for multi-section :class:~clak.views.CompositeView output.
Adds table options, --expand-keys, --format-scope, --width,
and --line-length. Does not set Meta.cli_view: return a
CompositeView(...) from cli_run. Table flags apply to the primary
section only. --line-length applies to text/pprint sections only.
--expand-keys is for a ListView primary; hide it with
Meta.view_cli_options when the primary is ShowView.
--format is table-scoped (view / yaml / json / csv);
markdown source is in --format-scope all envelopes, not --format raw.
clak.views
View classes for command line output formatting.
Public names are the view classes and a few format/width constants.
Helpers (parse_columns, resolve_view_width, dump_yaml, ...) live in
the submodules (clak.views.table, clak.views.base, ...).
OUTPUT_FORMATS = frozenset({'view', 'yaml', 'json', 'csv'})
module-attribute
TEXT_FORMATS = frozenset({'view', 'raw'})
module-attribute
DATA_FORMATS = frozenset({'json', 'yaml'})
module-attribute
FORMAT_SCOPES = frozenset({'first', 'all'})
module-attribute
WIDTH_MODES = frozenset({'content', 'fit', 'terminal'})
module-attribute
WRAP_MODES = frozenset({'last', 'all', 'first'})
module-attribute
DEFAULT_LINE_LENGTH = 120
module-attribute
ShowView
ListView
Bases: TableView
Render list data.
column_scan is a developer knob (class attribute, not a CLI setting):
union (default) takes columns from every row, first is the legacy
first-row-only behavior.
PprintView
DataView
RawView
MarkdownView
Bases: _MarkupView
Render markdown text (or raw source with format=raw)
RstView
Bases: _MarkupView
Render reStructuredText (or raw source with format=raw)
CompositeView
Bases: ClakView
Render ordered sections (tables and/or text) as one CLI output.
Human view mode prints sections separated by a blank line and equalizes
table outer widths. Machine formats honor format_scope:
first(default): export only the primary sectionall: export a structured envelope of every section
ClakView
Render command line output.
Subclasses implement :meth:to_text. Settings are layered, last wins:
settings_default < constructor kwargs < call kwargs; keys listed in
instance_settings keep a non-empty constructor value over call kwargs.
Three entry points share that one code path:
- :meth:
to_stringreturns the text (no side effect); - :meth:
emitprints it and returns it; - :meth:
renderis the historical form:stdout=True(default) emits,stdout=Falseonly returns.
A subclass sets settings_class (a :class:~clak.views.settings.ViewSettings
dataclass): settings_default is then derived from it (unless declared)
and the merged settings are validated/normalized before to_text.
effective_settings(**settings)
Layered, validated settings for one render (see class docstring).
emit(*args, **settings)
Print the rendered text and return it.
merge_settings(existing=None, cli_settings=None)
staticmethod
Merge CLI view settings over existing view settings.
CLI values win. When CLI overrides a non-None existing value, log at INFO.
render(*args, stdout=True, **settings)
Render args[0] (default: the constructor payload) as text.
Prints unless stdout=False; always returns the text. Prefer
:meth:emit / :meth:to_string in new code.
section_csv(settings, *, primary)
Text of this section in a csv envelope.
section_export(settings, *, primary)
Data of this section in a structured (envelope) export.
section_settings(settings, *, primary, fmt=None)
Render kwargs for this view as a composite section.
to_string(*args, **settings)
The rendered text, without printing.
to_text(payload, settings)
Build the output text (settings is a fresh dict, safe to pop).