tui

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package tui provides interactive terminal user interface components. It encapsulates TUI library specifics and provides clean interfaces for testing.

Index

Constants

This section is empty.

Variables

View Source
var ErrCanceled = fmt.Errorf("operation canceled by user")

ErrCanceled is returned when the user cancels the operation.

Functions

func IsInteractive

func IsInteractive() bool

IsInteractive determines if the current environment supports interactive prompts. It returns false in the following cases:

  • stdout is not a terminal (redirected to file, pipe, etc.)
  • running in a CI/CD environment (detected via environment variables)

This function is used to automatically skip TUI prompts in non-interactive contexts.

func IsTTY

func IsTTY() bool

IsTTY checks if stdout is a terminal. This is a lower-level check than IsInteractive.

func QuickSpinner

func QuickSpinner(title string, action func()) error

QuickSpinner runs a simple spinner with the given title and action. This is a convenience function for simple use cases.

func QuickSpinnerWithErr

func QuickSpinnerWithErr(ctx context.Context, title string, action func(context.Context) error) error

QuickSpinnerWithErr runs a spinner with context and error support.

Types

type Choice

type Choice int

Choice represents the initial selection choice.

const (
	// ChoiceAll indicates user wants to apply to all modules.
	ChoiceAll Choice = iota

	// ChoiceSelect indicates user wants to select specific modules.
	ChoiceSelect

	// ChoiceCancel indicates user wants to cancel the operation.
	ChoiceCancel
)

func ParseChoice

func ParseChoice(s string) Choice

ParseChoice converts a string to a Choice.

func (Choice) String

func (c Choice) String() string

String returns a string representation of the choice.

type MockCall

type MockCall struct {
	Method  string
	Modules []*workspace.Module
	Message string
}

MockCall records a method invocation on MockPrompter.

type MockPrompter

type MockPrompter struct {

	// SelectionResult is the pre-configured selection to return.
	SelectionResult Selection

	// SelectionError is the error to return from PromptModuleSelection.
	SelectionError error

	// ConfirmResult is the result to return from ConfirmOperation.
	ConfirmResult bool

	// ConfirmError is the error to return from ConfirmOperation.
	ConfirmError error

	// Calls records all method invocations for assertion.
	Calls []MockCall
	// contains filtered or unexported fields
}

MockPrompter is a mock implementation of Prompter for testing. It allows tests to control the selection results without requiring user interaction.

func NewMockPrompter

func NewMockPrompter() *MockPrompter

NewMockPrompter creates a new MockPrompter.

func (*MockPrompter) CallCount

func (m *MockPrompter) CallCount(method string) int

CallCount returns the number of times a method was called.

func (*MockPrompter) ConfirmOperation

func (m *MockPrompter) ConfirmOperation(message string) (bool, error)

ConfirmOperation returns the pre-configured ConfirmResult.

func (*MockPrompter) LastCall

func (m *MockPrompter) LastCall(method string) *MockCall

LastCall returns the most recent call to the specified method. Returns nil if the method was never called.

func (*MockPrompter) PromptModuleSelection

func (m *MockPrompter) PromptModuleSelection(modules []*workspace.Module) (Selection, error)

PromptModuleSelection returns the pre-configured SelectionResult.

func (*MockPrompter) Reset

func (m *MockPrompter) Reset()

Reset clears all recorded calls and resets state.

func (*MockPrompter) WithConfirmError

func (m *MockPrompter) WithConfirmError(err error) *MockPrompter

WithConfirmError sets the confirm error and returns the mock for chaining.

func (*MockPrompter) WithConfirmResult

func (m *MockPrompter) WithConfirmResult(result bool) *MockPrompter

WithConfirmResult sets the confirm result and returns the mock for chaining.

func (*MockPrompter) WithSelectionError

func (m *MockPrompter) WithSelectionError(err error) *MockPrompter

WithSelectionError sets the selection error and returns the mock for chaining.

func (*MockPrompter) WithSelectionResult

func (m *MockPrompter) WithSelectionResult(result Selection) *MockPrompter

WithSelectionResult sets the selection result and returns the mock for chaining.

type ModulePrompt

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

ModulePrompt implements the Prompter interface using charmbracelet/huh. It provides an interactive TUI for module selection in multi-module workspaces.

func NewModulePrompt

func NewModulePrompt(modules []*workspace.Module) *ModulePrompt

NewModulePrompt creates a new ModulePrompt with the given modules.

func (*ModulePrompt) ConfirmOperation

func (p *ModulePrompt) ConfirmOperation(message string) (bool, error)

ConfirmOperation asks for yes/no confirmation.

func (*ModulePrompt) PromptModuleSelection

func (p *ModulePrompt) PromptModuleSelection(modules []*workspace.Module) (Selection, error)

PromptModuleSelection shows an interactive module selection UI. First, it presents a choice: apply to all, select specific, or cancel. If "select specific" is chosen, it shows a multi-select checkbox interface.

type MultiModuleSpinner

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

MultiModuleSpinner shows progress for multi-module operations.

func NewMultiModuleSpinner

func NewMultiModuleSpinner(total int, accessible bool) *MultiModuleSpinner

NewMultiModuleSpinner creates a spinner for multi-module operations.

func (*MultiModuleSpinner) RunEach

func (m *MultiModuleSpinner) RunEach(
	ctx context.Context,
	moduleNames []string,
	action func(ctx context.Context, idx int) error,
) error

RunEach executes an action for each item with progress indication. The action receives the current index and should return an error if it fails.

type ProgressReporter

type ProgressReporter interface {
	// Start begins showing progress.
	Start(title string)

	// Update updates the current progress state.
	Update(current, total int, currentModule string)

	// Finish completes the progress display.
	Finish()
}

ProgressReporter provides a way to update progress during multi-module operations.

type Prompter

type Prompter interface {
	// PromptModuleSelection shows interactive module selection UI.
	// Returns a Selection indicating user choices or an error.
	// Returns ErrCanceled if the user cancels the operation.
	PromptModuleSelection(modules []*workspace.Module) (Selection, error)

	// ConfirmOperation asks for yes/no confirmation.
	// Returns true if confirmed, false if declined.
	ConfirmOperation(message string) (bool, error)
}

Prompter abstracts interactive prompts for module selection. This interface enables dependency injection and testing with mock implementations.

type Selection

type Selection struct {
	// All indicates if the user selected "apply to all modules".
	All bool

	// Modules contains the names of specific modules selected.
	// Empty if All is true or if operation was canceled.
	Modules []string

	// Canceled indicates if the user canceled the selection.
	Canceled bool
}

Selection represents the user's module selection from the TUI prompt.

func AllModules

func AllModules() Selection

AllModules returns a Selection with All set to true.

func CanceledSelection

func CanceledSelection() Selection

CanceledSelection returns a Selection marked as canceled.

func SelectedModules

func SelectedModules(names []string) Selection

SelectedModules returns a Selection with specific module names.

type Spinner

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

Spinner provides progress indication for long-running operations. It wraps charmbracelet/huh/spinner for a consistent interface.

func NewSpinner

func NewSpinner(opts ...SpinnerOption) *Spinner

NewSpinner creates a new Spinner with the given options.

func (*Spinner) Run

func (s *Spinner) Run(action func()) error

Run executes the given action with a spinner displayed. The action is run in a separate goroutine while the spinner animates. Returns any error from the action.

func (*Spinner) RunWithErr

func (s *Spinner) RunWithErr(ctx context.Context, action func(context.Context) error) error

RunWithErr executes the given action with context and error support. Returns any error from the action or context cancellation.

type SpinnerOption

type SpinnerOption func(*Spinner)

SpinnerOption configures a Spinner.

func WithAccessibleMode

func WithAccessibleMode(accessible bool) SpinnerOption

WithAccessibleMode enables accessible mode (no animations).

func WithSpinnerTitle

func WithSpinnerTitle(title string) SpinnerOption

WithSpinnerTitle sets the spinner's title message.

func WithSpinnerType

func WithSpinnerType(t SpinnerType) SpinnerOption

WithSpinnerType sets the spinner's animation style.

type SpinnerType

type SpinnerType int

SpinnerType represents different spinner animation styles.

const (
	// SpinnerLine uses line-based animation.
	SpinnerLine SpinnerType = iota
	// SpinnerDots uses dots animation.
	SpinnerDots
	// SpinnerMiniDot uses mini dot animation.
	SpinnerMiniDot
	// SpinnerPulse uses pulse animation.
	SpinnerPulse
)

Jump to

Keyboard shortcuts

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