package cliimport ()// TTY is the type the terminal dependency of the editor needs to satisfy.typeTTYinterface {// Setup sets up the terminal for the CLI app. // // This method returns a restore function that undoes the setup, and any // error during setup. It only returns fatal errors that make the terminal // unsuitable for later operations; non-fatal errors may be reported by // showing a warning message, but not returned. // // This method should be called before any other method is called.Setup() (restore func(), err error)// ReadEvent reads a terminal event.ReadEvent() (term.Event, error)// SetRawInput requests the next n ReadEvent calls to read raw events. It // is applicable to environments where events are represented as a special // sequences, such as VT100. It is a no-op if events are delivered as whole // units by the terminal, such as Windows consoles.SetRawInput(n int)// CloseReader releases resources allocated for reading terminal events.CloseReader()term.Writer// NotifySignals start relaying signals and returns a channel on which // signals are delivered.NotifySignals() <-chanos.Signal// StopSignals stops the relaying of signals. After this function returns, // the channel returned by NotifySignals will no longer deliver signals.StopSignals()// Size returns the height and width of the terminal.Size() (h, w int)}typeaTTYstruct {in, out *os.Filerterm.Readerterm.WritersigChchanos.SignalrawMutexsync.Mutexrawint}// NewTTY returns a new TTY from input and output terminal files.func (, *os.File) TTY {return &aTTY{in: , out: , Writer: term.NewWriter()}}func ( *aTTY) () (func(), error) { , := term.Setup(.in, .out)returnfunc() { := ()if != nil {fmt.Println(.out, "failed to restore terminal properties:", ) } }, }func ( *aTTY) () (, int) {returnsys.GetWinsize(.out)}func ( *aTTY) () (term.Event, error) {if .r == nil { .r = term.NewReader(.in) }if .consumeRaw() {return .r.ReadRawEvent() }return .r.ReadEvent()}func ( *aTTY) () bool { .rawMutex.Lock()defer .rawMutex.Unlock()if .raw <= 0 {returnfalse } .raw--returntrue}func ( *aTTY) ( int) { .rawMutex.Lock()defer .rawMutex.Unlock() .raw = }func ( *aTTY) () {if .r != nil { .r.Close() } .r = nil}func ( *aTTY) () <-chanos.Signal { .sigCh = sys.NotifySignals()return .sigCh}func ( *aTTY) () {signal.Stop(.sigCh)close(.sigCh) .sigCh = nil}
The pages are generated with Goldsv0.2.8-preview. (GOOS=darwin GOARCH=arm64)