blob: a4d1919a9e7d649e59e68747af9053b77f6e94da [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
David K. Bainbridge06631892021-08-19 13:07:00 +00005// Package terminal provides support functions for dealing with terminals, as
6// commonly found on UNIX systems.
7//
8// Deprecated: this package moved to golang.org/x/term.
Matteo Scandoloa4285862020-12-01 18:10:10 -08009package terminal
10
11import (
Matteo Scandoloa4285862020-12-01 18:10:10 -080012 "io"
David K. Bainbridge06631892021-08-19 13:07:00 +000013
14 "golang.org/x/term"
Matteo Scandoloa4285862020-12-01 18:10:10 -080015)
16
17// EscapeCodes contains escape sequences that can be written to the terminal in
18// order to achieve different styles of text.
David K. Bainbridge06631892021-08-19 13:07:00 +000019type EscapeCodes = term.EscapeCodes
Matteo Scandoloa4285862020-12-01 18:10:10 -080020
21// Terminal contains the state for running a VT100 terminal that is capable of
22// reading lines of input.
David K. Bainbridge06631892021-08-19 13:07:00 +000023type Terminal = term.Terminal
Matteo Scandoloa4285862020-12-01 18:10:10 -080024
25// NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is
26// a local terminal, that terminal must first have been put into raw mode.
27// prompt is a string that is written at the start of each input line (i.e.
28// "> ").
29func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
David K. Bainbridge06631892021-08-19 13:07:00 +000030 return term.NewTerminal(c, prompt)
Matteo Scandoloa4285862020-12-01 18:10:10 -080031}
32
33// ErrPasteIndicator may be returned from ReadLine as the error, in addition
34// to valid line data. It indicates that bracketed paste mode is enabled and
35// that the returned line consists only of pasted data. Programs may wish to
36// interpret pasted data more literally than typed data.
David K. Bainbridge06631892021-08-19 13:07:00 +000037var ErrPasteIndicator = term.ErrPasteIndicator
Matteo Scandoloa4285862020-12-01 18:10:10 -080038
David K. Bainbridge06631892021-08-19 13:07:00 +000039// State contains the state of a terminal.
40type State = term.State
41
42// IsTerminal returns whether the given file descriptor is a terminal.
43func IsTerminal(fd int) bool {
44 return term.IsTerminal(fd)
Matteo Scandoloa4285862020-12-01 18:10:10 -080045}
46
David K. Bainbridge06631892021-08-19 13:07:00 +000047// ReadPassword reads a line of input from a terminal without local echo. This
48// is commonly used for inputting passwords and other sensitive data. The slice
49// returned does not include the \n.
50func ReadPassword(fd int) ([]byte, error) {
51 return term.ReadPassword(fd)
Matteo Scandoloa4285862020-12-01 18:10:10 -080052}
53
David K. Bainbridge06631892021-08-19 13:07:00 +000054// MakeRaw puts the terminal connected to the given file descriptor into raw
55// mode and returns the previous state of the terminal so that it can be
56// restored.
57func MakeRaw(fd int) (*State, error) {
58 return term.MakeRaw(fd)
Matteo Scandoloa4285862020-12-01 18:10:10 -080059}
60
David K. Bainbridge06631892021-08-19 13:07:00 +000061// Restore restores the terminal connected to the given file descriptor to a
62// previous state.
63func Restore(fd int, oldState *State) error {
64 return term.Restore(fd, oldState)
Matteo Scandoloa4285862020-12-01 18:10:10 -080065}
66
David K. Bainbridge06631892021-08-19 13:07:00 +000067// GetState returns the current state of a terminal which may be useful to
68// restore the terminal after a signal.
69func GetState(fd int) (*State, error) {
70 return term.GetState(fd)
71}
Matteo Scandoloa4285862020-12-01 18:10:10 -080072
David K. Bainbridge06631892021-08-19 13:07:00 +000073// GetSize returns the dimensions of the given terminal.
74func GetSize(fd int) (width, height int, err error) {
75 return term.GetSize(fd)
Matteo Scandoloa4285862020-12-01 18:10:10 -080076}