Skip to content
Myr

Cursor postition response can be preceded by user key strokes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 12a43b5 · Browse files · 1 file+15 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−15 removed

  • other:+15 added−15 removed

1 file+15 added−15 removed

modified:src/tui/light.go+15 added-15 removed
2 unmodified lines
3import (4 "fmt"5 "os"6 "os/exec"7 "regexp"8 "strconv"9 "strings"10 "syscall"11 "time"
8 unmodified lines
20 defaultWidth = 8021 defaultHeight = 242223 escPollInterval = 524 offsetPollTries = 1025)2627const consoleDevice string = "/dev/tty"2829var offsetRegexp *regexp.Regexp = regexp.MustCompile("\x1b\\[([0-9]+);([0-9]+)R")3031func openTtyIn() *os.File {32 in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)33 if err != nil {34 panic("Failed to open " + consoleDevice)
101 unmodified lines
136137func (r *LightRenderer) findOffset() (row int, col int) {138 r.csi("6n")139 r.flush()136 bytes := r.getBytesInternal([]byte{})137138 // ^[[*;*R139 if len(bytes) > 5 && bytes[0] == 27 && bytes[1] == 91 && bytes[len(bytes)-1] == 'R' {140 nums := strings.Split(string(bytes[2:len(bytes)-1]), ";")141 if len(nums) == 2 {142 return atoi(nums[0], 0) - 1, atoi(nums[1], 0) - 1140 bytes := []byte{}141 for tries := 0; tries < offsetPollTries; tries++ {142 bytes = r.getBytesInternal(bytes, tries > 0)143 offsets := offsetRegexp.FindSubmatch(bytes)144 if len(offsets) > 2 {145 return atoi(string(offsets[1]), 0) - 1, atoi(string(offsets[2]), 0) - 1146 }144 return -1, -1147 }146147 // No idea148 return -1, -1149}150151func repeat(s string, times int) string {
121 unmodified lines
273 return int(b[0]), true274}275276func (r *LightRenderer) getBytes() []byte {277 return r.getBytesInternal(r.buffer)277 return r.getBytesInternal(r.buffer, false)278}279280func (r *LightRenderer) getBytesInternal(buffer []byte) []byte {281 c, ok := r.getch(false)282 if !ok {280func (r *LightRenderer) getBytesInternal(buffer []byte, nonblock bool) []byte {281 c, ok := r.getch(nonblock)282 if !nonblock && !ok {283 r.Close()284 errorExit("Failed to read " + consoleDevice)285 }286287 retries := 0288 if c == ESC {288 if c == ESC || nonblock {289 retries = r.escDelay / escPollInterval290 }291 buffer = append(buffer, byte(c))292
570 unmodified lines