1 package term 2 3 import "io" 4 5 // State holds the platform-specific state / console mode for the terminal. 6 type State terminalState 7 8 // Winsize represents the size of the terminal window. 9 type Winsize struct { 10 Height uint16 11 Width uint16 12 13 // Only used on Unix 14 x uint16 15 y uint16 16 } 17 18 // StdStreams returns the standard streams (stdin, stdout, stderr). 19 // 20 // On Windows, it attempts to turn on VT handling on all std handles if 21 // supported, or falls back to terminal emulation. On Unix, this returns 22 // the standard [os.Stdin], [os.Stdout] and [os.Stderr]. 23 func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { 24 return stdStreams() 25 } 26 27 // GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal. 28 func GetFdInfo(in interface{}) (fd uintptr, isTerminal bool) { 29 return getFdInfo(in) 30 } 31 32 // GetWinsize returns the window size based on the specified file descriptor. 33 func GetWinsize(fd uintptr) (*Winsize, error) { 34 return getWinsize(fd) 35 } 36 37 // SetWinsize tries to set the specified window size for the specified file 38 // descriptor. It is only implemented on Unix, and returns an error on Windows. 39 func SetWinsize(fd uintptr, ws *Winsize) error { 40 return setWinsize(fd, ws) 41 } 42 43 // IsTerminal returns true if the given file descriptor is a terminal. 44 func IsTerminal(fd uintptr) bool { 45 return isTerminal(fd) 46 } 47 48 // RestoreTerminal restores the terminal connected to the given file descriptor 49 // to a previous state. 50 func RestoreTerminal(fd uintptr, state *State) error { 51 return restoreTerminal(fd, state) 52 } 53 54 // SaveState saves the state of the terminal connected to the given file descriptor. 55 func SaveState(fd uintptr) (*State, error) { 56 return saveState(fd) 57 } 58 59 // DisableEcho applies the specified state to the terminal connected to the file 60 // descriptor, with echo disabled. 61 func DisableEcho(fd uintptr, state *State) error { 62 return disableEcho(fd, state) 63 } 64 65 // SetRawTerminal puts the terminal connected to the given file descriptor into 66 // raw mode and returns the previous state. On UNIX, this is the equivalent of 67 // [MakeRaw], and puts both the input and output into raw mode. On Windows, it 68 // only puts the input into raw mode. 69 func SetRawTerminal(fd uintptr) (previousState *State, err error) { 70 return setRawTerminal(fd) 71 } 72 73 // SetRawTerminalOutput puts the output of terminal connected to the given file 74 // descriptor into raw mode. On UNIX, this does nothing and returns nil for the 75 // state. On Windows, it disables LF -> CRLF translation. 76 func SetRawTerminalOutput(fd uintptr) (previousState *State, err error) { 77 return setRawTerminalOutput(fd) 78 } 79 80 // MakeRaw puts the terminal (Windows Console) connected to the 81 // given file descriptor into raw mode and returns the previous state of 82 // the terminal so that it can be restored. 83 func MakeRaw(fd uintptr) (previousState *State, err error) { 84 return makeRaw(fd) 85 } 86