listfmt

package
v0.22.171 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package listfmt — small renderer used by every `clawtool * list` subcommand (bridges, agents, sources, recipes, sandboxes, portals, hooks, …). Repowire pattern: each list command accepts `--format json|tsv|table` (default: table) and the renderer outputs in the requested shape so shell pipes get machine-readable rows without needing `awk` to peel a human-formatted table.

Usage:

listfmt.Render(stdout, "table", listfmt.Cols{
    Header: []string{"FAMILY", "STATUS", "DESCRIPTION"},
    Rows:   [][]string{{"codex", "ready", "..."}, ...},
})

`format` is parsed once by the caller — either via listfmt.Parse(argv) which strips `--format X` from a flag slice, or by the caller's own arg parser. listfmt itself is pure rendering.

Index

Constants

View Source
const DefaultFormat = FormatTable

DefaultFormat is what every list command falls back to when no `--format` flag is given. Table is the right default for interactive shell use; pipes / scripts can opt into tsv or json.

Variables

This section is empty.

Functions

func IsKnown

func IsKnown(s string) bool

IsKnown reports whether s parses to a Format other than the fallback. Useful when the caller wants to reject `--format xml` with a usage error rather than silently degrading.

func Render

func Render(w io.Writer, format Format, cols Cols) error

Render writes cols to w in the requested format. Unknown format falls back to the table renderer with a stderr-quality warning — a typo in --format should still produce useful output, not a silent empty pipe.

func RenderOrHint added in v0.22.40

func RenderOrHint(w io.Writer, format Format, cols Cols, hint string) error

RenderOrHint codifies the empty-state contract every `clawtool * list` command discovered during the 18aed7e..012abc0 series: when there are no rows to render, table mode emits a human-readable hint (the operator running the command interactively gets an actionable next-step) while JSON / TSV consumers route through Render so pipelines see the structured empty shape (`[]\n` and a header line respectively). Pre-helper, every list command hand-rolled the same `if format == FormatTable { print hint; return } else { Render(empty rows) }` branch; the helper bakes the contract into one place so future list commands inherit it for free.

The hint may contain embedded newlines for multi-line pointers (`skill list` ships a two-line "no skills" + "try `skill new <name>`" pair). A trailing newline is appended automatically — callers should NOT include one themselves.

When cols.Rows is non-empty the hint is ignored and the function delegates to Render unconditionally — same shape as `Render` itself, callable on any list state without a pre-emptive `len(cols.Rows) == 0` guard at the call site.

Types

type Cols

type Cols struct {
	Header []string
	Rows   [][]string
}

Cols is a small column-row container the renderer takes. Header names should be UPPERCASE for table mode (matches existing clawtool list output convention) and stay UPPERCASE for tsv too — JSON mode lower-cases them to produce idiomatic keys.

type Format

type Format string

Format enumerates the supported output shapes.

const (
	FormatTable Format = "table" // human-readable, padded columns
	FormatTSV   Format = "tsv"   // tab-separated, no header padding — pipe-friendly
	FormatJSON  Format = "json"  // array of objects keyed by header
)

func ExtractFlag

func ExtractFlag(argv []string) (Format, []string, error)

ExtractFlag pulls `--format <value>` (or `--format=<value>`) out of argv and returns (format, residual argv, error). Empty argv → (DefaultFormat, argv, nil). Unknown value is preserved verbatim — the caller decides whether to error or degrade.

Repeated `--format` is allowed; the last one wins (matches most CLI conventions where late flags override early ones).

func ParseFormat

func ParseFormat(s string) Format

ParseFormat normalises a string into a known Format. Empty, unknown values, and the defaults all collapse to FormatTable. Callers that want to reject unknowns can compare against IsKnown() first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL