package command import ( "errors" "strconv" ) // ErrInvalidArguments reports arguments that do not have the exact command format. var ErrInvalidArguments = errors.New("invalid command arguments") // ParsePositiveUint64 accepts exactly one base-10 positive decimal value. // It deliberately rejects signs, whitespace, zero, and non-canonical forms. func ParsePositiveUint64(args []string) (uint64, error) { if len(args) != 1 || args[0] == "" || (len(args[0]) > 1 && args[0][0] == '0') { return 0, ErrInvalidArguments } for _, r := range args[0] { if r < '0' || r > '9' { return 0, ErrInvalidArguments } } n, err := strconv.ParseUint(args[0], 10, 64) if err != nil || n < 1 { return 0, ErrInvalidArguments } return n, nil } // ParsePositiveDecimal parses a positive decimal that must fit the platform int. func ParsePositiveDecimal(args []string) (int, error) { n, err := ParsePositiveUint64(args) if err != nil || n > uint64(^uint(0)>>1) { return 0, ErrInvalidArguments } return int(n), nil } // Context is the set of application actions available to commands. type Context interface { CreateTab() error CreateSession() error CreateSessionNamed(name string) error CreateEphemeralSession() error CloseTab() error SplitRight() error SplitLeft() error SplitUp() error SplitDown() error ConsumeOrExpelPaneLeft() error ConsumeOrExpelPaneRight() error StackPane() error ToggleStack() error ToggleFloatingPane() error ClosePane() error OpenMovePanePicker() error OpenMoveTabPicker() error FocusPaneLeft() error FocusPaneRight() error FocusPaneUp() error FocusPaneDown() error EnterResizeMode() error GrowPaneWidth() error ShrinkPaneWidth() error GrowPaneHeight() error ShrinkPaneHeight() error EqualizePanes() error NextTab() error PrevTab() error BackSession() error Detach() error EnterVisualMode() error RenameSession() error RenameSessionTo(name string) error RenameTab() error RenameTabTo(name string) error OpenSessionPicker() error OpenNotifications() error YankLastNotification() error JumpRecentSession(rank int) error } // Arguments declares whether a command accepts palette arguments. type Arguments uint8 const ( ArgumentsNone Arguments = iota ArgumentsOptional ArgumentsRequired ) // ContextHint describes optional contextual data needed to execute a command. type ContextHint uint8 const ( ContextHintNone ContextHint = iota ContextHintRecentSessions ) // TargetKind declares which resolved target a control command needs. type TargetKind uint8 const ( TargetNone TargetKind = iota TargetSession TargetTab TargetPane ) // CommandScope declares the serialization boundary needed by a command. type CommandScope uint8 const ( CommandScopeSession CommandScope = iota // CommandScopeCrossSession requires dispatch to defer locking to the // mutation, which resolves and locks both source and destination sessions. CommandScopeCrossSession ) // ControlOptions carries per-invocation output preferences. type ControlOptions struct { JSON bool } // ControlResult carries a control command's output. type ControlResult struct { Output string } // ControlContext exposes prompt-free daemon actions to control commands. type ControlContext interface { CreateTab() error CreateSessionNamed(name string) error CloseTab() error ClosePane() error MovePane(destinationSession, destinationTabID string) error MoveTab(destinationSession string) error SplitRight() error SplitLeft() error SplitUp() error SplitDown() error ConsumeOrExpelPaneLeft() error ConsumeOrExpelPaneRight() error StackPane() error ToggleStack() error GrowPaneWidth() error ShrinkPaneWidth() error GrowPaneHeight() error ShrinkPaneHeight() error EqualizePanes() error FocusPaneLeft() error FocusPaneRight() error FocusPaneUp() error FocusPaneDown() error NextTab() error PrevTab() error RenameSessionTo(name string) error RenameTabTo(name string) error Toast(severity, message string) error SessionRecovery(action string) (string, error) ListSessions(json bool) (string, error) ListTabs(json bool) (string, error) ListPanes(json bool) (string, error) SessionEnvironment(json bool) (string, error) RemoteCatalog(json bool) (string, error) } // Command describes an executable command. type Command struct { Slug, Code, Name, Desc string Usage string Arguments Arguments // Preview describes the selected palette command while its exact token is // being entered. hasArgument is true after whitespace following the token. Preview func(args []string, hasArgument bool) string ContextHint ContextHint Scriptable bool PaletteVisible bool Target TargetKind Scope CommandScope Run func(Context, []string) error Control func(ControlContext, []string, ControlOptions) (ControlResult, error) }