input

package
v13.16.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 14 Imported by: 3

Documentation

Overview

Package input provides methods for reading user input

Index

Examples

Constants

This section is empty.

Variables

View Source
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{}
)
View Source
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")
)
View Source
var AlwaysYes = false

AlwaysYes is a flag, if set ReadAnswer will always return true (useful for working with option for forced actions)

View Source
var ErrInvalidAnswer = fmt.Errorf("Please enter Y or N")

ErrInvalidAnswer is error for wrong answer for Y/N question

View Source
var ErrKillSignal = linenoise.ErrKillSignal

ErrKillSignal is error type when user cancel input

View Source
var HideLength = false

HideLength is a flag to hide the password length

View Source
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 (*).

View Source
var MaskSymbol = "*"

MaskSymbol is a symbol used to mask passwords

View Source
var MaskSymbolColorTag = ""

MaskSymbolColorTag is an fmtc color tag used for MaskSymbol output

View Source
var NewLine = false

NewLine is a flag for extra new line after inputs

View Source
var Prompt = "> "

Prompt is a prompt string

View Source
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

func Read(title string, validators ...Validator) (string, error)

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

func ReadAnswer(title string, defaultAnswers ...string) (bool, error)

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

func ReadPassword(title string, validators ...Validator) (string, error)

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

func ReadPasswordSecure(title string, validators ...Validator) (*secstr.String, error)

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

func SetHistoryCapacity(capacity int) error

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

type CompletionHandler = func(input string) []string

CompletionHandler is completion handler

type HintHandler

type HintHandler = func(input string) string

HintHandler is hint handler

type Validator added in v13.10.0

type Validator interface {
	Validate(input string) (string, error)
}

Validator is input validator type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL