Documentation ¶
Overview ¶
Package terminal provides methods for working with user input
Index ¶
- Variables
- func AddHistory(data string)
- func Error(message any, args ...any)
- func Info(message any, args ...any)
- func PrintActionMessage(message string)
- func PrintActionStatus(status int)
- func PrintErrorMessage(message string, args ...any)deprecated
- func PrintWarnMessage(message string, args ...any)deprecated
- 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 ReadUI(title string, nonEmpty bool) (string, error)deprecated
- func SetCompletionHandler(h func(input string) []string)
- func SetHintHandler(h func(input string) string)
- func Warn(message any, args ...any)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // MaskSymbolColorTag is fmtc color tag used for MaskSymbol output MaskSymbolColorTag = "" // TitleColorTag is fmtc color tag used for input titles TitleColorTag = "{s}" // ErrorColorTag is fmtc color tag used for error messages ErrorColorTag = "{r}" // WarnColorTag is fmtc color tag used for warning messages WarnColorTag = "{y}" // InfoColorTag is fmtc color tag used for info messages InfoColorTag = "{c-}" )
var ( // ErrorPrefix is prefix for error messages ErrorPrefix = "" // WarnPrefix is prefix for warning messages WarnPrefix = "" // InfoPrefix is prefix for info messages InfoPrefix = "" )
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 Prompt = "> "
Prompt is prompt string
Functions ¶
func Error ¶ added in v12.70.0
Error prints error message
Example ¶
// Add custom error message prefix ErrorPrefix = "▲ " // Print red text to stderr Error("Error while sending data to %s", "https://example.com") // Print message from error struct err := errors.New("My error") Error(err)
Output:
func Info ¶ added in v12.70.0
Info prints info message
Example ¶
// Add custom info message prefix InfoPrefix = "❕ " // Print cyan text to stdout Warn("User %q will be created automatically", "bob")
Output:
func PrintActionMessage ¶
func PrintActionMessage(message string)
PrintActionMessage prints message about action currently in progress
Example ¶
statusOk := true PrintActionMessage("Starting service my-service") switch statusOk { case true: PrintActionStatus(0) // Print OK case false: PrintActionStatus(1) // Print ERROR }
Output:
func PrintActionStatus ¶
func PrintActionStatus(status int)
PrintActionStatus prints message with action execution status
Example ¶
statusOk := true PrintActionMessage("Starting service my-service") switch statusOk { case true: PrintActionStatus(0) // Print OK case false: PrintActionStatus(1) // Print ERROR }
Output:
func PrintErrorMessage
deprecated
func PrintWarnMessage
deprecated
func Read ¶ added in v12.64.0
Read reads user's input
Example ¶
// User must enter name input, err := ReadUI("Please enter user name", true) if err != nil { fmt.Printf("Error: %v", err) } fmt.Printf("User name: %s\v", input) // You can read user input without providing any title fmt.Println("Please enter user name") input, err = ReadUI("", true) if err != nil { fmt.Printf("Error: %v", err) } fmt.Printf("User name: %s\v", input)
Output:
func ReadAnswer ¶
ReadAnswer reads user's answer for 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 which 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", err) } fmt.Printf("User password: %s\v", password)
Output:
func ReadPasswordSecure ¶ added in v12.43.0
ReadPasswordSecure reads password or some private input which 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", err) } fmt.Printf("User password: %s\v", string(password.Data)) password.Destroy()
Output:
func SetCompletionHandler ¶
SetCompletionHandler adds function for autocompletion
func SetHintHandler ¶
SetHintHandler adds function for input hints
Types ¶
This section is empty.