Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | // 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. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 5 | // 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. |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 9 | package terminal |
| 10 | |
| 11 | import ( |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 12 | "io" |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 13 | |
| 14 | "golang.org/x/term" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | // EscapeCodes contains escape sequences that can be written to the terminal in |
| 18 | // order to achieve different styles of text. |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 19 | type EscapeCodes = term.EscapeCodes |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 20 | |
| 21 | // Terminal contains the state for running a VT100 terminal that is capable of |
| 22 | // reading lines of input. |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 23 | type Terminal = term.Terminal |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 24 | |
| 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 | // "> "). |
| 29 | func NewTerminal(c io.ReadWriter, prompt string) *Terminal { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 30 | return term.NewTerminal(c, prompt) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 31 | } |
| 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. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 37 | var ErrPasteIndicator = term.ErrPasteIndicator |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 38 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 39 | // State contains the state of a terminal. |
| 40 | type State = term.State |
| 41 | |
| 42 | // IsTerminal returns whether the given file descriptor is a terminal. |
| 43 | func IsTerminal(fd int) bool { |
| 44 | return term.IsTerminal(fd) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 47 | // 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. |
| 50 | func ReadPassword(fd int) ([]byte, error) { |
| 51 | return term.ReadPassword(fd) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 52 | } |
| 53 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 54 | // 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. |
| 57 | func MakeRaw(fd int) (*State, error) { |
| 58 | return term.MakeRaw(fd) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 61 | // Restore restores the terminal connected to the given file descriptor to a |
| 62 | // previous state. |
| 63 | func Restore(fd int, oldState *State) error { |
| 64 | return term.Restore(fd, oldState) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 65 | } |
| 66 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 67 | // GetState returns the current state of a terminal which may be useful to |
| 68 | // restore the terminal after a signal. |
| 69 | func GetState(fd int) (*State, error) { |
| 70 | return term.GetState(fd) |
| 71 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 72 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 73 | // GetSize returns the dimensions of the given terminal. |
| 74 | func GetSize(fd int) (width, height int, err error) { |
| 75 | return term.GetSize(fd) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 76 | } |