David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 5 | // Package term provides support functions for dealing with terminals, as |
| 6 | // commonly found on UNIX systems. |
| 7 | // |
| 8 | // Putting a terminal into raw mode is the most common requirement: |
| 9 | // |
| 10 | // oldState, err := terminal.MakeRaw(0) |
| 11 | // if err != nil { |
| 12 | // panic(err) |
| 13 | // } |
| 14 | // defer terminal.Restore(0, oldState) |
| 15 | package term |
| 16 | |
| 17 | // State contains the state of a terminal. |
| 18 | type State struct { |
| 19 | state |
| 20 | } |
| 21 | |
| 22 | // IsTerminal returns whether the given file descriptor is a terminal. |
| 23 | func IsTerminal(fd int) bool { |
| 24 | return isTerminal(fd) |
| 25 | } |
| 26 | |
| 27 | // MakeRaw puts the terminal connected to the given file descriptor into raw |
| 28 | // mode and returns the previous state of the terminal so that it can be |
| 29 | // restored. |
| 30 | func MakeRaw(fd int) (*State, error) { |
| 31 | return makeRaw(fd) |
| 32 | } |
| 33 | |
| 34 | // GetState returns the current state of a terminal which may be useful to |
| 35 | // restore the terminal after a signal. |
| 36 | func GetState(fd int) (*State, error) { |
| 37 | return getState(fd) |
| 38 | } |
| 39 | |
| 40 | // Restore restores the terminal connected to the given file descriptor to a |
| 41 | // previous state. |
| 42 | func Restore(fd int, oldState *State) error { |
| 43 | return restore(fd, oldState) |
| 44 | } |
| 45 | |
| 46 | // GetSize returns the visible dimensions of the given terminal. |
| 47 | // |
| 48 | // These dimensions don't include any scrollback buffer height. |
| 49 | func GetSize(fd int) (width, height int, err error) { |
| 50 | return getSize(fd) |
| 51 | } |
| 52 | |
| 53 | // ReadPassword reads a line of input from a terminal without local echo. This |
| 54 | // is commonly used for inputting passwords and other sensitive data. The slice |
| 55 | // returned does not include the \n. |
| 56 | func ReadPassword(fd int) ([]byte, error) { |
| 57 | return readPassword(fd) |
| 58 | } |