Documentation
¶
Overview ¶
Package spec defines the herdr-panel spec format: a TOML file describing a panel's stores, settings fields, lists, buttons and the commands behind them.
Load reads and validates a spec. Every problem is reported with its file:line:col, all at once, as an Errors value. A spec can also be built in Go and checked with Validate.
A spec runs commands, so a spec is code: see docs/security.md.
Index ¶
- Constants
- Variables
- func CheckInt(f *Field, s string) error
- func CheckText(f *Field, s string) error
- func EnvName(s string) string
- func ExpandArgv(argv []Template, v Values, dir string) ([]string, error)
- func ExpandPath(t Template, v Values, dir string) (string, error)
- func HostVersion() string
- func IsSpecError(err error) bool
- func JSONSchema() []byte
- func SelRef(name string) (list, column string)
- func ValidKeyName(k string) bool
- func Validate(s *Spec) error
- type Action
- type After
- type ChoiceItem
- type Column
- type Command
- type Dim
- type Error
- type Errors
- type Field
- type FieldType
- type Pos
- type SaveMode
- type Section
- type SectionKind
- type Size
- type Spec
- type Store
- type StoreKind
- type Template
- type Values
- type ValuesFunc
Constants ¶
const ( NSRow = "row" NSField = "field" NSCtx = "ctx" NSEnv = "env" // NSSel reads the selected row of another list: {sel.<list id>.<column>}. NSSel = "sel" )
Namespaces a placeholder may use.
const DefaultTimeout = 10 * time.Second
DefaultTimeout bounds a command whose spec gives none.
const MaxSize = 1 << 20
MaxSize bounds a spec file; a panel description is never this large.
const Version = 1
Version is the spec format version this package reads.
Variables ¶
var CtxNames = []string{
"workspace_id", "tab_id", "pane_id", "cwd",
"plugin_id", "config_dir", "state_dir", "spec_dir",
}
CtxNames are the names {ctx.*} accepts.
var ErrMissing = errors.New("no value")
ErrMissing is wrapped by Expand when a placeholder has no value.
var ReservedKeys = []string{
"?", "/", "q", "esc", "enter", "space", "tab", "shift+tab",
"up", "down", "left", "right", "pgup", "pgdown", "home", "end",
"delete", "backspace",
"ctrl+c", "ctrl+s", "ctrl+r",
}
ReservedKeys are bound by every panel and cannot be an action's shortcut. The vim aliases h, j, k and l are not reserved: a shortcut on one of them wins, and the arrow keys still move.
var StoreKinds = []StoreKind{EnvFile, TOMLFile}
StoreKinds are the kinds this version reads.
Functions ¶
func EnvName ¶
EnvName is a field id or row column as it appears in a command's environment: PANEL_FIELD_<EnvName(id)>, PANEL_ROW_<EnvName(column)>. Letters are upper-cased and anything other than a letter or digit becomes _.
func ExpandArgv ¶
ExpandArgv expands each element of a command on its own, and resolves a relative program ("./x", "bin/x") against dir. A bare name ("herdr") is left for PATH lookup.
func ExpandPath ¶
ExpandPath expands a path template, then a leading ~, then makes it absolute against dir.
A path that starts with {env.X} needs X to be non-empty: an empty one would turn "{env.X}/config.toml" into "/config.toml".
func HostVersion ¶
func HostVersion() string
HostVersion is the version of this module in the running binary: "v0.3.1", or "devel" for a build from a working tree or one whose go.mod replaces this module (a go.work or a replace directive builds from local source, whatever version the requirement names).
func IsSpecError ¶
IsSpecError reports whether err describes a problem in the spec itself, as opposed to failing to read it.
func JSONSchema ¶
func JSONSchema() []byte
JSONSchema returns a JSON Schema (draft 2020-12) for the spec format, for editors (taplo, VS Code's Even Better TOML) and agents. It describes shape and types; references between fields, stores and placeholders are checked by Validate, which a schema cannot express.
func ValidKeyName ¶
ValidKeyName reports whether k is a key as Bubble Tea names it: a printable character ("r", "R", "?"), a named key ("enter", "f5"), or either after ctrl+, alt+ or shift+. After ctrl+ or alt+ a letter is lower case, and shift+ never precedes a letter (shift+a arrives as A).
func Validate ¶
Validate checks a spec built in Go. It first fills in what the TOML decoder would: the format version, the save mode, a field's id and label from its key, bool values "true"/"false", command timeouts, after = refresh, the id column and column titles. It then checks the shapes the decoder checks (enums, bounds, non-empty commands) and everything Parse checks. It returns an Errors value listing every problem; calling it again changes nothing.
A bool Default is stored as written: with TrueValue "1", write Default "1", not "true". Unlike the TOML default, a Store built in Go creates its file only when Create is set.
Types ¶
type Action ¶
type Action struct {
Label string
Key string // a shortcut, as Bubble Tea names keys; "" for none
Run *Command
Confirm Template // a question asked in the panel before running; "" for none
After After
Pos Pos
}
Action is a button or a row action.
type After ¶
type After string
After says what a panel does when an action succeeds.
const ( AfterRefresh After = "refresh" // reload lists and stores AfterClose After = "close" // close the panel AfterNothing After = "none" // AfterCloseThenRun closes the panel first and then runs the command, // detached, once the panel has exited; its result is never shown. It is // for an action that opens something the open panel would be in the way // of, such as another popup. AfterCloseThenRun After = "close_then_run" )
The After values.
type ChoiceItem ¶
ChoiceItem is one value a choice field can take.
type Dim ¶
Dim is one popup dimension: a number of cells or a percentage of the terminal. The zero value means "not set".
type Errors ¶
type Errors []*Error
Errors is every problem found in one spec, in file order. Load and Validate return it rather than stopping at the first problem, so an editor or an agent can fix them all in one pass.
type Field ¶
type Field struct {
ID string // unique in the spec; {field.<ID>} refers to it
Store string
Key string
Type FieldType
Label string
Help string
Default string // the value shown when the store has none, as stored
Required bool // an empty value blocks Save
Choices []ChoiceItem
Min, Max *int64
Pattern string // a regular expression a text value must match in full
MaxLen int
Placeholder string
TrueValue string // what a bool stores; "true" by default
FalseValue string // "false" by default
Test *Command
Pos Pos
}
Field is one editable value in a store.
type Pos ¶
type Pos struct {
Line, Col int
}
Pos is a position in a spec file. Line and Col start at 1; the zero value means "no position", for errors about the file as a whole.
type Section ¶
type Section struct {
// Title may use {sel.*}, {ctx.*} and {env.*} placeholders (see
// ExpandTitle); {{ and }} are literal braces.
Title string
Kind SectionKind
// ID names a list section, so other sections can follow its selected row
// with {sel.<id>.<column>}. Only lists have one; it may be empty.
ID string
Fields []*Field // FieldsSection
Source *Command // ListSection and TextSection: prints rows or text
Every time.Duration
Columns []Column
IDColumn string
Empty string // shown when a list has no rows
RowActions []*Action
Buttons []*Action // ButtonsSection
Text string // TextSection, when there is no source
View string // ViewSection: the name the host supplies a View under
Pos Pos
}
Section is one titled part of the panel.
func (*Section) ExpandTitle ¶ added in v0.2.0
ExpandTitle is the section's title with its placeholders filled in. A value that is missing, such as {sel.*} while nothing is selected, shows as "…"; a title that is not a valid template shows as written.
type SectionKind ¶
type SectionKind string
SectionKind is what a section shows.
const ( FieldsSection SectionKind = "fields" ListSection SectionKind = "list" ButtonsSection SectionKind = "buttons" TextSection SectionKind = "text" // ViewSection is drawn by Go code the panel's host supplies under the // section's View name (see panel.Options.Views). Experimental. ViewSection SectionKind = "view" )
The section kinds.
type Size ¶
type Size struct {
Width, Height Dim
}
Size is the popup size an opener asks herdr for. herdr's own popups take either form.
type Spec ¶
type Spec struct {
File string // the path it was loaded from, or "" when built in Go
Dir string // relative paths and programs resolve against this
Version int
Requires string // a minimum herdr-panel version, ">=0.3"; "" for none
Title string
Size Size
Save SaveMode
Stores []*Store // in file order
Sections []*Section
// contains filtered or unexported fields
}
Spec is a validated panel description.
func Load ¶
Load reads, decodes and validates the spec at path. Its errors carry the path and the line and column of each problem.
func Parse ¶
Parse decodes and validates a spec held in memory. file names it in errors; dir is where relative paths and programs resolve.
type Store ¶
type Store struct {
Name string
Kind StoreKind
Path Template // expanded with {env.*} and {ctx.*}; relative to Dir
Create bool // create the file on first save when it does not exist
Apply *Command // run after a save that changed this store; may be nil
Pos Pos
}
Store is where field values live.
type StoreKind ¶
type StoreKind string
StoreKind names a store implementation.
const ( // EnvFile is a KEY=value file: comments, order, quoting and `export` // are kept. EnvFile StoreKind = "envfile" // TOMLFile is a TOML file whose top-level keys hold the fields: only // edited lines change, so comments, tables and order are kept. TOMLFile StoreKind = "tomlfile" )
The store kinds.
type Template ¶
type Template struct {
Raw string
// contains filtered or unexported fields
}
Template is a string with placeholders: {row.<column>}, {field.<id>}, {sel.<list>.<column>}, {ctx.<name>}, {env.<NAME>} and {env.<NAME>:-default}. {{ and }} stand for literal braces.
A template always expands to exactly one string. In an argument vector each element expands on its own, so a value with spaces or quotes stays one argument and can never become two, or become shell syntax.
func MustTemplate ¶
MustTemplate parses s and panics on a syntax error; for specs built in Go.
func ParseTemplate ¶
ParseTemplate parses s. It checks syntax only; which namespaces and names are allowed depends on where the template is used (see Validate).
func (Template) Placeholders ¶
Placeholders returns the namespace and name of each placeholder.