//go:build unixpackage coreimport ()// GetCursorPos returns the current cursor position in the terminal.// It is safe to call this function even if the shell is reading input.func ( *Keys) () (, int) { := func() (int, int) {os.Stderr.WriteString("\r\ngetCursorPos() not supported by terminal emulator, disabling....\r\n")return -1, -1 }var []bytevar [][]string// Echo the query and wait for the main key // reading routine to send us the response back.fmt.Print("\x1b[6n")// In order not to get stuck with an input that might be user-one // (like when the user typed before the shell is fully started, and yet not having // queried cursor yet), we keep reading from stdin until we find the cursor response. // Everything else is passed back as user input.for {switch {case .waiting, .reading: = <-.cursordefault: := make([]byte, keyScanBufSize) , := os.Stdin.Read()if != nil {return () } = [:] }// We have read (or have been passed) something.iflen() == 0 {return () }// Attempt to locate cursor response in it. = rxRcvCursorPos.FindAllStringSubmatch(string(), 1)// If there is something but not cursor answer, its user input.iflen() == 0 && len() > 0 { .mutex.RLock() .buf = append(.buf, ...) .mutex.RUnlock()continue }// And if empty, then we should abort.iflen() == 0 {return () }break }// We know that we have a cursor answer, process it. , := strconv.Atoi([0][1])if != nil {return () } , = strconv.Atoi([0][2])if != nil {return () }return , }func ( *Keys) () ( []byte, error) {// Start reading from os.Stdin in the background. // We will either read keys from user, or an EOF // send by ourselves, because we pause reading. := make([]byte, keyScanBufSize) , := Stdin.Read()if != nil && errors.Is(, io.EOF) {return }// Always attempt to extract cursor position info. // If found, strip it and keep the remaining keys. , := .extractCursorPos([:])iflen() > 0 { .cursor <- }return , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.