prompt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 9 Imported by: 52

README

Prompt

User-friendly interactive prompts for Go.

Action Status Go Reference

Features

  1. input lets the user enter a string using the terminal ui.
  2. toggle lets the user choose one of several strings using the terminal ui (Usually used for yes or no choices).
  3. choose lets the user choose one of several strings using the terminal ui.
  4. multichoose lets the user choose multiple strings from multiple strings using the terminal ui.

Usage

Input

example

screenshot-input

InputWithLimit can limit the type of input:

// Only integers can be entered
val, err = p.Ask("Input example (Only Integer):").InputWithLimit("", prompt.InputInteger)

// Only numbers (integers and decimals) can be entered
val, err = p.Ask("Input example (Only Number):").InputWithLimit("", prompt.InputNumber)

Toggle

example

screenshot-toggle

Choose

example

screenshot-choose

MultiChoose

example

screenshot-multichoose

Show help message

Prompt.SetHelpVisible(true) displays the help message for key bindings.

val, err := prompt.New().Ask("Choose value:").SetHelpVisible(true).
	Choose([]string{"Item 1", "Item 2", "Item 3"})

screenshot-help

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrModelConversion = errors.New("model conversion failed")
	ErrUserQuit        = errors.New("user quit prompt")
)
View Source
var (
	DefaultNormalPromptPrefix      = "?"
	DefaultFinishPromptPrefix      = "✔"
	DefaultNormalPromptSuffix      = "›"
	DefaultFinishPromptSuffix      = "…"
	DefaultNormalPromptPrefixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
	DefaultFinishPromptPrefixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
	DefaultNormalPromptSuffixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))
	DefaultFinishPromptSuffixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))

	DefaultItemStyle         = lipgloss.NewStyle()
	DefaultSelectedItemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
	DefaultChoiceStyle       = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
)

Functions

This section is empty.

Types

type ChooseModel

type ChooseModel struct {
	ListHandler
	// contains filtered or unexported fields
}

func NewChooseModel

func NewChooseModel(choices []string) *ChooseModel

func NewChooseModelWithStyle

func NewChooseModelWithStyle(choices []string, style *ListStyle) *ChooseModel

func (ChooseModel) Data

func (m ChooseModel) Data() any

func (ChooseModel) DataString

func (m ChooseModel) DataString() string

func (ChooseModel) Init

func (m ChooseModel) Init() tea.Cmd

func (ChooseModel) KeyBindings

func (m ChooseModel) KeyBindings() []key.Binding

func (ChooseModel) Update

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

func (ChooseModel) View

func (m ChooseModel) View() string

type InputLimit

type InputLimit int
const (
	InputAll InputLimit = iota
	InputInteger
	InputNumber
)

type InputModel

type InputModel struct {
	ItemStyle         lipgloss.Style
	SelectedItemStyle lipgloss.Style
	ChoiceStyle       lipgloss.Style
	// contains filtered or unexported fields
}

func NewInputModel

func NewInputModel(defaultValue string) *InputModel

func (InputModel) Data

func (m InputModel) Data() any

func (InputModel) DataString

func (m InputModel) DataString() string

func (InputModel) Init

func (m InputModel) Init() tea.Cmd

func (InputModel) KeyBindings

func (m InputModel) KeyBindings() []key.Binding

func (*InputModel) SetInputLimit

func (m *InputModel) SetInputLimit(inputLimit InputLimit) *InputModel

func (InputModel) Update

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

func (InputModel) View

func (m InputModel) View() string

type ListHandler

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

func NewListHandler

func NewListHandler(choiceCount int, style *ListStyle) *ListHandler

func (ListHandler) Cursor

func (h ListHandler) Cursor() int

func (*ListHandler) MoveNext

func (h *ListHandler) MoveNext()

func (*ListHandler) MovePrev

func (h *ListHandler) MovePrev()

func (ListHandler) Style

func (h ListHandler) Style() *ListStyle

type ListStyle

type ListStyle struct {
	ItemStyle         lipgloss.Style
	SelectedItemStyle lipgloss.Style
	ChoiceStyle       lipgloss.Style
}

func NewListStyle

func NewListStyle() *ListStyle

type MultiChooseModel

type MultiChooseModel struct {
	ListHandler
	// contains filtered or unexported fields
}

func NewMultiChooseModel

func NewMultiChooseModel(choices []string) *MultiChooseModel

func NewMultiChooseModelWithStyle

func NewMultiChooseModelWithStyle(choices []string, style *ListStyle) *MultiChooseModel

func (MultiChooseModel) Data

func (m MultiChooseModel) Data() any

func (MultiChooseModel) DataString

func (m MultiChooseModel) DataString() string

func (MultiChooseModel) Init

func (m MultiChooseModel) Init() tea.Cmd

func (MultiChooseModel) KeyBindings

func (m MultiChooseModel) KeyBindings() []key.Binding

func (MultiChooseModel) Update

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

func (MultiChooseModel) View

func (m MultiChooseModel) View() string

type Prompt

type Prompt struct {

	// Style
	Message           string
	NormalPrefix      string
	FinishPrefix      string
	NormalSuffix      string
	FinishSuffix      string
	PrefixStyle       lipgloss.Style
	FinishPrefixStyle lipgloss.Style
	SuffixStyle       lipgloss.Style
	FinishSuffixStyle lipgloss.Style
	// contains filtered or unexported fields
}

func New

func New() *Prompt

New returns a default style *Prompt.

func (*Prompt) Ask

func (p *Prompt) Ask(message string) *Prompt

Ask set prompt message

func (Prompt) Choose

func (p Prompt) Choose(choices []string) (string, error)

func (Prompt) ChooseWithStyle

func (p Prompt) ChooseWithStyle(choices []string, style *ListStyle) (string, error)

func (*Prompt) Error

func (p *Prompt) Error() error

func (Prompt) Init

func (m Prompt) Init() tea.Cmd

func (Prompt) Input

func (p Prompt) Input(defaultValue string) (string, error)

func (Prompt) InputWithLimit

func (p Prompt) InputWithLimit(defaultValue string, inputLimit InputLimit) (string, error)

func (Prompt) Model

func (p Prompt) Model() PromptModel

func (Prompt) MultiChoose

func (p Prompt) MultiChoose(choices []string) ([]string, error)

func (Prompt) MultiChooseWithStyle

func (p Prompt) MultiChooseWithStyle(choices []string, style *ListStyle) ([]string, error)

func (*Prompt) Run

func (p *Prompt) Run() (PromptModel, error)

func (*Prompt) SetHelpVisible

func (p *Prompt) SetHelpVisible(visible bool) *Prompt

func (*Prompt) SetModel

func (p *Prompt) SetModel(pm PromptModel) *Prompt

func (Prompt) Toggle

func (p Prompt) Toggle(choices []string) (string, error)

func (Prompt) ToggleWithStyle

func (p Prompt) ToggleWithStyle(choices []string, style *ListStyle) (string, error)

func (Prompt) Update

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

func (Prompt) View

func (p Prompt) View() string

type PromptModel

type PromptModel interface {
	tea.Model
	Data() any
	DataString() string
	KeyBindings() []key.Binding
}

type ToggleModel

type ToggleModel struct {
	ListHandler
	// contains filtered or unexported fields
}

func NewToggleModel

func NewToggleModel(choices []string) *ToggleModel

func NewToggleModelWithStyle

func NewToggleModelWithStyle(choices []string, style *ListStyle) *ToggleModel

func (ToggleModel) Data

func (m ToggleModel) Data() any

func (ToggleModel) DataString

func (m ToggleModel) DataString() string

func (ToggleModel) Init

func (m ToggleModel) Init() tea.Cmd

func (ToggleModel) KeyBindings

func (m ToggleModel) KeyBindings() []key.Binding

func (ToggleModel) Update

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

func (ToggleModel) View

func (m ToggleModel) View() string

Directories

Path Synopsis
choose command
input command
multichoose command
toggle command

Jump to

Keyboard shortcuts

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