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.
Option layers (mirror ClakView hierarchy):
- ClakViewOptMixin — generic (no CLI flags)
- TableViewOptMixin — table (Show/List):
width,format,columns,sort_columns,sort_mode,wrap,add_index - 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, merge_view_settings, require_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
PprintView
DataView
Bases: ClakView
Render any payload as structured JSON or YAML.
render(*args, stdout=True, **kwargs)
Render data
RawView
MarkdownView
Bases: ClakView
Render markdown text (or raw source with format=raw)
render(*args, stdout=True, **kwargs)
Render data
RstView
Bases: ClakView
Render reStructuredText (or raw source with format=raw)
render(*args, stdout=True, **kwargs)
Render data
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
render(*args, stdout=True, **kwargs)
Render composite sections
ClakView
Render command line output
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.