Documentation
¶
Overview ¶
Package term provides support for interacting with terminals.
Index ¶
- func Canonical(r *RawConfig)
- func ConvertLineEndings(r *RawConfig)
- func GenSignals(r *RawConfig)
- func IsTTY(f *os.File) bool
- func NonBlocking(r *RawConfig)
- func WatchResize(ctx context.Context, f *os.File) (<-chan Dimensions, error)
- type Device
- type Dimensions
- type Input
- type InputKey
- type Pos
- type RawConfig
- type RawOption
- type Screen
- func (s *Screen) Bell()
- func (s *Screen) ClearLine()
- func (s *Screen) ClearLineToEnd()
- func (s *Screen) ClearLineToStart()
- func (s *Screen) ClearScreen()
- func (s *Screen) ClearToEnd()
- func (s *Screen) ClearToStart()
- func (s *Screen) CursorDown(n int)
- func (s *Screen) CursorLeft(n int)
- func (s *Screen) CursorPos() (Pos, error)
- func (s *Screen) CursorRight(n int)
- func (s *Screen) CursorTo(pos Pos)
- func (s *Screen) CursorUp(n int)
- func (s *Screen) HideCursor()
- func (s *Screen) Interruptible(state bool)
- func (s *Screen) Print(a ...interface{}) (n int, err error)
- func (s *Screen) Printf(format string, a ...interface{}) (n int, err error)
- func (s *Screen) Println(a ...interface{}) (n int, err error)
- func (s *Screen) Read(p []byte) (n int, err error)
- func (s *Screen) ReadInput() (*Input, error)
- func (s *Screen) ReadSecret(prompt string) ([]byte, error)
- func (s *Screen) Readline() ([]byte, error)
- func (s *Screen) ReadlineWithPrompt(prompt string) ([]byte, error)
- func (s *Screen) ShowCursor()
- func (s *Screen) TrueColor() bool
- func (s *Screen) Write(p []byte) (n int, err error)
- func (s *Screen) WriteString(p string) (n int, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶
func Canonical(r *RawConfig)
Canonical provides a `RawOption` to enable line-based canonical/cooked input processing.
func ConvertLineEndings ¶
func ConvertLineEndings(r *RawConfig)
ConvertLineEndings provides a `RawOption` to automatically convert CR to NL on input, and NL to CRNL when writing output.
func GenSignals ¶
func GenSignals(r *RawConfig)
GenSignals provides a `RawOption` to turn control characters like `^C` and `^Z` into signals instead of passing them through directly as characters.
func NonBlocking ¶
func NonBlocking(r *RawConfig)
NonBlocking provides a `RawOption` that configures the device to simulate non-blocking behavior by allowing `Read` calls to return immediately when there is no data to read.
This should be used sparingly as it could degrade performance. However, it should still be better than changing the blocking mode with `O_NONBLOCK`, e.g. changing the mode on stdin could easily break the shell under normal circumstances when the program exits.
func WatchResize ¶
WatchResize sends updated dimensions whenever the terminal window is resized.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device represents a device file that has been "converted" using `MakeRaw`.
Devices must be reset by calling the `Reset` method before the program exits. Otherwise, external systems like the user's shell might be left in a broken state.
func MakeRaw ¶
MakeRaw converts the given device file into "raw" mode, i.e. disables echoing, disables special processing of certain characters, etc.
type Dimensions ¶
Dimensions represents the window dimensions for the terminal.
func WindowSize ¶
func WindowSize(f *os.File) (Dimensions, error)
WindowSize returns the dimensions of the terminal.
type Input ¶
Input represents input received from the terminal. The `Byte` field represents non-control characters. When the `Byte` field is `0`, then the `Key` field represents a control character or arrow key.
Note that both `\n` and `\r` are mapped to `KeyEnter` for simplicity, and that the `\n` and `\t` characters are returned in the `Key` field, while the space character `' '` is returned in the `Byte` field.
type InputKey ¶
type InputKey int
InputKey represents a special input key received from the terminal. This encompasses both control characters and arrow keys.
const ( KeyNull InputKey = iota KeyCtrlA KeyCtrlB KeyCtrlC KeyCtrlD KeyCtrlE KeyCtrlF KeyCtrlG KeyCtrlH KeyCtrlI KeyCtrlJ KeyCtrlK KeyCtrlL KeyCtrlM KeyCtrlN KeyCtrlO KeyCtrlP KeyCtrlQ KeyCtrlR KeyCtrlS KeyCtrlT KeyCtrlU KeyCtrlV KeyCtrlW KeyCtrlX KeyCtrlY KeyCtrlZ KeyCtrlLeftBracket KeyCtrlBackslash KeyCtrlRightBracket KeyCtrlCaret KeyCtrlUnderscore )
Control characters.
type RawConfig ¶
type RawConfig struct {
// contains filtered or unexported fields
}
RawConfig specifies the configuration for `MakeRaw`. It can be specified using one of the `RawOption` functions.
type RawOption ¶
type RawOption func(*RawConfig)
RawOption functions configure `RawConfig` for `MakeRaw` calls.
type Screen ¶
type Screen struct {
// contains filtered or unexported fields
}
Screen provides an interface for building interactive terminal applications.
On UNIX systems, `Screen` reads and writes from `/dev/tty`. On Windows, it reads from `CONIN$` and writes to `CONOUT$`.
func (*Screen) ClearLineToEnd ¶
func (s *Screen) ClearLineToEnd()
ClearLineToEnd clears everything from the cursor to the end of the current line.
func (*Screen) ClearLineToStart ¶
func (s *Screen) ClearLineToStart()
ClearLineToStart clears everything from the cursor to the start of the current line.
func (*Screen) ClearScreen ¶
func (s *Screen) ClearScreen()
ClearScreen clears the screen and moves the cursor to the top left.
func (*Screen) ClearToEnd ¶
func (s *Screen) ClearToEnd()
ClearToEnd clears everything from the cursor to the end of the screen.
func (*Screen) ClearToStart ¶
func (s *Screen) ClearToStart()
ClearToStart clears everything from the cursor to the start of the screen.
func (*Screen) CursorDown ¶
CursorDown moves the cursor down by the given amount.
func (*Screen) CursorLeft ¶
CursorLeft moves the cursor left by the given amount.
func (*Screen) CursorRight ¶
CursorRight moves the cursor right by the given amount.
func (*Screen) HideCursor ¶
func (s *Screen) HideCursor()
HideCursor hides the cursor on the terminal. It also registers a `process.Exit` handler to restore the cursor when the process exits.
func (*Screen) Interruptible ¶
Interruptible sets whether the reading of input from the terminal can be interrupted by certain control characters. If interruptible:
* `^C` exits the process with an exit code of 130.
* `^\` aborts the process with a panic and prints stacktraces.
All `Screen` instances are interruptible by default.
func (*Screen) Print ¶
Print formats the operands like `fmt.Print` and writes to the terminal output.
func (*Screen) Printf ¶
Printf formats the operands like `fmt.Printf` and writes to the terminal output.
func (*Screen) Println ¶
Println formats the operands like `fmt.Println` and writes to the terminal output.
func (*Screen) ReadSecret ¶
ReadSecret prompts the user for a secret without echoing. The prompt is written to `os.Stderr` as that is more likely to be seen, e.g. if a user has redirected stdout.
Unlike the other read-related methods, this method defers to the platform for processing input and handling interrupt characters like `^C`. Special consideration is only given to the backspace character which overwrites the previous byte when one is present.
func (*Screen) Readline ¶
Readline keeps reading from the terminal input until a `\n` (or `\r` on Windows) is encountered.
func (*Screen) ReadlineWithPrompt ¶
ReadlineWithPrompt emits the given `prompt` to the terminal output before invoking `Readline`.