Skip to content

Commit

Permalink
close rwxrob#212
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxrob committed Nov 15, 2024
1 parent 5d57b5e commit d832147
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions bonzai.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"slices"
"strconv"
"strings"
"sync"
"text/template"
"unicode"

Expand All @@ -27,40 +28,14 @@ func init() {
}
}

// Completer specifies anything with Complete function based
// on the remaining arguments. The Complete method must never panic
// and always return at least an empty slice of strings. For completing
// data from a [Cmd] use [CmdCompleter] instead. Implementations must
// not examine anything from the command line itself depending entirely
// on the passed arguments instead (which are usually the remaining
// arguments from the command line). Implementations may and will often
// depend on external data sources to determine the possible completion
// values, for example, current host names, users, or data from a web
// API endpoint.
type Completer interface {
Complete(args ...string) []string
}

// CmdCompleter is a specialized [Completer] that requires a [Cmd]. This
// is used for the following core completions:
//
// - [pkg/github.com/rwxrob/bonzai/comp.Cmds]
// - [pkg/github.com/rwxrob/bonzai/comp.Aliases]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsAliases]
// - [pkg/github.com/rwxrob/bonzai/comp.Opts]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsOpts]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsOptsAliases]
type CmdCompleter interface {
Completer
Cmd() *Cmd
SetCmd(x *Cmd)
}

type Cmd struct {
Name string // ex: delete (required)
Alias string // ex: rm|d|del (optional)
Opts string // ex: mon|wed|fri (optional)

// Shareable variables also used for persistent initial values
Vars map[string]Var

// Own work (optional if Cmds or Def)
Do func(x *Cmd, args ...string) error

Expand Down Expand Up @@ -90,6 +65,48 @@ type Cmd struct {
cmdAlias map[string]*Cmd // see [cacheCmdAlias]
}

// Var contains information to be shared between [Cmd] instances and
// contains a [sync.Mutex] allowing safe-for-concurrency modification
// when needed. The Str
type Var struct {
sync.Mutex
Key string // same as that used in Vars (map[string]Var)
Short string // short description of variable
Str string
Int int
Bool bool
Any any
}

// Completer specifies anything with Complete function based
// on the remaining arguments. The Complete method must never panic
// and always return at least an empty slice of strings. For completing
// data from a [Cmd] use [CmdCompleter] instead. Implementations must
// not examine anything from the command line itself depending entirely
// on the passed arguments instead (which are usually the remaining
// arguments from the command line). Implementations may and will often
// depend on external data sources to determine the possible completion
// values, for example, current host names, users, or data from a web
// API endpoint.
type Completer interface {
Complete(args ...string) []string
}

// CmdCompleter is a specialized [Completer] that requires a [Cmd]. This
// is used for the following core completions:
//
// - [pkg/github.com/rwxrob/bonzai/comp.Cmds]
// - [pkg/github.com/rwxrob/bonzai/comp.Aliases]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsAliases]
// - [pkg/github.com/rwxrob/bonzai/comp.Opts]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsOpts]
// - [pkg/github.com/rwxrob/bonzai/comp.CmdsOptsAliases]
type CmdCompleter interface {
Completer
Cmd() *Cmd
SetCmd(x *Cmd)
}

// Caller returns the internal reference to the parent/caller of this
// command. It is not set until [Cmd.Seek] is called or indirectly by
// [Cmd.Run] or [Cmd.Exec]. Caller is set to itself if there is no
Expand Down

0 comments on commit d832147

Please sign in to comment.