listfmt

package
v0.22.39 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

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.

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