choose

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 6 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultKeyMap = KeyMap{
		Help: key.NewBinding(
			key.WithKeys("?"),
			key.WithHelp("?", "toggle help"),
		),
		Confirm: key.NewBinding(
			key.WithKeys("enter"),
			key.WithHelp("enter", "confirm"),
		),
		Quit: key.NewBinding(
			key.WithKeys("q", "esc", "ctrl+c"),
			key.WithHelp("q", "quit"),
		),
		Prev: key.NewBinding(
			key.WithKeys("up", "k"),
			key.WithHelp("↑/k", "move up"),
		),
		Next: key.NewBinding(
			key.WithKeys("down", "j", "tab"),
			key.WithHelp("↓/j/tab", "move down"),
		),
	}

	HorizontalKeyMap = KeyMap{
		Help: key.NewBinding(
			key.WithKeys("?"),
			key.WithHelp("?", "toggle help"),
		),
		Confirm: key.NewBinding(
			key.WithKeys("enter"),
			key.WithHelp("enter", "confirm"),
		),
		Quit: key.NewBinding(
			key.WithKeys("q", "esc", "ctrl+c"),
			key.WithHelp("q", "quit"),
		),
		Prev: key.NewBinding(
			key.WithKeys("left", "h"),
			key.WithHelp("←/h", "move left"),
		),
		Next: key.NewBinding(
			key.WithKeys("right", "l", "tab", " "),
			key.WithHelp("→/l/tab/space", "move right"),
		),
	}
)
View Source
var ThemeArrow = func(choices []Choice, cursor int) string {
	s := strings.Builder{}
	s.WriteString("\n")

	maxLen := 0
	for _, choice := range choices {
		if maxLen < len(choice.Text) {
			maxLen = len(choice.Text)
		}
	}

	for i := 0; i < len(choices); i++ {
		text := choices[i].Text
		note := choices[i].Note
		if note != "" {
			note = strings.Repeat(" ", maxLen-len(text)+2) + constants.DefaultNoteStyle.Render(note)
		}
		choice := text + note

		if cursor == i {
			s.WriteString(constants.DefaultSelectedItemStyle.Render(("❯ " + choice)))
		} else {
			s.WriteString(constants.DefaultItemStyle.Render(fmt.Sprintf("  " + choice)))
		}
		s.WriteString("\n")
	}

	return s.String()
}
View Source
var ThemeDefault = func(choices []Choice, cursor int) string {
	s := strings.Builder{}
	s.WriteString("\n")

	maxLen := 0
	for _, choice := range choices {
		if maxLen < len(choice.Text) {
			maxLen = len(choice.Text)
		}
	}

	for i := 0; i < len(choices); i++ {
		text := choices[i].Text
		note := choices[i].Note
		if note != "" {
			note = strings.Repeat(" ", maxLen-len(text)+2) + constants.DefaultNoteStyle.Render(note)
		}
		choice := text + note

		if cursor == i {
			s.WriteString(constants.DefaultSelectedItemStyle.Render("• " + choice))
		} else {
			s.WriteString(constants.DefaultItemStyle.Render(fmt.Sprintf("  " + choice)))
		}
		s.WriteString("\n")
	}

	return s.String()
}
View Source
var ThemeLine = func(choices []Choice, cursor int) string {
	s := strings.Builder{}

	result := make([]string, len(choices))
	for index, choice := range choices {
		if index == cursor {
			result[index] = constants.DefaultSelectedItemStyle.Render(choice.Text)
		} else {
			result[index] = constants.DefaultItemStyle.Render(choice.Text)
		}
	}
	s.WriteString(strings.Join(result, " / "))
	s.WriteString("\n")

	return s.String()
}

Functions

This section is empty.

Types

type Choice added in v0.9.0

type Choice struct {
	Text string
	Note string
}

type KeyMap added in v0.8.1

type KeyMap struct {
	Prev    key.Binding
	Next    key.Binding
	Help    key.Binding
	Confirm key.Binding
	Quit    key.Binding
}

func (KeyMap) FullHelp added in v0.8.1

func (k KeyMap) FullHelp() [][]key.Binding

func (KeyMap) ShortHelp added in v0.8.1

func (k KeyMap) ShortHelp() []key.Binding

type Model

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

func New

func New(choices []Choice, opts ...Option) *Model

func NewWithStrings added in v0.9.0

func NewWithStrings(choices []string, opts ...Option) *Model

func (Model) Data

func (m Model) Data() string

func (Model) DataString

func (m Model) DataString() string

func (Model) Error added in v0.8.0

func (m Model) Error() error

func (Model) Init

func (m Model) Init() tea.Cmd

func (Model) Quitting added in v0.8.0

func (m Model) Quitting() bool

func (Model) TeaProgramOpts added in v0.8.4

func (m Model) TeaProgramOpts() []tea.ProgramOption

func (Model) Update

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

func (Model) View

func (m Model) View() string

type Option

type Option func(*Model)

func WithHelp added in v0.8.0

func WithHelp(show bool) Option

func WithKeyMap added in v0.8.1

func WithKeyMap(keyMap KeyMap) Option

func WithTeaProgramOpts added in v0.8.4

func WithTeaProgramOpts(opts ...tea.ProgramOption) Option

func WithTheme

func WithTheme(theme Theme) Option

type Theme

type Theme func([]Choice, int) string

Jump to

Keyboard shortcuts

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