term

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: Apache-2.0 Imports: 6 Imported by: 984

README

term - utilities for dealing with terminals

Test GoDoc Go Report Card

term provides structures and helper functions to work with terminal (state, sizes).

Using term
package main

import (
	"log"
	"os"

	"github.com/moby/term"
)

func main() {
	fd := os.Stdin.Fd()
	if term.IsTerminal(fd) {
		ws, err := term.GetWinsize(fd)
		if err != nil {
			log.Fatalf("term.GetWinsize: %s", err)
		}
		log.Printf("%d:%d\n", ws.Height, ws.Width)
	}
}

Contributing

Want to hack on term? Docker's contributions guidelines apply.

Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.

Documentation

Overview

Package term provides structures and helper functions to work with terminal (state, sizes).

Index

Constants

This section is empty.

Variables

View Source
var ASCII = []string{
	"ctrl-@",
	"ctrl-a",
	"ctrl-b",
	"ctrl-c",
	"ctrl-d",
	"ctrl-e",
	"ctrl-f",
	"ctrl-g",
	"ctrl-h",
	"ctrl-i",
	"ctrl-j",
	"ctrl-k",
	"ctrl-l",
	"ctrl-m",
	"ctrl-n",
	"ctrl-o",
	"ctrl-p",
	"ctrl-q",
	"ctrl-r",
	"ctrl-s",
	"ctrl-t",
	"ctrl-u",
	"ctrl-v",
	"ctrl-w",
	"ctrl-x",
	"ctrl-y",
	"ctrl-z",
	"ctrl-[",
	"ctrl-\\",
	"ctrl-]",
	"ctrl-^",
	"ctrl-_",
}

ASCII list the possible supported ASCII key sequence

View Source
var ErrInvalidState = errors.New("Invalid terminal state")

ErrInvalidState is returned if the state of the terminal is invalid.

Deprecated: ErrInvalidState is no longer used.

Functions

func DisableEcho

func DisableEcho(fd uintptr, state *State) error

DisableEcho applies the specified state to the terminal connected to the file descriptor, with echo disabled.

func GetFdInfo

func GetFdInfo(in interface{}) (fd uintptr, isTerminal bool)

GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.

func IsTerminal

func IsTerminal(fd uintptr) bool

IsTerminal returns true if the given file descriptor is a terminal.

func NewEscapeProxy

func NewEscapeProxy(r io.Reader, escapeKeys []byte) io.Reader

NewEscapeProxy returns a new TTY proxy reader which wraps the given reader and detects when the specified escape keys are read, in which case the Read method will return an error of type EscapeError.

func RestoreTerminal

func RestoreTerminal(fd uintptr, state *State) error

RestoreTerminal restores the terminal connected to the given file descriptor to a previous state.

func SetWinsize

func SetWinsize(fd uintptr, ws *Winsize) error

SetWinsize tries to set the specified window size for the specified file descriptor. It is only implemented on Unix, and returns an error on Windows.

func StdStreams

func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer)

StdStreams returns the standard streams (stdin, stdout, stderr).

On Windows, it attempts to turn on VT handling on all std handles if supported, or falls back to terminal emulation. On Unix, this returns the standard os.Stdin, os.Stdout and os.Stderr.

func ToBytes

func ToBytes(keys string) ([]byte, error)

ToBytes converts a string representing a suite of key-sequence to the corresponding ASCII code.

Types

type EscapeError

type EscapeError struct{}

EscapeError is special error which returned by a TTY proxy reader's Read() method in case its detach escape sequence is read.

func (EscapeError) Error

func (EscapeError) Error() string

type State

type State terminalState

State holds the platform-specific state / console mode for the terminal.

func MakeRaw

func MakeRaw(fd uintptr) (previousState *State, err error)

MakeRaw puts the terminal (Windows Console) connected to the given file descriptor into raw mode and returns the previous state of the terminal so that it can be restored.

func SaveState

func SaveState(fd uintptr) (*State, error)

SaveState saves the state of the terminal connected to the given file descriptor.

func SetRawTerminal

func SetRawTerminal(fd uintptr) (previousState *State, err error)

SetRawTerminal puts the terminal connected to the given file descriptor into raw mode and returns the previous state. On UNIX, this is the equivalent of MakeRaw, and puts both the input and output into raw mode. On Windows, it only puts the input into raw mode.

func SetRawTerminalOutput

func SetRawTerminalOutput(fd uintptr) (previousState *State, err error)

SetRawTerminalOutput puts the output of terminal connected to the given file descriptor into raw mode. On UNIX, this does nothing and returns nil for the state. On Windows, it disables LF -> CRLF translation.

type Termios deprecated

type Termios = unix.Termios

Termios is the Unix API for terminal I/O.

Deprecated: use unix.Termios.

type Winsize

type Winsize struct {
	Height uint16
	Width  uint16
	// contains filtered or unexported fields
}

Winsize represents the size of the terminal window.

func GetWinsize

func GetWinsize(fd uintptr) (*Winsize, error)

GetWinsize returns the window size based on the specified file descriptor.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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