term

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 License: BSD-3-Clause Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBytes

func GetBytes(echo EchoMode, limit uint8) ([]byte, error)

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

func GetChar(echo bool) (rune, error)

GetChar gets one character from a terminal. It panics if stdin and stdout are not connected to a terminal.

func GetLine

func GetLine() (string, error)

GetLine gets one line of input from a terminal. It panics if stdin and stdout are not connected to a terminal.

func GetPassword

func GetPassword() ([]byte, error)

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

func GetSize(fd uintptr) (uint16, uint16, error)

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

func Input(prompt string, in interface{}, opt *InputOpt) error

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

func IsTerminal(fd uintptr) bool

IsTerminal returns whether the file descriptor fd is connected to a terminal.

func MakeRaw

func MakeRaw(fd uintptr) (func() error, error)

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(prompt, title string, options []string, columns uint) (uint, error)

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(prompt, title string, options []string, columns, dfltIdx uint) (uint, error)

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

func Select(prompt, options string) (uint, error)

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

func YesNo(prompt, options string) (bool, error)

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 EchoMode

type EchoMode uint8
const (
	EchoNormal EchoMode = iota // characters are printed to the screen as typed
	EchoNone                   // nothing is printed to the screen
	EchoMask                   // an * is printed to the screen for each character
)

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.

Jump to

Keyboard shortcuts

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