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