Documentation
¶
Overview ¶
Package forms provides keys.Action-driven forms and confirm dialogs for viewkit terminal UIs.
Index ¶
- func Bool(vals map[string]any, key string) bool
- func Int(vals map[string]any, key string) int
- func Match(vals []string, prefix string) []string
- func Raw(vals map[string]any, key string) string
- func SelectFirst(options []string, cur string) []string
- func SelectIndex(options []string, want string) int
- func Str(vals map[string]any, key string) string
- func Strings(vals map[string]any, key string) []string
- type Confirm
- type Field
- type FieldKind
- type Form
- func (fm *Form) AcceptSuggestion() bool
- func (fm *Form) CycleSuggestion(delta int)
- func (fm *Form) Focused() *Field
- func (fm *Form) Handle(a keys.Action) bool
- func (fm *Form) Insert(s string)
- func (fm *Form) Overlay(bg string, f layout.Frame, title string, pos ...layout.OverlayPos) string
- func (fm *Form) Render(f layout.Frame, title string) string
- func (fm *Form) RenderWindow(f layout.Frame, title string, maxLines int) string
- func (fm *Form) Suggestions() []string
- func (fm *Form) Values() map[string]any
- type Result
- type Suggester
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶ added in v0.1.6
Bool returns the boolean value stored under key. Missing keys and non-bool values yield false.
func Int ¶ added in v0.1.6
Int returns the value stored under key as an integer. Text fields hold strings, so the common case parses base-10 text, but already-numeric values are accepted too. Missing keys and unparsable text yield 0.
func Match ¶ added in v0.1.9
Match filters vals down to those extending prefix, case-insensitively. An exact match is dropped: there is nothing left to complete. Results are deduplicated and sorted so the candidate list does not reorder as the vocabulary is rebuilt.
func Raw ¶ added in v0.1.9
Raw returns the string value stored under key verbatim. Seeding a rebuilt form uses it rather than Str, so a trailing delimiter or space the user deliberately typed survives the rebuild.
func SelectFirst ¶ added in v0.1.6
SelectFirst returns options reordered so cur leads, for select fields that should open on their current value. The relative order of the remaining options is preserved, and duplicates of cur beyond the first are kept. The result is always a fresh slice; options is never mutated.
func SelectIndex ¶ added in v0.1.6
SelectIndex returns the position of want in options, for seeding Field.Selected. Unknown and empty values select the first option.
Types ¶
type Field ¶
type Field struct {
Key string
Label string
Kind FieldKind
Options []string
Text string
On bool
Selected int
Checked map[int]bool
Secret bool
// Suggest proposes completions for the token being typed. Text and
// multiline fields only; a nil Suggester disables completion.
Suggest Suggester
// Delim splits the field into independently completed tokens, e.g. ","
// for a comma-separated list or " " for a search expression. Empty means
// the whole text is one token.
Delim string
}
type Form ¶
type Form struct {
Fields []Field
// contains filtered or unexported fields
}
Example ¶
package main
import (
"github.com/codyconfer/viewkit/forms"
"github.com/codyconfer/viewkit/keys"
"github.com/codyconfer/viewkit/layout"
)
func main() {
fm := forms.NewForm(
forms.Field{Key: "name", Label: "Name", Kind: forms.FieldText},
forms.Field{
Key: "risk",
Label: "Risk",
Kind: forms.FieldSelect,
Options: []string{"low", "med", "high"},
},
forms.Field{Key: "auto", Label: "Auto", Kind: forms.FieldToggle},
)
fm.Insert("example")
fm.Handle(keys.Down)
fm.Handle(keys.Right)
fm.Handle(keys.Down)
fm.Handle(keys.Confirm)
_ = fm.Values()
_ = fm.Render(layout.NewFrame(60), "SETTINGS")
}
Output:
func (*Form) AcceptSuggestion ¶ added in v0.1.9
AcceptSuggestion writes the active candidate into the focused field, replacing the token being typed. It reports false when there is nothing to accept, which lets a host give the accept key a second meaning.
func (*Form) CycleSuggestion ¶ added in v0.1.9
CycleSuggestion moves through the candidate list. It wraps at both ends rather than clamping like field navigation does: a short list is meant to be cycled repeatedly, and stopping at the last entry hides the first.
func (*Form) RenderWindow ¶ added in v0.1.6
RenderWindow renders at most maxLines of field rows, scrolled to keep the focused field visible. A maxLines of zero renders every field.
func (*Form) Suggestions ¶ added in v0.1.9
Suggestions lists the completions offered for the focused field, most relevant first. It is empty when the field has no Suggester or nothing matches what has been typed.
type Suggester ¶ added in v0.1.9
Suggester proposes completions for the token a text field is currently building. It receives that token — not the whole field — so a delimited field can complete each entry independently.