Documentation ¶
Overview ¶
Package prompt implements a cross platform line-editing prompt. It also provides routines to use ANSI escape sequences across platforms for terminal connected io.Readers/io.Writers.
If os.Stdin isn't connected to a terminal or (on Unix)if the terminal doesn't support the ANSI escape sequences needed a fallback prompt is provided that doesn't do line-editing. Unix terminals that are not supported will have the TERM environment variable set to either "dumb" or "cons25".
The keyboard shortcuts are similar to those found in the Readline library:
- Enter / CTRL+D
- End the line.
- CTRL+C
- End the line, return error `ErrCTRLC`.
- Backspace
- Remove the character to the left.
- CTRL+L
- Clear the screen(keeping the current lines content).
- Home / End
- Jump to the beginning/end of the line.
- Up arrow / Down arrow
- Go back and forward in the history.
- Left arrow / Right arrow
- Move left/right one character.
- Delete
- Remove the character to the right.
Index ¶
- Variables
- func Ask(question string) (bool, error)
- func Basic(prefix string, required bool) (string, error)
- func BasicDefault(prefix, def string) (string, error)
- func Custom(prefix string, test func(string) (string, bool)) (string, error)
- func IsNotTerminal(err error) bool
- func Password(prefix string) (string, error)
- func TerminalSize(out *os.File) (int, int, error)
- type AnsiReader
- type AnsiWriter
- type Buffer
- func (buf *Buffer) ClsScreen() error
- func (buf *Buffer) Del() error
- func (buf *Buffer) DelLeft() error
- func (buf *Buffer) End() error
- func (buf *Buffer) EndLine() error
- func (buf *Buffer) Insert(rs ...rune) error
- func (buf *Buffer) Left() error
- func (buf *Buffer) Refresh() error
- func (buf *Buffer) Right() error
- func (buf *Buffer) Set(rs ...rune) error
- func (buf *Buffer) Start() error
- func (buf *Buffer) String() string
- type Terminal
- func (term *Terminal) Ask(question string) (bool, error)
- func (term *Terminal) Basic(prefix string, required bool) (string, error)
- func (term *Terminal) BasicDefault(prefix, def string) (string, error)
- func (term *Terminal) Close() error
- func (term *Terminal) Custom(prefix string, test func(string) (string, bool)) (string, error)
- func (term *Terminal) GetPassword(prefix string) (string, error)
- func (term *Terminal) GetPrompt(prefix string) (string, error)
- func (term *Terminal) Password(prefix string) (string, error)
- func (term *Terminal) Reopen() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCTRLC is returned when CTRL+C is pressed stopping the prompt. ErrCTRLC = errors.New("Interrupted (CTRL+C)") // ErrEOF is returned when CTRL+D is pressed stopping the prompt. ErrEOF = errors.New("EOF (CTRL+D)") )
Functions ¶
func Basic ¶
Basic is a wrapper around Terminal.Basic.
Example ¶
package main import ( "net/mail" "github.com/Bowery/prompt" ) func main() (string, error) { email, err := prompt.Basic("Email", true) if err != nil { return "", err } _, err = mail.ParseAddress(email) return email, err }
Output:
func BasicDefault ¶
BasicDefault is a wrapper around Terminal.BasicDefault.
func IsNotTerminal ¶
IsNotTerminal checks if an error is related to the input not being a terminal.
func Password ¶
Password is a wrapper around Terminal.Password.
Example ¶
package main import ( "github.com/Bowery/prompt" "golang.org/x/crypto/bcrypt" ) func main() ([]byte, error) { clear, err := prompt.Password("Password") if err != nil { return nil, err } return bcrypt.GenerateFromPassword([]byte(clear), bcrypt.DefaultCost) }
Output:
Types ¶
type AnsiReader ¶
type AnsiReader struct {
// contains filtered or unexported fields
}
AnsiReader is an io.Reader that wraps an *os.File.
func NewAnsiReader ¶
func NewAnsiReader(in *os.File) *AnsiReader
NewAnsiReader creates a AnsiReader from the given input file.
type AnsiWriter ¶
type AnsiWriter struct {
// contains filtered or unexported fields
}
AnsiWriter is an io.Writer that wraps an *os.File.
func NewAnsiWriter ¶
func NewAnsiWriter(out *os.File) *AnsiWriter
NewAnsiWriter creates a AnsiWriter from the given output file.
type Buffer ¶
type Buffer struct { Out *os.File Prompt string Echo bool Cols int // contains filtered or unexported fields }
Buffer contains state for line editing and writing.
type Terminal ¶
type Terminal struct { In *os.File Out *os.File History []string // contains filtered or unexported fields }
Terminal contains the state for raw terminal input.
func NewTerminal ¶
NewTerminal creates a terminal and sets it to raw input mode.
func (*Terminal) Ask ¶
Ask gets input and checks if it's truthy or not, and returns that in a boolean fashion.
func (*Terminal) BasicDefault ¶
BasicDefault gets input and if empty uses the given default.
func (*Terminal) Custom ¶
Custom gets input and calls the given test function with the input to check if the input is valid, a true return will return the string.
func (*Terminal) GetPassword ¶
GetPassword gets a line with the prefix and doesn't echo input.