prompt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: MIT Imports: 9 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.

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 keymaps.

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

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"))
	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 // 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

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 *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 ...tea.ProgramOption) (string, error)

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

func (Prompt) ChooseWithStyle

func (p Prompt) ChooseWithStyle(choices []string, style *ListStyle, opts ...tea.ProgramOption) (string, error)

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

func (Prompt) Init

func (p Prompt) Init() tea.Cmd

func (Prompt) Input

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

Input asks the user to enter a string. You can use InputWithLimit to limit what the user can enter.

func (Prompt) InputWithLimit

func (p Prompt) InputWithLimit(defaultValue string, inputLimit InputLimit, opts ...tea.ProgramOption) (string, error)

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 ...tea.ProgramOption) ([]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, opts ...tea.ProgramOption) ([]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, opts ...tea.ProgramOption) (PromptModel, error)

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

func (*Prompt) SetHelpVisible

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

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) Toggle

func (p Prompt) Toggle(choices []string, opts ...tea.ProgramOption) (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, opts ...tea.ProgramOption) (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

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