prompt

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 12 Imported by: 52

README

Prompt

User-friendly interactive prompts for Go.
Based on Bubble Tea. Inspired by Prompts

Action Status Codecov Go Reference Go Reference Git tag Go Version

Table of Contents

Features

  1. choose lets the user choose one of several strings using the terminal ui.
  2. multichoose lets the user choose multiple strings from multiple strings using the terminal ui.
  3. input lets the user enter a string using the terminal ui. You can specify that only numbers or integers are allowed.
  4. Show help message for keymaps.
  5. Based on Bubble Tea. prompt.Prompt and all child models implement tea.Model.

Screenshots

Choose

View screenshots

Theme Default

example

choose

Theme Arrow

example

choose-theme-arrow

Theme Line

example

choose-theme-line

MultiChoose

View screenshots

Theme Default

example

multichoose

Theme Dot

example

multichoose-theme-dot

Input

View screenshots

example

screenshot-input

Password input

example

screenshot-input-echo-password

Password input like linux (do not display any characters)

example

screenshot-input-echo-none

Only integers can be entered

example

Only numbers can be entered

example

Input with validation

example

screenshot-input-with-validation

TextArea

View screenshots

example

screenshot-textarea

Toggle

View screenshots

Deprecated: use Choose([]string{}, WithTheme(ChooseThemeLine)) instead. Preview.

Show help message

View screenshots

All components support displaying help message for shortcut keys at the bottom.

choose-with-help

Examples:

  1. Choose with help
  2. MultiChoose with help
  3. Input with help
  4. TextArea with help
  5. Toggle with help

screenshot-help

License

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"))
	DefaultErrorPromptPrefixStyle  = lipgloss.NewStyle().Foreground(lipgloss.Color("1"))
	DefaultNormalPromptSuffixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))
	DefaultFinishPromptSuffixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))
	DefaultNoteStyle               = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
		Light: "#909090",
		Dark:  "#626262",
	})

	DefaultItemStyle         = lipgloss.NewStyle()
	DefaultSelectedItemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
	DefaultChoiceStyle       = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
)
View Source
var WithChooseTheme = choose.WithTheme

Functions

This section is empty.

Types

type EchoMode added in v0.5.0

type EchoMode = textinput.EchoMode
const (
	// EchoNormal displays text as is. This is the default behavior.
	EchoNormal EchoMode = textinput.EchoNormal

	// EchoPassword displays the EchoCharacter mask instead of actual
	// characters.  This is commonly used for password fields.
	EchoPassword EchoMode = textinput.EchoPassword

	// EchoNone displays nothing as characters are entered. This is commonly
	// seen for password fields on the command line.
	EchoNone EchoMode = textinput.EchoNone
)

type InputMode added in v0.5.0

type InputMode int
const (
	InputAll     InputMode = iota // allow any input.
	InputInteger                  // only integers can be entered.
	InputNumber                   // only integers and decimals can be entered.
)

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 deprecated

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

Deprecated: use InputModel.WithInputMode instead.

func (InputModel) Update

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

func (InputModel) UseKeyEnter added in v0.4.0

func (m InputModel) UseKeyEnter() bool

func (InputModel) UseKeyQ added in v0.3.1

func (m InputModel) UseKeyQ() bool

func (InputModel) View

func (m InputModel) View() string

func (*InputModel) WithEchoMode added in v0.5.0

func (m *InputModel) WithEchoMode(mode EchoMode) *InputModel

func (*InputModel) WithInputMode added in v0.5.0

func (m *InputModel) WithInputMode(mode InputMode) *InputModel

func (*InputModel) WithValidateFunc added in v0.5.0

func (m *InputModel) WithValidateFunc(vf ValidateFunc) *InputModel

type InputOption added in v0.5.0

type InputOption func(*InputModel)

func WithEchoMode added in v0.5.0

func WithEchoMode(mode EchoMode) InputOption

func WithInputMode added in v0.5.0

func WithInputMode(mode InputMode) InputOption

func WithValidateFunc added in v0.5.0

func WithValidateFunc(vf ValidateFunc) InputOption

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 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 *Prompt using the default style.

func (*Prompt) Ask

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

Ask set prompt message

func (Prompt) Choose

func (p Prompt) Choose(choices []string, opts ...choose.Option) (string, error)

Choose lets the user choose one of the given choices.

func (Prompt) Init

func (p Prompt) Init() tea.Cmd

func (Prompt) Input

func (p Prompt) Input(defaultValue string, opts ...InputOption) (string, error)

Input asks the user to enter a string.

func (Prompt) InputWithLimit deprecated

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

Deprecated: use InputModel.Input("", prompt.WithInputMode()) instead. Input asks the user to enter a string. It restricts the types of characters the user can enter.

func (Prompt) MultiChoose

func (p Prompt) MultiChoose(choices []string, opts ...multichoose.Option) ([]string, error)

MultiChoose lets the user choose multiples from the given choices.

func (*Prompt) Run

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

Run runs the program using the given model, blocking until the user chooses or exits.

func (*Prompt) SetHelpVisible deprecated

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

Deprecated: use Prompt.WithHelp instead. SetHelpVisible sets whether the help of the keymap is visible

func (*Prompt) SetModel

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

SetModel sets the model used by the prompt. In most cases you won't need to use this.

func (Prompt) TextArea added in v0.4.0

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

func (Prompt) Toggle

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

Toggle lets the user choose one of the given choices. Appearance uses the default style.

func (Prompt) ToggleWithStyle

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

ToggleWithStyle lets the user choose one of the given choices. Appearance uses the given style.

func (Prompt) Update

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

func (Prompt) View

func (p Prompt) View() string

func (*Prompt) WithHelp added in v0.4.0

func (p *Prompt) WithHelp(enable bool) *Prompt

WithHelp sets whether the help of the keymap is visible

func (*Prompt) WithProgramOptions added in v0.4.0

func (p *Prompt) WithProgramOptions(opts ...tea.ProgramOption) *Prompt

WithProgramOptions sets the `tea.ProgramOption` passed when calling `tea.NewProgram`. This function is mainly used for testing, usually you don't need to use this function.

func (*Prompt) WithTestView added in v0.5.1

func (p *Prompt) WithTestView(initView *string, finalView *string) *Prompt

type PromptModel

type PromptModel interface {
	tea.Model
	Data() any          // Returns the final result.
	DataString() string // Returns a string for display in the result position.
	KeyBindings() []key.Binding
	UseKeyQ() bool
	UseKeyEnter() bool
}

type TextAreaModel added in v0.4.0

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

func NewTextAreaModel added in v0.4.0

func NewTextAreaModel(defaultValue string) *TextAreaModel

func (TextAreaModel) Data added in v0.4.0

func (m TextAreaModel) Data() any

func (TextAreaModel) DataString added in v0.4.0

func (m TextAreaModel) DataString() string

func (TextAreaModel) Init added in v0.4.0

func (m TextAreaModel) Init() tea.Cmd

func (TextAreaModel) KeyBindings added in v0.4.0

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

func (TextAreaModel) Update added in v0.4.0

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

func (TextAreaModel) UseKeyEnter added in v0.4.0

func (m TextAreaModel) UseKeyEnter() bool

func (TextAreaModel) UseKeyQ added in v0.4.0

func (m TextAreaModel) UseKeyQ() bool

func (TextAreaModel) View added in v0.4.0

func (m TextAreaModel) View() string

type ToggleModel deprecated

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

Deprecated: use Choose([]string{}, WithTheme(ChooseThemeLine)) instead.

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) UseKeyEnter added in v0.4.0

func (m ToggleModel) UseKeyEnter() bool

func (ToggleModel) UseKeyQ added in v0.3.1

func (m ToggleModel) UseKeyQ() bool

func (ToggleModel) View

func (m ToggleModel) View() string

type ValidateFunc added in v0.5.0

type ValidateFunc func(string) error

Directories

Path Synopsis
examples
choose command
input command
input-echo-none command
input-with-help command
multichoose command
textarea command
toggle command

Jump to

Keyboard shortcuts

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