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.
- 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) Start() error
- func (buf *Buffer) String() string
- type Terminal
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrCTRLC = errors.New("Interrupted (CTRL+C)")
)
Functions ¶
func Basic ¶
Basic gets input and if required tests to ensure input was given.
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 gets input and if empty uses the given default.
func 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 IsNotTerminal ¶
IsNotTerminal checks if an error is related to io not being a terminal.
func Password ¶
Password retrieves a password from stdin without echoing it.
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(clear, bcrypt.DefaultCost) }
Output:
Types ¶
type AnsiReader ¶
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 ¶
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 ¶
Terminal contains the state for raw terminal input.
func NewTerminal ¶
NewTerminal creates a terminal and sets it to raw input mode.