Documentation
¶
Index ¶
- Variables
- type Command
- type CommandHandler
- type ExitError
- type Handler
- func (h *Handler) Close() error
- func (h *Handler) Cursor() (term.Coordinates, term.CursorStyle, bool)
- func (h *Handler) Draw(w term.Writer)
- func (h *Handler) Handle(ev term.Event) (exit, handled bool)
- func (h *Handler) Resize(width, height int)
- func (h *Handler) Selection() (string, bool)
- func (h *Handler) Wait()
- type Option
- func WithAttributes(attr term.Attributes) Option
- func WithErrorAttributes(attr term.Attributes) Option
- func WithExitError(err error) Option
- func WithPrompt(prompt string) Option
- func WithRunningAnimationFrames(frames []string, sequence []int) Option
- func WithStorage(key string, s storageapi.Service) Option
- func WithSuccessAttributes(attr term.Attributes) Option
- func WithTabStyle(style inputbox.TabStyle) Option
- func WithValidCommandAttributes(attr term.Attributes) Option
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("command not found")
ErrNotFound is returned by a CommandHandler to indicate it does not handle the given command.
Functions ¶
This section is empty.
Types ¶
type CommandHandler ¶
type CommandHandler interface {
// HandleCommand is called when the user submits
// a command. It returns an iterator of responsive
// components to display as output.
HandleCommand(ctx context.Context, cmd Command) (
iterator.Iterator[component.Responsive], error,
)
// Complete returns tab completion candidates for
// the given command and arguments.
Complete(ctx context.Context, cmd string, args []string) (
iterator.Iterator[string], error,
)
}
CommandHandler dispatches commands and provides completions.
func FuncCommandHandler ¶
func FuncCommandHandler( fn func(context.Context, Command) (iterator.Iterator[component.Responsive], error), completer func(context.Context, string, []string) (iterator.Iterator[string], error), ) CommandHandler
FuncCommandHandler returns a CommandHandler that calls fn every time HandleCommand is invoked and completer when Complete is invoked.
func NopCommandCompleter ¶
func NopCommandCompleter( fn func(context.Context, Command) (iterator.Iterator[component.Responsive], error), ) CommandHandler
NopCommandCompleter returns a CommandHandler that calls fn every time HandleCommand is invoked, but does not have a completion function.
type ExitError ¶ added in v0.0.27
type ExitError struct{ Code int }
ExitError represents a non-zero command exit code.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a read-eval-print loop handler that owns an output container (top) and an inputbox handler (bottom). It dispatches commands asynchronously via a CommandHandler and streams output into the container.
func New ¶
func New( cmd CommandHandler, scheduleNextTick func(func()) bool, interrupter term.Interrupter, opts ...Option, ) *Handler
New creates a new Handler with the given command handler and options. The scheduleNextTick function schedules a callback on the next event-loop tick. The interrupter forces a redraw of the event loop.
func (*Handler) Close ¶ added in v0.0.48
Close cancels all in-flight commands and animations, then waits for goroutines to finish.
func (*Handler) Cursor ¶
func (h *Handler) Cursor() (term.Coordinates, term.CursorStyle, bool)
Cursor satisfies tui.Handler.
type Option ¶
type Option func(*Handler)
Option configures a Handler.
func WithAttributes ¶
func WithAttributes(attr term.Attributes) Option
WithAttributes sets the display attributes.
func WithErrorAttributes ¶ added in v0.0.27
func WithErrorAttributes(attr term.Attributes) Option
WithErrorAttributes sets the prompt prefix color on command failure and the color of error messages. Default: red foreground.
func WithExitError ¶ added in v0.0.39
WithExitError configures an error that, when returned by HandleCommand (detected via errors.Is), causes the REPL to exit on the next call to Handle.
func WithPrompt ¶
WithPrompt sets the prompt string displayed before each input line.
func WithRunningAnimationFrames ¶ added in v0.0.27
WithRunningAnimationFrames sets the spinner animation shown while a command is running. Default: component.ProgressAnimationFrames().
func WithStorage ¶
func WithStorage(key string, s storageapi.Service) Option
WithStorage sets the backing store for command history. Defaults to storagestub.NewInMemoryService().
func WithSuccessAttributes ¶ added in v0.0.27
func WithSuccessAttributes(attr term.Attributes) Option
WithSuccessAttributes sets the prompt prefix color on command success. Default: green foreground.
func WithTabStyle ¶
WithTabStyle sets the tab completion style.
func WithValidCommandAttributes ¶ added in v0.0.27
func WithValidCommandAttributes(attr term.Attributes) Option
WithValidCommandAttributes sets inputbox attributes when the typed command is a valid executable. Default: base attributes with bold.