Documentation
¶
Overview ¶
Package input provides methods for reading user input
Index ¶
- Variables
- func AddHistory(data string)
- func Read(title string, validators ...Validator) (string, error)
- func ReadAnswer(title string, defaultAnswers ...string) (bool, error)
- func ReadPassword(title string, validators ...Validator) (string, error)
- func ReadPasswordSecure(title string, validators ...Validator) (*secstr.String, error)
- func SetCompletionHandler(h CompletionHandler)
- func SetHintHandler(h HintHandler)
- func SetHistoryCapacity(capacity int) error
- type CompletionHandler
- type HintHandler
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NotEmpty returns an error if input is empty NotEmpty = notEmptyValidator{} // IsNumber returns an error if the input is not a valid number IsNumber = isNumberValidator{} // IsFloat returns an error if the input is not a valid floating number IsFloat = isFloatValidator{} // IsEmail returns an error if the input is not a valid email IsEmail = isEmailValidator{} // IsURL returns an error if the input is not a valid URL IsURL = isURLValidator{} )
var ( // ErrIsEmpty is error for empty input ErrIsEmpty = errors.New("You must enter non-empty value") // ErrInvalidNumber is error for invalid number ErrInvalidNumber = errors.New("Entered value is not a valid number") // ErrInvalidFloat is error for invalid floating number ErrInvalidFloat = errors.New("Entered value is not a valid floating number") // ErrInvalidEmail is error for invalid email ErrInvalidEmail = errors.New("Entered value is not a valid e-mail") // ErrInvalidURL is error for invalid URL ErrInvalidURL = errors.New("Entered value is not a valid URL") )
var AlwaysYes = false
AlwaysYes is a flag, if set ReadAnswer will always return true (useful for working with option for forced actions)
var ErrInvalidAnswer = fmt.Errorf("Please enter Y or N")
ErrInvalidAnswer is error for wrong answer for Y/N question
var ErrKillSignal = linenoise.ErrKillSignal
ErrKillSignal is error type when user cancel input
var HideLength = false
HideLength is a flag to hide the password length
var HidePassword = false
HidePassword is a flag to hide the password while typing. Because we are using the low-level linenoise method for this feature, we cannot use a custom masking symbol, so it will always be an asterisk (*).
var MaskSymbol = "*"
MaskSymbol is a symbol used to mask passwords
var MaskSymbolColorTag = ""
MaskSymbolColorTag is an fmtc color tag used for MaskSymbol output
var NewLine = false
NewLine is a flag for extra new line after inputs
var Prompt = "> "
Prompt is a prompt string
var TitleColorTag = "{s}"
TitleColorTag is an fmtc color tag used for input titles
Functions ¶
func AddHistory ¶
func AddHistory(data string)
AddHistory adds line to input history
Example ¶
input, err := Read("Please enter user name", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } // Save entered value to the input history AddHistory(input) fmt.Printf("User name: %s\n", input)
Output:
func Read ¶
Read reads user input
Example ¶
// User must enter name userName, err := Read("Please enter user name", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User name: %s\n", userName) // You can read user input without providing any title fmt.Println("Please enter user name") userName, err = Read("") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User name: %s\n", userName) // You can define many validators at once userEmail, err := Read("Please enter user email", NotEmpty, IsEmail) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User email: %s\n", userEmail)
Output:
func ReadAnswer ¶
ReadAnswer reads user's answer to yes/no question
Example ¶
// If the user doesn't enter any value, we will use the default // value (Y in this case) ok, err := ReadAnswer("Remove this file?", "Y") if !ok || err != nil { return } if ok { fmt.Println("File removed") }
Output:
func ReadPassword ¶
ReadPassword reads password or some private input that will be hidden after pressing Enter
Example ¶
Prompt = "› " MaskSymbol = "•" MaskSymbolColorTag = "{s}" NewLine = true // User must enter the password password, err := ReadPassword("Please enter password", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User password: %s\n", password)
Output:
func ReadPasswordSecure ¶
ReadPasswordSecure reads password or some private input that will be hidden after pressing Enter
Example ¶
Prompt = "› " MaskSymbol = "•" MaskSymbolColorTag = "{s}" // User must enter the password password, err := ReadPasswordSecure("Please enter password", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User password: %s\n", string(password.Data)) password.Destroy()
Output:
func SetCompletionHandler ¶
func SetCompletionHandler(h CompletionHandler)
SetCompletionHandler adds autocompletion function (using Tab key)
Example ¶
commands := []string{"add", "delete", "search", "help", "quit"} SetCompletionHandler(func(input string) []string { var result []string for _, c := range commands { if strings.HasPrefix(c, input) { result = append(result, c) } } return result }) SetHintHandler(func(input string) string { for _, c := range commands { if strings.HasPrefix(c, input) { return c[len(input):] } } return "" }) input, err := Read("Please enter command", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Command: %s\n", input)
Output:
func SetHintHandler ¶
func SetHintHandler(h HintHandler)
SetHintHandler adds function for input hints
Example ¶
commands := []string{"add", "delete", "search", "help", "quit"} SetCompletionHandler(func(input string) []string { var result []string for _, c := range commands { if strings.HasPrefix(c, input) { result = append(result, c) } } return result }) SetHintHandler(func(input string) string { for _, c := range commands { if strings.HasPrefix(c, input) { return c[len(input):] } } return "" }) input, err := Read("Please enter command", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Command: %s\n", input)
Output:
func SetHistoryCapacity ¶
SetHistoryCapacity sets maximum capacity of history
Example ¶
input, err := Read("Please enter user name", NotEmpty) if err != nil { fmt.Printf("Error: %v\n", err) return } // Limit history size to last 3 entries SetHistoryCapacity(3) // Save entered value to the input history AddHistory(input) fmt.Printf("User name: %s\n", input)
Output:
Types ¶
type CompletionHandler ¶
CompletionHandler is completion handler