2018-05-15 21:13:11 +00:00
|
|
|
package promptui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/chzyer/readline"
|
|
|
|
"github.com/manifoldco/promptui/screenbuf"
|
|
|
|
)
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Prompt represents a single line text field input with options for validation and input masks.
|
2018-05-15 21:13:11 +00:00
|
|
|
type Prompt struct {
|
2020-09-14 18:31:56 +00:00
|
|
|
// Label is the value displayed on the command line prompt.
|
|
|
|
//
|
|
|
|
// The value for Label can be a simple string or a struct that will need to be accessed by dot notation
|
|
|
|
// inside the templates. For example, `{{ .Name }}` will display the name property of a struct.
|
2018-05-15 21:13:11 +00:00
|
|
|
Label interface{}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Default is the initial value for the prompt. This value will be displayed next to the prompt's label
|
|
|
|
// and the user will be able to view or change it depending on the options.
|
|
|
|
Default string
|
2018-05-15 21:13:11 +00:00
|
|
|
|
|
|
|
// AllowEdit lets the user edit the default value. If false, any key press
|
|
|
|
// other than <Enter> automatically clears the default value.
|
|
|
|
AllowEdit bool
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Validate is an optional function that fill be used against the entered value in the prompt to validate it.
|
2018-05-15 21:13:11 +00:00
|
|
|
Validate ValidateFunc
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Mask is an optional rune that sets which character to display instead of the entered characters. This
|
|
|
|
// allows hiding private information like passwords.
|
2018-05-15 21:13:11 +00:00
|
|
|
Mask rune
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// HideEntered sets whether to hide the text after the user has pressed enter.
|
|
|
|
HideEntered bool
|
|
|
|
|
2018-05-15 21:13:11 +00:00
|
|
|
// Templates can be used to customize the prompt output. If nil is passed, the
|
2020-09-14 18:31:56 +00:00
|
|
|
// default templates are used. See the PromptTemplates docs for more info.
|
2018-05-15 21:13:11 +00:00
|
|
|
Templates *PromptTemplates
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// IsConfirm makes the prompt ask for a yes or no ([Y/N]) question rather than request an input. When set,
|
|
|
|
// most properties related to input will be ignored.
|
2018-05-15 21:13:11 +00:00
|
|
|
IsConfirm bool
|
|
|
|
|
|
|
|
// IsVimMode enables vi-like movements (hjkl) and editing.
|
|
|
|
IsVimMode bool
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// the Pointer defines how to render the cursor.
|
|
|
|
Pointer Pointer
|
|
|
|
|
|
|
|
Stdin io.ReadCloser
|
|
|
|
Stdout io.WriteCloser
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PromptTemplates allow a prompt to be customized following stdlib
|
2020-09-14 18:31:56 +00:00
|
|
|
// text/template syntax. Custom state, colors and background color are available for use inside
|
|
|
|
// the templates and are documented inside the Variable section of the docs.
|
|
|
|
//
|
|
|
|
// Examples
|
|
|
|
//
|
|
|
|
// text/templates use a special notation to display programmable content. Using the double bracket notation,
|
|
|
|
// the value can be printed with specific helper functions. For example
|
|
|
|
//
|
|
|
|
// This displays the value given to the template as pure, unstylized text.
|
|
|
|
// '{{ . }}'
|
|
|
|
//
|
|
|
|
// This displays the value colored in cyan
|
|
|
|
// '{{ . | cyan }}'
|
|
|
|
//
|
|
|
|
// This displays the value colored in red with a cyan background-color
|
|
|
|
// '{{ . | red | cyan }}'
|
|
|
|
//
|
|
|
|
// See the doc of text/template for more info: https://golang.org/pkg/text/template/
|
2018-05-15 21:13:11 +00:00
|
|
|
type PromptTemplates struct {
|
2020-09-14 18:31:56 +00:00
|
|
|
// Prompt is a text/template for the prompt label displayed on the left side of the prompt.
|
2018-05-15 21:13:11 +00:00
|
|
|
Prompt string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Prompt is a text/template for the prompt label when IsConfirm is set as true.
|
2018-05-15 21:13:11 +00:00
|
|
|
Confirm string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Valid is a text/template for the prompt label when the value entered is valid.
|
2018-05-15 21:13:11 +00:00
|
|
|
Valid string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Invalid is a text/template for the prompt label when the value entered is invalid.
|
2018-05-15 21:13:11 +00:00
|
|
|
Invalid string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Success is a text/template for the prompt label when the user has pressed entered and the value has been
|
|
|
|
// deemed valid by the validation function. The label will keep using this template even when the prompt ends
|
|
|
|
// inside the console.
|
2018-05-15 21:13:11 +00:00
|
|
|
Success string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Prompt is a text/template for the prompt label when the value is invalid due to an error triggered by
|
|
|
|
// the prompt's validation function.
|
2018-05-15 21:13:11 +00:00
|
|
|
ValidationError string
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// FuncMap is a map of helper functions that can be used inside of templates according to the text/template
|
|
|
|
// documentation.
|
|
|
|
//
|
|
|
|
// By default, FuncMap contains the color functions used to color the text in templates. If FuncMap
|
|
|
|
// is overridden, the colors functions must be added in the override from promptui.FuncMap to work.
|
2018-05-15 21:13:11 +00:00
|
|
|
FuncMap template.FuncMap
|
|
|
|
|
|
|
|
prompt *template.Template
|
|
|
|
valid *template.Template
|
|
|
|
invalid *template.Template
|
|
|
|
validation *template.Template
|
|
|
|
success *template.Template
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// Run executes the prompt. Its displays the label and default value if any, asking the user to enter a value.
|
|
|
|
// Run will keep the prompt alive until it has been canceled from the command prompt or it has received a valid
|
|
|
|
// value. It will return the value and an error if any occurred during the prompt's execution.
|
2018-05-15 21:13:11 +00:00
|
|
|
func (p *Prompt) Run() (string, error) {
|
2020-09-14 18:31:56 +00:00
|
|
|
var err error
|
2018-05-15 21:13:11 +00:00
|
|
|
|
|
|
|
err = p.prepareTemplates()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
c := &readline.Config{
|
|
|
|
Stdin: p.Stdin,
|
|
|
|
Stdout: p.Stdout,
|
|
|
|
EnableMask: p.Mask != 0,
|
|
|
|
MaskRune: p.Mask,
|
|
|
|
HistoryLimit: -1,
|
|
|
|
VimMode: p.IsVimMode,
|
|
|
|
UniqueEditLine: true,
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
err = c.Init()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rl, err := readline.NewEx(c)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-09-14 18:31:56 +00:00
|
|
|
// we're taking over the cursor, so stop showing it.
|
2018-05-15 21:13:11 +00:00
|
|
|
rl.Write([]byte(hideCursor))
|
|
|
|
sb := screenbuf.New(rl)
|
|
|
|
|
|
|
|
validFn := func(x string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if p.Validate != nil {
|
|
|
|
validFn = p.Validate
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputErr error
|
|
|
|
input := p.Default
|
|
|
|
if p.IsConfirm {
|
|
|
|
input = ""
|
|
|
|
}
|
|
|
|
eraseDefault := input != "" && !p.AllowEdit
|
2020-09-14 18:31:56 +00:00
|
|
|
cur := NewCursor(input, p.Pointer, eraseDefault)
|
2018-05-15 21:13:11 +00:00
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
listen := func(input []rune, pos int, key rune) ([]rune, int, bool) {
|
|
|
|
_, _, keepOn := cur.Listen(input, pos, key)
|
|
|
|
err := validFn(cur.Get())
|
2018-05-15 21:13:11 +00:00
|
|
|
var prompt []byte
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
prompt = render(p.Templates.invalid, p.Label)
|
|
|
|
} else {
|
|
|
|
prompt = render(p.Templates.valid, p.Label)
|
|
|
|
if p.IsConfirm {
|
|
|
|
prompt = render(p.Templates.prompt, p.Label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
echo := cur.Format()
|
2018-05-15 21:13:11 +00:00
|
|
|
if p.Mask != 0 {
|
2020-09-14 18:31:56 +00:00
|
|
|
echo = cur.FormatMask(p.Mask)
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
prompt = append(prompt, []byte(echo)...)
|
2018-05-15 21:13:11 +00:00
|
|
|
sb.Reset()
|
|
|
|
sb.Write(prompt)
|
|
|
|
if inputErr != nil {
|
|
|
|
validation := render(p.Templates.validation, inputErr)
|
|
|
|
sb.Write(validation)
|
|
|
|
inputErr = nil
|
|
|
|
}
|
|
|
|
sb.Flush()
|
2020-09-14 18:31:56 +00:00
|
|
|
return nil, 0, keepOn
|
|
|
|
}
|
2018-05-15 21:13:11 +00:00
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
c.SetListener(listen)
|
2018-05-15 21:13:11 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
_, err = rl.Readline()
|
2020-09-14 18:31:56 +00:00
|
|
|
inputErr = validFn(cur.Get())
|
2018-05-15 21:13:11 +00:00
|
|
|
if inputErr == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-09-14 18:31:56 +00:00
|
|
|
switch err {
|
|
|
|
case readline.ErrInterrupt:
|
|
|
|
err = ErrInterrupt
|
|
|
|
case io.EOF:
|
|
|
|
err = ErrEOF
|
|
|
|
}
|
2018-05-15 21:13:11 +00:00
|
|
|
if err.Error() == "Interrupt" {
|
|
|
|
err = ErrInterrupt
|
|
|
|
}
|
|
|
|
sb.Reset()
|
|
|
|
sb.WriteString("")
|
|
|
|
sb.Flush()
|
|
|
|
rl.Write([]byte(showCursor))
|
|
|
|
rl.Close()
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
echo := cur.Get()
|
2018-05-15 21:13:11 +00:00
|
|
|
if p.Mask != 0 {
|
2021-10-06 17:33:14 +00:00
|
|
|
echo = cur.GetMask(p.Mask)
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
prompt := render(p.Templates.success, p.Label)
|
2018-05-15 21:13:11 +00:00
|
|
|
prompt = append(prompt, []byte(echo)...)
|
|
|
|
|
|
|
|
if p.IsConfirm {
|
|
|
|
lowerDefault := strings.ToLower(p.Default)
|
2020-09-14 18:31:56 +00:00
|
|
|
if strings.ToLower(cur.Get()) != "y" && (lowerDefault != "y" || (lowerDefault == "y" && cur.Get() != "")) {
|
2018-05-15 21:13:11 +00:00
|
|
|
prompt = render(p.Templates.invalid, p.Label)
|
|
|
|
err = ErrAbort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
if p.HideEntered {
|
|
|
|
clearScreen(sb)
|
|
|
|
} else {
|
|
|
|
sb.Reset()
|
|
|
|
sb.Write(prompt)
|
|
|
|
sb.Flush()
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:13:11 +00:00
|
|
|
rl.Write([]byte(showCursor))
|
|
|
|
rl.Close()
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
return cur.Get(), err
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Prompt) prepareTemplates() error {
|
|
|
|
tpls := p.Templates
|
|
|
|
if tpls == nil {
|
|
|
|
tpls = &PromptTemplates{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tpls.FuncMap == nil {
|
|
|
|
tpls.FuncMap = FuncMap
|
|
|
|
}
|
|
|
|
|
|
|
|
bold := Styler(FGBold)
|
|
|
|
|
|
|
|
if p.IsConfirm {
|
|
|
|
if tpls.Confirm == "" {
|
|
|
|
confirm := "y/N"
|
|
|
|
if strings.ToLower(p.Default) == "y" {
|
|
|
|
confirm = "Y/n"
|
|
|
|
}
|
|
|
|
tpls.Confirm = fmt.Sprintf(`{{ "%s" | bold }} {{ . | bold }}? {{ "[%s]" | faint }} `, IconInitial, confirm)
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.New("").Funcs(tpls.FuncMap).Parse(tpls.Confirm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.prompt = tpl
|
|
|
|
} else {
|
|
|
|
if tpls.Prompt == "" {
|
|
|
|
tpls.Prompt = fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconInitial), bold(":"))
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.New("").Funcs(tpls.FuncMap).Parse(tpls.Prompt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.prompt = tpl
|
|
|
|
}
|
|
|
|
|
|
|
|
if tpls.Valid == "" {
|
|
|
|
tpls.Valid = fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconGood), bold(":"))
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.New("").Funcs(tpls.FuncMap).Parse(tpls.Valid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.valid = tpl
|
|
|
|
|
|
|
|
if tpls.Invalid == "" {
|
|
|
|
tpls.Invalid = fmt.Sprintf("%s {{ . | bold }}%s ", bold(IconBad), bold(":"))
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Invalid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.invalid = tpl
|
|
|
|
|
|
|
|
if tpls.ValidationError == "" {
|
|
|
|
tpls.ValidationError = `{{ ">>" | red }} {{ . | red }}`
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.ValidationError)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.validation = tpl
|
|
|
|
|
|
|
|
if tpls.Success == "" {
|
2020-09-14 18:31:56 +00:00
|
|
|
tpls.Success = fmt.Sprintf("{{ . | faint }}%s ", Styler(FGFaint)(":"))
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err = template.New("").Funcs(tpls.FuncMap).Parse(tpls.Success)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpls.success = tpl
|
|
|
|
|
|
|
|
p.Templates = tpls
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|