repl

package
v0.0.39 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Command

type Command struct {
	Name string
	Args []string
}

Command represents a user-issued command.

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

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.

func (*ExitError) Error added in v0.0.27

func (e *ExitError) Error() string

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) Cursor

func (h *Handler) Cursor() (term.Coordinates, term.CursorStyle, bool)

Cursor satisfies tui.Handler.

func (*Handler) Draw

func (h *Handler) Draw(w term.Writer)

Draw satisfies tui.Component.

func (*Handler) Handle

func (h *Handler) Handle(ev term.Event) (exit, handled bool)

Handle satisfies tui.Handler.

func (*Handler) Resize

func (h *Handler) Resize(width, height int)

Resize satisfies tui.Component.

func (*Handler) Selection

func (h *Handler) Selection() (string, bool)

Selection satisfies tui.Handler.

func (*Handler) Wait

func (h *Handler) Wait()

Wait blocks until all dispatched command goroutines have finished.

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

func WithExitError(err error) Option

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

func WithPrompt(prompt string) Option

WithPrompt sets the prompt string displayed before each input line.

func WithRunningAnimationFrames added in v0.0.27

func WithRunningAnimationFrames(frames []string, sequence []int) Option

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

func WithTabStyle(style inputbox.TabStyle) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL