input

package
v12.123.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package input provides methods for reading user input

Index

Examples

Constants

This section is empty.

Variables

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 ErrKillSignal = linenoise.ErrKillSignal

ErrKillSignal is error type when user cancel input

View Source
var HideLength = false

HideLength is flag for hiding password length

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

View Source
var MaskSymbol = "*"

MaskSymbol is symbol used for masking passwords

View Source
var MaskSymbolColorTag = ""

MaskSymbolColorTag is fmtc color tag used for MaskSymbol output

View Source
var Prompt = "> "

Prompt is prompt string

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

func Read(title string, nonEmpty bool) (string, error)

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

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, nonEmpty bool) (string, error)

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

func ReadPasswordSecure(title string, nonEmpty bool) (*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", 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 added in v12.118.0

func SetHistoryCapacity(capacity int) error

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 added in v12.118.0

type CompletionHandler = func(input string) []string

CompletionHandler is completion handler

type HintHandler added in v12.118.0

type HintHandler = func(input string) string

HintHandler is hint handler

Jump to

Keyboard shortcuts

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