tui

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package tui provides shared TUI components for Spectr CLI interactive modes.

Index

Constants

View Source
const (
	ColorBorder    = "240"
	ColorHeader    = "99"
	ColorSelected  = "229"
	ColorHighlight = "57"
	ColorHelp      = "240"
)

Color constants used across TUI components

View Source
const (
	// DefaultTableHeight is the default number of visible rows.
	DefaultTableHeight = 10
)
View Source
const (
	// EllipsisMinLength is the minimum string length before
	// truncation adds ellipsis.
	EllipsisMinLength = 3
)

Variables

This section is empty.

Functions

func ApplyTableStyles

func ApplyTableStyles(t *table.Model)

ApplyTableStyles applies the default Spectr styling to a table.

func ChoiceStyle

func ChoiceStyle() lipgloss.Style

ChoiceStyle returns the style for unselected choices.

func CopyToClipboard

func CopyToClipboard(text string) error

CopyToClipboard copies text to clipboard using native or OSC 52.

func HelpStyle

func HelpStyle() lipgloss.Style

HelpStyle returns the style for help text.

func SelectedStyle

func SelectedStyle() lipgloss.Style

SelectedStyle returns the style for selected items.

func TitleStyle

func TitleStyle() lipgloss.Style

TitleStyle returns the style for titles.

func TruncateString

func TruncateString(s string, maxLen int) string

TruncateString truncates a string and adds ellipsis if needed.

Types

type Action

type Action struct {
	Key         string        // The key binding (e.g., "e", "a", "enter")
	Description string        // Human-readable description for help text
	Handler     ActionHandler // The function to call when key is pressed
}

Action represents a configurable key action for TablePicker.

type ActionHandler

type ActionHandler func(row table.Row) (tea.Cmd, *ActionResult)

ActionHandler is a function that handles a key action on a selected row. It receives the selected row and returns a tea.Cmd and an optional result.

type ActionResult

type ActionResult struct {
	// ID is the selected item ID (usually from first column).
	ID string

	// Quit indicates the TUI should exit.
	Quit bool

	// Cancelled indicates the user cancelled.
	Cancelled bool

	// Copied indicates an ID was copied to clipboard.
	Copied bool

	// ArchiveRequested indicates archive action was requested.
	ArchiveRequested bool

	// Error contains any error from the action.
	Error error

	// Custom allows actions to pass custom data.
	Custom any
}

ActionResult represents the result of an action.

type MenuConfig struct {
	// Title is the menu title.
	Title string

	// Choices are the menu options.
	Choices []string

	// SelectHandler is called when an option is selected.
	// Receives the index of the selected choice.
	SelectHandler func(index int) tea.Cmd
}

MenuConfig holds configuration for MenuPicker.

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

MenuPicker is a simple menu-based selector. It provides a vertical list of choices with navigation and selection support.

func NewMenuPicker

func NewMenuPicker(config MenuConfig) *MenuPicker

NewMenuPicker creates a new MenuPicker with the given configuration.

Selection behavior:

  • If config.SelectHandler is provided, it will be called on selection
  • If config.SelectHandler is nil, the menu will quit on selection (default)

This default allows callers to create simple menus without specifying a handler when they only need to retrieve the selected index via Run().

func (m *MenuPicker) Init() tea.Cmd

Init implements tea.Model.

func (m *MenuPicker) Quitting() bool

Quitting returns whether the menu was quit.

func (m *MenuPicker) Run() (int, error)

Run runs the MenuPicker and returns the selected index, or -1 if cancelled.

func (m *MenuPicker) Selected() int

Selected returns the index of the selected choice.

func (m *MenuPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update implements tea.Model.

func (m *MenuPicker) View() string

View implements tea.Model.

func (m *MenuPicker) WithSelectHandler(handler func(index int) (tea.Model, tea.Cmd)) *MenuPicker

WithSelectHandler sets the handler for menu selection. The handler receives the selected index and can return a new model to transition to.

type TableConfig

type TableConfig struct {
	// Columns defines the table columns.
	Columns []table.Column

	// Rows contains the table data.
	Rows []table.Row

	// Height is the visible height of the table.
	Height int

	// Actions is a map of key bindings to actions.
	Actions map[string]Action

	// HelpText is custom help text; if empty, generated from actions.
	HelpText string

	// ProjectPath is the project root for file operations.
	ProjectPath string

	// FooterExtra is additional text to show after help.
	FooterExtra string
}

TableConfig holds configuration for TablePicker.

type TablePicker

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

TablePicker is a configurable table-based item selector.

func NewTablePicker

func NewTablePicker(config TableConfig) *TablePicker

NewTablePicker creates a new TablePicker with the given configuration.

func (*TablePicker) Init

func (p *TablePicker) Init() tea.Cmd

Init implements tea.Model.

func (*TablePicker) Result

func (p *TablePicker) Result() *ActionResult

Result returns the action result after the picker has quit.

func (*TablePicker) Run

func (p *TablePicker) Run() (*ActionResult, error)

Run runs the TablePicker and returns the result.

func (*TablePicker) SetFooterExtra

func (p *TablePicker) SetFooterExtra(extra string)

SetFooterExtra sets additional footer text.

func (*TablePicker) SetRows

func (p *TablePicker) SetRows(rows []table.Row)

SetRows updates the table rows and regenerates help text.

func (*TablePicker) Table

func (p *TablePicker) Table() *table.Model

Table returns the underlying table model for advanced use cases.

func (*TablePicker) Update

func (p *TablePicker) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update implements tea.Model.

func (*TablePicker) UpdateHelpText

func (p *TablePicker) UpdateHelpText()

UpdateHelpText regenerates the help text with the current row count.

func (*TablePicker) View

func (p *TablePicker) View() string

View implements tea.Model.

func (*TablePicker) WithAction

func (p *TablePicker) WithAction(key, description string, handler ActionHandler) *TablePicker

WithAction adds a key action to the picker.

func (*TablePicker) WithProjectPath

func (p *TablePicker) WithProjectPath(path string) *TablePicker

WithProjectPath sets the project path for file operations.

Jump to

Keyboard shortcuts

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