term

package
v0.0.0-...-dc64ae7 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: CC0-1.0 Imports: 14 Imported by: 0

Documentation

Overview

Package term provides support for interacting with terminals.

Index

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 IsTTY

func IsTTY(f *os.File) bool

IsTTY checks whether the given device file is connected to a terminal.

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

func WatchResize(ctx context.Context, f *os.File) (<-chan Dimensions, error)

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

func MakeRaw(f *os.File, opts ...RawOption) (*Device, error)

MakeRaw converts the given device file into "raw" mode, i.e. disables echoing, disables special processing of certain characters, etc.

func (*Device) Read

func (d *Device) Read(p []byte) (int, error)

Read does a raw read on the device.

func (*Device) Reset

func (d *Device) Reset() error

Reset resets the device file back to its initial state before it was converted.

type Dimensions

type Dimensions struct {
	Cols int
	Rows int
}

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

type Input struct {
	Byte byte
	Key  InputKey
}

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.

const (
	KeyBackspace InputKey = 127
	KeyEnter     InputKey = '\n'
	KeyEscape    InputKey = 27
	KeyInterrupt InputKey = KeyCtrlC
	KeyTab       InputKey = '\t'
)

Common aliases for control characters.

const (
	KeyDown InputKey = iota + 128
	KeyEnd
	KeyHome
	KeyLeft
	KeyPageDown
	KeyPageUp
	KeyRight
	KeyUp
)

Arrow keys.

type Pos

type Pos struct {
	Col int
	Row int
}

Pos represents the cursor position on the terminal.

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 New

func New() (*Screen, error)

New instantiates a new `Screen` for interactive terminal applications.

func (*Screen) Bell

func (s *Screen) Bell()

Bell tells the terminal to emit a beep/bell.

func (*Screen) ClearLine

func (s *Screen) ClearLine()

ClearLine clears the current line.

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

func (s *Screen) CursorDown(n int)

CursorDown moves the cursor down by the given amount.

func (*Screen) CursorLeft

func (s *Screen) CursorLeft(n int)

CursorLeft moves the cursor left by the given amount.

func (*Screen) CursorPos

func (s *Screen) CursorPos() (Pos, error)

CursorPos returns the current position of the cursor.

func (*Screen) CursorRight

func (s *Screen) CursorRight(n int)

CursorRight moves the cursor right by the given amount.

func (*Screen) CursorTo

func (s *Screen) CursorTo(pos Pos)

CursorTo moves the cursor to the given position.

func (*Screen) CursorUp

func (s *Screen) CursorUp(n int)

CursorUp moves the cursor up 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

func (s *Screen) Interruptible(state bool)

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

func (s *Screen) Print(a ...interface{}) (n int, err error)

Print formats the operands like `fmt.Print` and writes to the terminal output.

func (*Screen) Printf

func (s *Screen) Printf(format string, a ...interface{}) (n int, err error)

Printf formats the operands like `fmt.Printf` and writes to the terminal output.

func (*Screen) Println

func (s *Screen) Println(a ...interface{}) (n int, err error)

Println formats the operands like `fmt.Println` and writes to the terminal output.

func (*Screen) Read

func (s *Screen) Read(p []byte) (n int, err error)

Read reads the terminal input.

func (*Screen) ReadInput

func (s *Screen) ReadInput() (*Input, error)

ReadInput reads `Input` from the terminal.

func (*Screen) ReadSecret

func (s *Screen) ReadSecret(prompt string) ([]byte, error)

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

func (s *Screen) Readline() ([]byte, error)

Readline keeps reading from the terminal input until a `\n` (or `\r` on Windows) is encountered.

func (*Screen) ReadlineWithPrompt

func (s *Screen) ReadlineWithPrompt(prompt string) ([]byte, error)

ReadlineWithPrompt emits the given `prompt` to the terminal output before invoking `Readline`.

func (*Screen) ShowCursor

func (s *Screen) ShowCursor()

ShowCursor makes the cursor visible.

func (*Screen) TrueColor

func (s *Screen) TrueColor() bool

TrueColor returns whether the terminal supports 24-bit colors.

func (*Screen) Write

func (s *Screen) Write(p []byte) (n int, err error)

Write writes to the terminal output.

func (*Screen) WriteString

func (s *Screen) WriteString(p string) (n int, err error)

WriteString is like `Write`, but writes a string instead of a byte slice.

Directories

Path Synopsis
Package style provides support for styling terminal output.
Package style provides support for styling terminal output.

Jump to

Keyboard shortcuts

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