repl

package
v0.0.101 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 20 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, pw ProgressWriter) (
		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

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) Close added in v0.0.48

func (h *Handler) Close() error

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.

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) LayoutHeights added in v0.0.67

func (h *Handler) LayoutHeights() (rlH, outH int)

LayoutHeights computes the inputbox and output layout heights The first return value is the height allocated to the inputbox (rl), and the second is the height allocated to previous command output.

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) Text added in v0.0.69

func (h *Handler) Text() string

Text returns the current text in the inputbox, i.e. whatever the user has typed at the current prompt but not yet submitted.

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 WithMaxHistory added in v0.0.66

func WithMaxHistory(max int) Option

WithMaxHistory sets the maximum number of command history entries retained in memory and persisted in storage.

func WithProgressBarCharSet added in v0.0.58

func WithProgressBarCharSet(chars component.ProgressBarCharSet) Option

WithProgressBarCharSet sets the characters used to render the progress bar. Default: component.DefaultProgressBarCharSet().

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.

type ProgressWriter added in v0.0.58

type ProgressWriter interface {
	Progress(progress, total int64, units string)
}

ProgressWriter allows command handlers to report progress for long-running operations.

func NopProgressWriter added in v0.0.58

func NopProgressWriter() ProgressWriter

NopProgressWriter returns a ProgressWriter that discards all progress updates.

Jump to

Keyboard shortcuts

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