Documentation ¶
Overview ¶
Package term provides functions on unixoid systems for dealing with POSIX compliant terminals/terminal emulators that also support ANSI escape sequences.
It is only tested on Linux with the Xfce terminal emulator and the Linux console.
All inputs can be canceled with ^D (EOF).
Index ¶
- func GetBytes(echo EchoMode, limit uint8) ([]byte, error)
- func GetChar(echo bool) (rune, error)
- func GetLine() (string, error)
- func GetPassword() ([]byte, error)
- func GetSize(fd uintptr) (uint16, uint16, error)
- func Input(prompt string, in interface{}, opt *InputOpt) error
- func IsTerminal(fd uintptr) bool
- func MakeRaw(fd uintptr) (func() error, error)
- func Menu(prompt, title string, options []string, columns uint) (uint, error)
- func MenuWithDefault(prompt, title string, options []string, columns, dfltIdx uint) (uint, error)
- func Select(prompt, options string) (uint, error)
- func YesNo(prompt, options string) (bool, error)
- type EchoMode
- type InputOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBytes ¶
GetBytes gets input from a terminal and returns it as a slice of bytes, which does not include the final \n (if any). The echo parameter controls what is printed to the screen. If limit > 0, its the max. number of characters to get; if the number is reached the input will be submitted w/o typing enter. It panics if stdin and stdout are not connected to a terminal.
func GetChar ¶
GetChar gets one character from a terminal. It panics if stdin and stdout are not connected to a terminal.
func GetLine ¶
GetLine gets one line of input from a terminal. It panics if stdin and stdout are not connected to a terminal.
func GetPassword ¶
GetPassword gets one line of input from a terminal with the input masked with an * character. It panics if stdin and stdout are not connected to a terminal.
func GetSize ¶
GetSize returns the size (width, height) of the terminal. It returns an error if the file descriptor fd is not connected to a terminal.
func Input ¶
Input gets input from a terminal. The in argument must be the address of a variable to which the input should be assigned. If only enter is typed and there is no default value or if the input cannot be converted to the correct type, the prompt will be shown again. It panics if stdin and stdout are not connected to a terminal or if opt.Default or the return value of opt.ConvFunc are not assignable to *in.
func IsTerminal ¶
IsTerminal returns whether the file descriptor fd is connected to a terminal.
func MakeRaw ¶
MakeRaw puts the terminal into raw mode as decribed in section "Raw Mode" in the termios(3) manpage. It returns an error if the file descriptor fd is not connected to a terminal. The returned function can be used to restore the terminal to its previous state.
restore, err := term.MakeRaw(os.Stdout.Fd()) if err != nil { panic(err) } defer restore()
func Menu ¶
Menu prints a menu to the screen and returns the index of the selected option within the options slice. If columns is 0, the number of columns will be computed depending on the screen size and the number of options. If title is not "" it will be printed above the menu. It panics if stdin and stdout are not connected to a terminal.
func MenuWithDefault ¶ added in v0.2.0
MenuWithDefault does the same as Menu but takes a default value for the index of an option. The value will be ignored if dfltIdx >= len(options).
func Select ¶
Select accepts one character from the options string and returns its index within the options. If one character in options is upper case, it is the default. It panics if stdin and stdout are not connected to a terminal or if more than one character are upper case.
func YesNo ¶
YesNo gets the answer to a yes/no question. The options string must contain exactly two characters. The first is for yes (returning true), the second for no (returning false). If one character is upper case, it is the default. The options will be appended to the prompt.
term.YesNo("Exit?", "yN") -> Exit? [yN]
It panics if stdin and stdout are not connected to a terminal, if there are more than two characters in options or if both are upper case.
Types ¶
type InputOpt ¶
type InputOpt struct { Default interface{} // optional Echo EchoMode // default: EchoNormal Limit uint8 // see function GetBytes ConvFunc func(string) (interface{}, error) // optional }
Options for Input function. If ConvFunc is used it must return an error if the input value cannot be converted.