command

package
v0.2.2098 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// System.
	ErrReaderFailedRead = errors.New("command reader failed to read")
	ErrMakeTermRaw      = errors.New("failed to create raw terminal")

	// Prompt.
	ErrCancelled        = errors.New("cancelled by user")
	ErrInterrupted      = errors.New("interrupted by user")
	ErrResponseRequired = errors.New("response is required")
)

Functions

func NewCommand

func NewCommand(config ...Config) *command

NewCommand returns command instance.

Types

type CommandWizard

type CommandWizard struct {
	// contains filtered or unexported fields
}

CommandWizard extends command with wizard functionality.

func NewCommandWizard

func NewCommandWizard(config ...any) *CommandWizard

NewCommandWizard creates a new command wizard instance.

func (*CommandWizard) AddChoice

func (w *CommandWizard) AddChoice(id, question string, options []Option, required ...bool) *CommandWizard

AddChoice adds a choice step.

func (*CommandWizard) AddConditionalProcess

func (w *CommandWizard) AddConditionalProcess(
	id, initialMessage string,
	processFn func(results WizardResults, progress func(string)) (any, error),
	condition func(WizardResults) bool,
) *CommandWizard

AddConditionalProcess adds a conditional processing step.

func (*CommandWizard) AddConditionalStep

func (w *CommandWizard) AddConditionalStep(step WizardStep, condition func(results WizardResults) bool) *CommandWizard

AddConditionalStep adds a step with condition.

func (*CommandWizard) AddConfirm

func (w *CommandWizard) AddConfirm(id, question string, defaultValue ...bool) *CommandWizard

AddConfirm adds a confirm step.

func (*CommandWizard) AddInfo

func (w *CommandWizard) AddInfo(id, message string) *CommandWizard

AddInfo adds an info step (display only).

func (*CommandWizard) AddMultiChoice

func (w *CommandWizard) AddMultiChoice(id, question string, options []Option, required ...bool) *CommandWizard

AddMultiChoice adds a multi-choice step.

func (*CommandWizard) AddProcess

func (w *CommandWizard) AddProcess(
	id, initialMessage string,
	processFn func(results WizardResults, progress func(string)) (any, error),
) *CommandWizard

AddProcess adds a processing step for long-running operations.

func (*CommandWizard) AddPrompt

func (w *CommandWizard) AddPrompt(id, question string, required ...bool) *CommandWizard

AddPrompt adds a prompt step.

func (*CommandWizard) AddPromptWithDefault

func (w *CommandWizard) AddPromptWithDefault(id, question string, defaultValue string) *CommandWizard

AddPromptWithDefault adds a prompt with default value step.

func (*CommandWizard) AddStep

func (w *CommandWizard) AddStep(step WizardStep) *CommandWizard

AddStep adds a step to the wizard.

func (CommandWizard) Choice

func (c CommandWizard) Choice(question string, options []Option, required ...bool) (*Output[Option], error)

Choice displays the single choice prompt and returns the `*Output` otherwise `error`.

func (CommandWizard) Confirm

func (c CommandWizard) Confirm(question string) (*Output[bool], error)

func (CommandWizard) ConfirmWithDefault

func (c CommandWizard) ConfirmWithDefault(question string, defaultValue bool) (*Output[bool], error)

func (*CommandWizard) GetBoolResult

func (w *CommandWizard) GetBoolResult(id string) (bool, bool)

GetBoolResult gets a boolean result by ID.

func (*CommandWizard) GetMultiOptionResult

func (w *CommandWizard) GetMultiOptionResult(id string) ([]Option, bool)

GetMultiOptionResult gets a []Option result by ID.

func (*CommandWizard) GetOptionResult

func (w *CommandWizard) GetOptionResult(id string) (Option, bool)

GetOptionResult gets an Option result by ID.

func (*CommandWizard) GetResult

func (w *CommandWizard) GetResult(id string) (any, bool)

GetResult gets a specific result by ID.

func (*CommandWizard) GetStringResult

func (w *CommandWizard) GetStringResult(id string) (string, bool)

GetStringResult gets a string result by ID.

func (CommandWizard) MultiChoice

func (c CommandWizard) MultiChoice(question string, options []Option, required ...bool) (*Output[[]Option], error)

MultiChoice displays the multiple choice prompt and returns the `*Output` otherwise `error`.

func (*CommandWizard) PrintSummary

func (w *CommandWizard) PrintSummary()

PrintSummary prints a summary of all wizard results.

func (CommandWizard) Prompt

func (c CommandWizard) Prompt(question string, required ...bool) (*Output[string], error)

Prompt displays prompt command and return `string` otherwise `error`

func (CommandWizard) PromptWithDefault

func (c CommandWizard) PromptWithDefault(question string, defaultValue string) (*Output[string], error)

PromptWithDefault displays prompt command with default value and return `string`.

func (*CommandWizard) Run

func (w *CommandWizard) Run() (WizardResults, error)

Run executes the wizard.

type Config

type Config struct {
	QuestionMark  string
	PrintResponse bool
}

Config defines command configuration.

type Option

type Option struct {
	Key   any
	Value string
}

Option defines option argument for command choice.

type Output

type Output[T result] struct {
	// contains filtered or unexported fields
}

Output defines command output.

func (*Output[T]) PrintResponse

func (o *Output[T]) PrintResponse()

PrintResponse prints command output.

func (*Output[T]) Value

func (o *Output[T]) Value() T

Value returns value of command output.

func (*Output[T]) WithContext

func (o *Output[T]) WithContext(previousOutputs ...outputInterface) *Output[T]

WithContext adds context into the command output.

type ResultColor

type ResultColor uint8

ResultColor defines available colors for result text.

const (
	ColorDefault   ResultColor = iota // Auto-detect or default.
	ColorLightGray                    // Light gray (\033[37m).
	ColorWhite                        // White (\033[97m).
	ColorCyan                         // Cyan (\033[96m).
	ColorLightBlue                    // Light blue (\033[94m).
	ColorYellow                       // Yellow (\033[93m).
	ColorGreen                        // Green (\033[92m).
)

type StepType

type StepType int

StepType defines the type of wizard step.

const (
	StepChoice StepType = iota
	StepMultiChoice
	StepPrompt
	StepConfirm
	StepInfo    // For displaying information only.
	StepProcess // For long-running operations.
)

type WizardConfig

type WizardConfig struct {
	Title         string
	Description   string
	ShowProgress  bool
	ClearScreen   bool
	ClearHistory  bool // Clear terminal scroll history.
	ResultColor   ResultColor
	ShowFinish    bool   // Show finish screen.
	FinishMessage string // Custom finish message.
}

WizardConfig configuration for wizard.

type WizardResults

type WizardResults map[string]any

WizardResults holds all results from wizard steps.

type WizardStep

type WizardStep struct {
	ID       string
	Type     StepType
	Question string
	Options  []Option
	Required bool
	Default  any // For default values.

	// Conditional execution.
	Condition func(results WizardResults) bool

	// Custom validation.
	Validate func(value any) error

	// Transform/modify result before storing.
	Transform func(value any) any

	// Process function for long-running operations.
	Process func(results WizardResults, progress func(string)) (any, error)
}

WizardStep defines a single step in the wizard.

Jump to

Keyboard shortcuts

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