package uvimport ()varisWindows = runtime.GOOS == "windows"// File is an interface that represents a file with a file descriptor.//// This is typically an [os.File] like [os.Stdin] and [os.Stdout].typeFileinterface {io.ReadWriteCloserterm.File// Name returns the name of the file.Name() string}// Winsize represents the size of a terminal in cells and pixels.//// This is the same as [unix.Winsize], but defined here for cross-platform compatibility.typeWinsizestruct {Rowuint16Coluint16Xpixeluint16Ypixeluint16}// Console represents a cross-platform console I/O interface.typeConsoleinterface {io.ReadWriteCloser// Environ returns the console's environment variables.Environ() []string// Getenv retrieves the value of the environment variable named by the key.Getenv(key string) string// LookupEnv retrieves the value of the environment variable named by the key // and a boolean indicating whether the variable is present.LookupEnv(key string) (string, bool)// Reader returns the input reader of the console.Reader() io.Reader// Writer returns the output writer of the console.Writer() io.Writer// MakeRaw puts the console input side into raw mode.MakeRaw() (state *term.State, err error)// Restore restores the console to its previous state.Restore() error// GetSize returns the current size of the console.GetSize() (width, height int, err error)// GetWinsize returns the current size of the console in cells and pixels.GetWinsize() (*Winsize, error)}// TTY represents a Unix TTY device. It implements the [Console] interface.typeTTYstruct { *console}var _ Console = (*TTY)(nil)// WinCon represents a Windows Console. It implements the [Console] interface.typeWinConstruct { *console}var _ Console = (*WinCon)(nil)// Console is a cross-platform console I/O.typeconsolestruct {inputFileinputState *term.StateoutputFileoutputState *term.StateenvironEnviron}// DefaultConsole returns a new default console instance that uses standard I/O// [os.Stdin], [os.Stdout], and [os.Environ].//// To use [os.Stderr] as the output, you can create a new console with// [NewConsole] and pass [os.Stderr] as the output parameter.func () Console {returnNewConsole(os.Stdin, os.Stdout, os.Environ())}// ControllingConsole returns a new console instance that uses the current// controlling terminal's input and output file descriptors.func () (Console, error) { , , := OpenTTY()if != nil {returnnil, }returnNewConsole(, , os.Environ()), nil}// NewConsole creates a new [Console] with the given input, output, and// environment variables.//// You can use [OpenTTY] to open the current controlling console files and pass// them to this function. Use [ControllingConsole] for a convenience function// that does this for you.//// Use this to create a new terminal for PTY processes by passing the PTY slave// file as the input and output and any environment variables the process// needs.func (, File, []string) Console {if == nil { = os.Stdin }if == nil { = os.Stdout }if == nil { = os.Environ() }returnnewConsole(, , )}// Environ returns the console's environment variables.func ( *console) () []string {return .environ}// Writer returns the output writer of the console.func ( *console) () io.Writer {return .output}// Reader returns the input reader of the console.func ( *console) () io.Reader {return .input}// Write writes data to the console's output.func ( *console) ( []byte) ( int, error) {return .output.Write()}// Read reads data from the console's input.func ( *console) ( []byte) ( int, error) {return .input.Read()}// Getenv retrieves the value of the environment variable named by the key.func ( *console) ( string) string {return .environ.Getenv()}// LookupEnv retrieves the value of the environment variable named by the key// and a boolean indicating whether the variable is present.func ( *console) ( string) (string, bool) {return .environ.LookupEnv()}// MakeRaw puts the console input side into raw mode.func ( *console) () ( *term.State, error) { , , := makeRaw(.input, .output)if != nil {returnnil, } .inputState = .outputState = if != nil {return , nil }return , nil}// Restore restores the console to its previous state.func ( *console) () error {if .inputState != nil {if := term.Restore(.input.Fd(), .inputState); != nil {return } .inputState = nil }if .outputState != nil {if := term.Restore(.output.Fd(), .outputState); != nil {return } .outputState = nil }returnnil}// Close restores the console to its previous state and releases resources.func ( *console) () error {if := .Restore(); != nil {return }returnnil}// GetSize returns the current size of the console.func ( *console) () (, int, error) {returngetSize(.input, .output)}// GetWinsize returns the current size of the console in cells and pixels.func ( *console) () (*Winsize, error) { , := getWinsize(.input, .output)if != nil {returnnil, }return &, nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)