cmdmodel

package
v0.4.1-0...-582f678 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MsgMissingArgFuncErr = `` /* 185-byte string literal not displayed */

	MsgIsCmdItselfErr = `` /* 697-byte string literal not displayed */

	MsgUsingAliasInCmdPathErr = `` /* 580-byte string literal not displayed */

	MsgDuplicateAliasErr = `` /* 134-byte string literal not displayed */

	MsgInvalidHomeCmdPathErr = `` /* 236-byte string literal not displayed */

)
View Source
const (
	ArgNone = ArgType(iota)
	ArgOptional
	ArgRequired
)
View Source
const (
	LOW = StatusSeverity(iota)
	MED
	HIGH
)

Variables

View Source
var (
	ErrNotCommand = fmt.Errorf("unrecognized command")
	// Command is valid, but entered incorrectly
	ErrIncompleteCmd   = fmt.Errorf("improper command entry")
	ErrMissingArgument = fmt.Errorf("missing argument")
	ErrEmptyCommand    = fmt.Errorf("empty command")
	// Command is missing an execution path
	ErrUnimplementedCmd       = fmt.Errorf("unimplemented command")
	ErrMisconfiguredArgParser = fmt.Errorf("command arg parsing is misconfigured")
)

Functions

func GetArgAs

func GetArgAs[T, E any](m *Base[E]) (T, bool)

GetArgAs will try to cast a command argument to the specified type. Returns the argument of the command, cast to the specified type, along with true to indicate type-cast success. False will be returned if the type-case fails.

🔴 Failed type-casts are assumed to be catastrophic and will propagate an arg-type-error to the base view.

🟠 Retrieving an argument from a command that does not support arguments, is considered catastrophic and will propagate an error to the base view.

🟡 False will be returned for nil arguments, without error propagation; optional arguments can be nil.

Types

type ArgType

type ArgType int

type Base

type Base[T any] struct {
	// contains filtered or unexported fields
}

func NewBaseModel

func NewBaseModel[T any](cmdData CommandData[T]) *Base[T]

func (*Base[T]) AddArgTypeError

func (m *Base[T]) AddArgTypeError(arg any, expectedType string)

func (*Base[T]) AddError

func (m *Base[T]) AddError(err error)

func (*Base[T]) Exec

func (m *Base[T]) Exec(model T) T

Exec executes the current command path in the context of the passed model. All detected errors are logged and stored.

func (Base[T]) GetAliases

func (m Base[T]) GetAliases() []string

func (Base[T]) GetCmdPaths

func (m Base[T]) GetCmdPaths() (paths []string)

func (Base[T]) GetId

func (m Base[T]) GetId() int

func (Base[T]) GetName

func (m Base[T]) GetName() string

func (Base[T]) GetStatus

func (m Base[T]) GetStatus() Status

func (Base[T]) GetViewSize

func (m Base[T]) GetViewSize() (int, int)

func (Base[T]) IsActivePath

func (m Base[T]) IsActivePath(cmdPath string) bool

func (Base[T]) IsInitialized

func (m Base[T]) IsInitialized() bool

IsInitialized checks to make sure that various expected values are set.

func (Base[T]) ParseCommand

func (m Base[T]) ParseCommand(cmdInput string) Status

func (*Base[T]) SetStatus

func (m *Base[T]) SetStatus(s Status)

func (*Base[T]) Update

func (bc *Base[T]) Update(model T, msg tea.Msg) (T, tea.Cmd)

func (*Base[T]) View

func (bc *Base[T]) View(model T) string

type Command

type Command[T any] struct {
	// A list of words that execute a specific
	// command function, when entered into the
	// command input. Empty paths refer to
	// the command alias itself as a command.
	Path string

	// Executes the logic of the command, which
	// updates the command model.
	Run func(T) T

	// The main display function for the command.
	View func(T) string

	// Hides the command input, which relinquishes
	// keyboard control to the command. This is
	// necessary for commands which control the
	// UI using the keyboard.
	CaptureInput bool

	// The type of arguments that the command
	// requires. ArgNone is the default.
	//
	//	ArgNone
	//	ArgOptional
	//	ArgRequired
	ArgType ArgType

	// Parses the argument passed to the command.
	// It should return a user-readable error
	// if it fails to parse.
	//
	// This function is required if the arg type
	// is NOT ArgNone.
	ParseArg func(arg string) (any, error)
	// contains filtered or unexported fields
}

type CommandData

type CommandData[T any] struct {
	Name     string
	Aliases  []string
	Commands []Command[T]
}

type HomeMsg

type HomeMsg struct{}

type InputModel

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

func NewInputModel

func NewInputModel(h *utils.InputHistory, homePath string, cmdModels ...Interface) InputModel

TODO - Use an interface to define input history methods

func (InputModel) Init

func (m InputModel) Init() tea.Cmd

func (InputModel) Update

func (m InputModel) Update(msg tea.Msg) (InputModel, tea.Cmd)

func (InputModel) View

func (m InputModel) View() string

type InputOption

type InputOption func(*InputModel)

type Interface

type Interface interface {
	Update(tea.Msg) (Interface, tea.Cmd)
	View() string
	GetName() string
	GetAliases() []string
	GetId() int
	GetCmdPaths() []string
	ParseCommand(string) Status
	GetStatus() Status
	SetStatus(Status)
	IsInitialized() bool
}

type Status

type Status struct {
	Suggestions  []string
	Path         string
	Arg          any
	Error        error
	IsCommand    bool
	CaptureInput bool
}

type StatusBarMsg

type StatusBarMsg struct {
	String   string
	Severity StatusSeverity
}

type StatusSeverity

type StatusSeverity int

type UpdateCmdMsg

type UpdateCmdMsg struct {
	Model Interface
}

type ViewportSizeMsg

type ViewportSizeMsg struct {
	Width  int
	Height int
}

type WindowSizeMsg

type WindowSizeMsg struct {
	Width  int
	Height int
}

Jump to

Keyboard shortcuts

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