Documentation ¶
Overview ¶
Package input provides methods for reading user input
Index ¶
- Variables
- func AddHistory(data string)
- func Read(title string, nonEmpty bool) (string, error)
- func ReadAnswer(title string, defaultAnswers ...string) (bool, error)
- func ReadPassword(title string, nonEmpty bool) (string, error)
- func ReadPasswordSecure(title string, nonEmpty bool) (*secstr.String, error)
- func SetCompletionHandler(h CompletionHandler)
- func SetHintHandler(h HintHandler)
- func SetHistoryCapacity(capacity int) error
- type CompletionHandler
- type HintHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AlwaysYes = false
AlwaysYes is a flag, if set ReadAnswer will always return true (useful for working with option for forced actions)
var ErrKillSignal = linenoise.ErrKillSignal
ErrKillSignal is error type when user cancel input
var HideLength = false
HideLength is flag for hiding password length
var HidePassword = false
HidePassword is flag for hiding password while typing Because of using the low-level linenoise method for this feature, we can not use a custom masking symbol, so it always will be an asterisk (*).
var MaskSymbol = "*"
MaskSymbol is symbol used for masking passwords
var MaskSymbolColorTag = ""
MaskSymbolColorTag is fmtc color tag used for MaskSymbol output
var Prompt = "> "
Prompt is prompt string
var TitleColorTag = "{s}"
TitleColorTag is 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", true) 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 input, err := Read("Please enter user name", true) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User name: %s\n", input) // You can read user input without providing any title fmt.Println("Please enter user name") input, err = Read("", true) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("User name: %s\n", input)
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}" // User must enter the password password, err := ReadPassword("Please enter password", true) 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", true) 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", true) 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", true) 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", true) 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