prompt

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 10 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. input lets the user enter a string using the terminal ui. You can specify that only numbers or integers are allowed.
  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.
  5. Show help message for keymaps.
  6. Based on Bubble Tea. prompt.Prompt and all child models implement tea.Model.

Usage

Choose

Theme Default

example

screenshot-choose

Theme Arrow

example

screenshot-choose-theme-arrow

Theme Line

example

screenshot-choose-theme-horizontal

With Help Message

example

screenshot-choose-with-help

MultiChoose

example

screenshot-multichoose

Input

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

example

screenshot-textarea

Toggle

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

Show help message

Prompt.WithHelp(true) displays the help message for keymaps.

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

Examples:

  1. Choose with help
  2. Input with help
  3. TextArea with help
  4. Toggle with help
  5. MultiChoose 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 ChooseThemeArrow = ChooseTheme{
	Direction: ChooseDirectionV,
	View: func(choices []string, cursor int) string {
		s := strings.Builder{}
		s.WriteString("\n")

		for i := 0; i < len(choices); i++ {
			if cursor == i {
				s.WriteString(DefaultSelectedItemStyle.Render(fmt.Sprintf("❯ %s", choices[i])))
			} else {
				s.WriteString(DefaultItemStyle.Render(fmt.Sprintf("  %s", choices[i])))
			}
			s.WriteString("\n")
		}

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

		for i := 0; i < len(choices); i++ {
			if cursor == i {
				s.WriteString(DefaultSelectedItemStyle.Render(fmt.Sprintf("• %s", choices[i])))
			} else {
				s.WriteString(DefaultItemStyle.Render(fmt.Sprintf("  %s", choices[i])))
			}
			s.WriteString("\n")
		}

		return s.String()
	},
}
View Source
var ChooseThemeLine = ChooseTheme{
	Direction: ChooseDirectionH,
	View: func(choices []string, cursor int) string {
		s := strings.Builder{}

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

		return s.String()
	},
}

Functions

This section is empty.

Types

type ChooseDirection added in v0.6.0

type ChooseDirection int
const (
	ChooseDirectionAll ChooseDirection = iota
	ChooseDirectionH
	ChooseDirectionV
)

type ChooseModel

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

func NewChooseModel

func NewChooseModel(choices []string, opts ...ChooseOption) *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) UseKeyEnter added in v0.4.0

func (m ChooseModel) UseKeyEnter() bool

func (ChooseModel) UseKeyQ added in v0.3.1

func (m ChooseModel) UseKeyQ() bool

func (ChooseModel) View

func (m ChooseModel) View() string

type ChooseOption added in v0.6.0

type ChooseOption func(*ChooseModel)

func WithTheme added in v0.6.0

func WithTheme(theme ChooseTheme) ChooseOption

type ChooseTheme added in v0.6.0

type ChooseTheme struct {
	Direction ChooseDirection
	View      func([]string, int) string
}

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

func (m MultiChooseModel) UseKeyEnter() bool

func (MultiChooseModel) UseKeyQ added in v0.3.1

func (m MultiChooseModel) UseKeyQ() bool

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 *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 ...ChooseOption) (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) ([]string, error)

MultiChoose lets the user choose multiples from the given choices. Appearance uses the default style.

func (Prompt) MultiChooseWithStyle

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

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

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