Revamp escape sequence processing for WSL
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Also add support for alt-[0-9] and f1[12]
authored by Junegunn Choi · committed by Junegunn Choi · parent 4b332d8 · Browse files · 4 files·+183 added−242 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+183 added−242 removed
- other:+183 added−242 removed
Provenance
- Commit
8c661d4- Tree
6e32a55- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4b332d8
4 files changed+183 added−242 removed
modified:src/tui/ncurses.go+159 added-236 removed
8 unmodified lines9 #cgo !static LDFLAGS: -lncurses10 #cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl11 #cgo android static LDFLAGS: -l:libncurses.a -fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch12 13-SCREEN *c_newterm () {14- return newterm(NULL, stderr, stdin);13+FILE* c_tty() {14+ return fopen("/dev/tty", "r");15 }16+17+SCREEN* c_newterm(FILE* tty) {18+ return newterm(NULL, stderr, tty);19+}20 */21 import "C"22 23 import (24 "fmt"25 "os"26 "strings"23- "syscall"27 "time"28 "unicode/utf8"29 )30 30 unmodified lines61 ColUser // Should be the last entry62 )63 64 var (62- _in *os.File65 _screen *C.SCREEN66 _colorMap map[int]ColorPair67 _colorFn func(ColorPair, Attr) C.int68 )13 unmodified lines82 return Default1683 }84 85 func Init(theme *ColorTheme, black bool, mouse bool) {84- {85- in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)86- if err != nil {87- panic("Failed to open /dev/tty")88- }89- _in = in90- // Break STDIN91- // syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))92- }93-86 C.setlocale(C.LC_ALL, C.CString(""))87+ tty := C.c_tty()88+ if tty == nil {89+ fmt.Println("Failed to open /dev/tty")90+ os.Exit(2)91+ }95- _screen = C.c_newterm()92+ _screen = C.c_newterm(tty)93 if _screen == nil {94 fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))95 os.Exit(2)96 }97 C.set_term(_screen)98 if mouse {99 C.mousemask(C.ALL_MOUSE_EVENTS, nil)100+ C.mouseinterval(0)101 }102 C.noecho()103 C.raw() // stty dsusp undef104+ C.nonl()105+ C.keypad(C.stdscr, true)106+ C.set_escdelay(200)107+ C.timeout(100) // ESCDELAY 200ms + timeout 100ms108 109 _color = theme != nil110 if _color {111 C.start_color()172 unmodified lines284 _colorMap[key] = id285 return id286 }287 286-func getch(nonblock bool) int {287- b := make([]byte, 1)288- syscall.SetNonblock(int(_in.Fd()), nonblock)289- _, err := _in.Read(b)290- if err != nil {291- return -1292- }293- return int(b[0])294-}295-296-func GetBytes() []byte {297- c := getch(false)298- retries := 0299- if c == 27 {300- // Wait for additional keys after ESC for 100ms (10 * 10ms)301- retries = 10302- }303- _buf = append(_buf, byte(c))304-305- for {306- c = getch(true)307- if c == -1 {308- if retries > 0 {309- retries--310- time.Sleep(10 * time.Millisecond)311- continue312- }313- break288+func consume(expects ...rune) bool {289+ for _, r := range expects {290+ if int(C.getch()) != int(r) {291+ return false292 }315- retries = 0316- _buf = append(_buf, byte(c))293 }318-319- return _buf294+ return true295 }296 322-// 27 (91 79) 77 type x y323-func mouseSequence(sz *int) Event {324- if len(_buf) < 6 {325- return Event{Invalid, 0, nil}326- }327- *sz = 6328- switch _buf[3] {329- case 32, 36, 40, 48, // mouse-down / shift / cmd / ctrl330- 35, 39, 43, 51: // mouse-up / shift / cmd / ctrl331- mod := _buf[3] >= 36332- down := _buf[3]%2 == 0333- x := int(_buf[4] - 33)334- y := int(_buf[5] - 33)335- double := false336- if down {337- now := time.Now()338- if now.Sub(_prevDownTime) < doubleClickDuration {339- _clickY = append(_clickY, y)340- } else {341- _clickY = []int{y}342- }343- _prevDownTime = now344- } else {345- if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&346- time.Now().Sub(_prevDownTime) < doubleClickDuration {347- double = true348- }349- }350- return Event{Mouse, 0, &MouseEvent{y, x, 0, down, double, mod}}351- case 96, 100, 104, 112, // scroll-up / shift / cmd / ctrl352- 97, 101, 105, 113: // scroll-down / shift / cmd / ctrl353- mod := _buf[3] >= 100354- s := 1 - int(_buf[3]%2)*2355- x := int(_buf[4] - 33)356- y := int(_buf[5] - 33)357- return Event{Mouse, 0, &MouseEvent{y, x, s, false, false, mod}}358- }359- return Event{Invalid, 0, nil}360-}361-362-func escSequence(sz *int) Event {363- if len(_buf) < 2 {297+func escSequence() Event {298+ // nodelay is not thread-safe (e.g. <ESC><CTRL-P>)299+ // C.nodelay(C.stdscr, true)300+ c := C.getch()301+ switch c {302+ case C.ERR:303 return Event{ESC, 0, nil}365- }366- *sz = 2367- switch _buf[1] {368- case 13:304+ case CtrlM:305 return Event{AltEnter, 0, nil}370- case 32:371- return Event{AltSpace, 0, nil}372- case 47:306+ case '/':307 return Event{AltSlash, 0, nil}374- case 98:375- return Event{AltB, 0, nil}376- case 100:377- return Event{AltD, 0, nil}378- case 102:379- return Event{AltF, 0, nil}380- case 127:308+ case ' ':309+ return Event{AltSpace, 0, nil}310+ case 127, C.KEY_BACKSPACE:311 return Event{AltBS, 0, nil}382- case 91, 79:383- if len(_buf) < 3 {312+ case '[':313+ // Bracketed paste mode (printf "\e[?2004h")314+ // \e[200~ TEXT \e[201~315+ if consume('2', '0', '0', '~') {316 return Event{Invalid, 0, nil}317 }386- *sz = 3387- switch _buf[2] {388- case 68:389- return Event{Left, 0, nil}390- case 67:391- return Event{Right, 0, nil}392- case 66:393- return Event{Down, 0, nil}394- case 65:395- return Event{Up, 0, nil}396- case 90:397- return Event{BTab, 0, nil}398- case 72:399- return Event{Home, 0, nil}400- case 70:401- return Event{End, 0, nil}402- case 77:403- return mouseSequence(sz)404- case 80:405- return Event{F1, 0, nil}406- case 81:407- return Event{F2, 0, nil}408- case 82:409- return Event{F3, 0, nil}410- case 83:411- return Event{F4, 0, nil}412- case 49, 50, 51, 52, 53, 54:413- if len(_buf) < 4 {414- return Event{Invalid, 0, nil}415- }416- *sz = 4417- switch _buf[2] {418- case 50:419- if len(_buf) == 5 && _buf[4] == 126 {420- *sz = 5421- switch _buf[3] {422- case 48:423- return Event{F9, 0, nil}424- case 49:425- return Event{F10, 0, nil}426- }427- }428- // Bracketed paste mode \e[200~ / \e[201429- if _buf[3] == 48 && (_buf[4] == 48 || _buf[4] == 49) && _buf[5] == 126 {430- *sz = 6431- return Event{Invalid, 0, nil}432- }433- return Event{Invalid, 0, nil} // INS434- case 51:435- return Event{Del, 0, nil}436- case 52:437- return Event{End, 0, nil}438- case 53:439- return Event{PgUp, 0, nil}440- case 54:441- return Event{PgDn, 0, nil}442- case 49:443- switch _buf[3] {444- case 126:445- return Event{Home, 0, nil}446- case 53, 55, 56, 57:447- if len(_buf) == 5 && _buf[4] == 126 {448- *sz = 5449- switch _buf[3] {450- case 53:451- return Event{F5, 0, nil}452- case 55:453- return Event{F6, 0, nil}454- case 56:455- return Event{F7, 0, nil}456- case 57:457- return Event{F8, 0, nil}458- }459- }460- return Event{Invalid, 0, nil}461- case 59:462- if len(_buf) != 6 {463- return Event{Invalid, 0, nil}464- }465- *sz = 6466- switch _buf[4] {467- case 50:468- switch _buf[5] {469- case 68:470- return Event{Home, 0, nil}471- case 67:472- return Event{End, 0, nil}473- }474- case 53:475- switch _buf[5] {476- case 68:477- return Event{SLeft, 0, nil}478- case 67:479- return Event{SRight, 0, nil}480- }481- } // _buf[4]482- } // _buf[3]483- } // _buf[2]484- } // _buf[2]485- } // _buf[1]486- if _buf[1] >= 'a' && _buf[1] <= 'z' {487- return Event{AltA + int(_buf[1]) - 'a', 0, nil}318 }319+ if c >= 'a' && c <= 'z' {489- return Event{Invalid, 0, nil}490-}320+ return Event{AltA + int(c) - 'a', 0, nil}321+ }322 492-func GetChar() Event {493- if len(_buf) == 0 {494- _buf = GetBytes()323+ if c >= '0' && c <= '9' {324+ return Event{Alt0 + int(c) - '0', 0, nil}325 }496- if len(_buf) == 0 {497- panic("Empty _buffer")326+327+ // Don't care. Ignore the rest.328+ for ; c != C.ERR; c = C.getch() {329 }330+ return Event{Invalid, 0, nil}331+}332 500- sz := 1501- defer func() {502- _buf = _buf[sz:]503- }()504-505- switch _buf[0] {506- case CtrlC:507- return Event{CtrlC, 0, nil}508- case CtrlG:509- return Event{CtrlG, 0, nil}510- case CtrlQ:511- return Event{CtrlQ, 0, nil}512- case 127:333+func GetChar() Event {334+ c := C.getch()335+ switch c {336+ case C.ERR:337+ return Event{Invalid, 0, nil}338+ case C.KEY_UP:339+ return Event{Up, 0, nil}340+ case C.KEY_DOWN:341+ return Event{Down, 0, nil}342+ case C.KEY_LEFT:343+ return Event{Left, 0, nil}344+ case C.KEY_RIGHT:345+ return Event{Right, 0, nil}346+ case C.KEY_HOME:347+ return Event{Home, 0, nil}348+ case C.KEY_END:349+ return Event{End, 0, nil}350+ case C.KEY_BACKSPACE:351 return Event{BSpace, 0, nil}352+ case C.KEY_F0 + 1:353+ return Event{F1, 0, nil}354+ case C.KEY_F0 + 2:355+ return Event{F2, 0, nil}356+ case C.KEY_F0 + 3:357+ return Event{F3, 0, nil}358+ case C.KEY_F0 + 4:359+ return Event{F4, 0, nil}360+ case C.KEY_F0 + 5:361+ return Event{F5, 0, nil}362+ case C.KEY_F0 + 6:363+ return Event{F6, 0, nil}364+ case C.KEY_F0 + 7:365+ return Event{F7, 0, nil}366+ case C.KEY_F0 + 8:367+ return Event{F8, 0, nil}368+ case C.KEY_F0 + 9:369+ return Event{F9, 0, nil}370+ case C.KEY_F0 + 10:371+ return Event{F10, 0, nil}372+ case C.KEY_F0 + 11:373+ return Event{F11, 0, nil}374+ case C.KEY_F0 + 12:375+ return Event{F12, 0, nil}376+ case C.KEY_DC:377+ return Event{Del, 0, nil}378+ case C.KEY_PPAGE:379+ return Event{PgUp, 0, nil}380+ case C.KEY_NPAGE:381+ return Event{PgDn, 0, nil}382+ case C.KEY_BTAB:383+ return Event{BTab, 0, nil}384+ case C.KEY_ENTER:385+ return Event{CtrlM, 0, nil}386+ case C.KEY_SLEFT:387+ return Event{SLeft, 0, nil}388+ case C.KEY_SRIGHT:389+ return Event{SRight, 0, nil}390+ case C.KEY_MOUSE:391+ var me C.MEVENT392+ if C.getmouse(&me) != C.ERR {393+ mod := ((me.bstate & C.BUTTON_SHIFT) | (me.bstate & C.BUTTON_CTRL) | (me.bstate & C.BUTTON_ALT)) > 0394+ x := int(me.x)395+ y := int(me.y)396+ /* Cannot use BUTTON1_DOUBLE_CLICKED due to mouseinterval(0) */397+ if (me.bstate & C.BUTTON1_PRESSED) > 0 {398+ now := time.Now()399+ if now.Sub(_prevDownTime) < doubleClickDuration {400+ _clickY = append(_clickY, y)401+ } else {402+ _clickY = []int{y}403+ _prevDownTime = now404+ }405+ return Event{Mouse, 0, &MouseEvent{y, x, 0, true, false, mod}}406+ } else if (me.bstate & C.BUTTON1_RELEASED) > 0 {407+ double := false408+ if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&409+ time.Now().Sub(_prevDownTime) < doubleClickDuration {410+ double = true411+ }412+ return Event{Mouse, 0, &MouseEvent{y, x, 0, false, double, mod}}413+ } else if (me.bstate&0x8000000) > 0 || (me.bstate&0x80) > 0 {414+ return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, mod}}415+ } else if (me.bstate & C.BUTTON4_PRESSED) > 0 {416+ return Event{Mouse, 0, &MouseEvent{y, x, 1, false, false, mod}}417+ }418+ }419+ return Event{Invalid, 0, nil}420+ case C.KEY_RESIZE:421+ return Event{Invalid, 0, nil}422 case ESC:515- return escSequence(&sz)423+ return escSequence()424+ case 127:425+ return Event{BSpace, 0, nil}426 }517-427 // CTRL-A ~ CTRL-Z519- if _buf[0] <= CtrlZ {520- return Event{int(_buf[0]), 0, nil}428+ if c >= CtrlA && c <= CtrlZ {429+ return Event{int(c), 0, nil}430 }431+432+ // Multi-byte character433+ buffer := []byte{byte(c)}434+ for {522- r, rsz := utf8.DecodeRune(_buf)523- if r == utf8.RuneError {524- return Event{ESC, 0, nil}435+ r, _ := utf8.DecodeRune(buffer)436+ if r != utf8.RuneError {437+ return Event{Rune, r, nil}438+ }439+440+ c := C.getch()441+ if c == C.ERR {442+ break443+ }444+ if c >= C.KEY_CODE_YES {445+ C.ungetch(c)446+ break447+ }448+ buffer = append(buffer, byte(c))449 }526- sz = rsz527- return Event{Rune, r, nil}450+ return Event{Invalid, 0, nil}451 }modified:src/tui/tcell.go+6 added-3 removed
205 unmodified lines206 if now.Sub(_prevDownTime) < doubleClickDuration {207 _clickY = append(_clickY, x)208 } else {209 _clickY = []int{x}210+ _prevDownTime = now211 }211- _prevDownTime = now212 } else {213 if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&214 time.Now().Sub(_prevDownTime) < doubleClickDuration {215 double = true109 unmodified lines325 return Event{F9, 0, nil}326 case tcell.KeyF10:327 return Event{F10, 0, nil}328 case tcell.KeyF11:329- return Event{Invalid, 0, nil}329+ return Event{F11, 0, nil}330 case tcell.KeyF12:331- return Event{Invalid, 0, nil}331+ return Event{F12, 0, nil}332 333 // ev.Ch doesn't work for some reason for space:334 case tcell.KeyRune:335 r := ev.Rune()6 unmodified lines342 }343 if r >= 'a' && r <= 'z' {344 return Event{AltA + int(r) - 'a', 0, nil}345 }346+ if r >= '0' && r <= '9' {347+ return Event{Alt0 + int(r) - '0', 0, nil}348+ }349 }350 return Event{Rune, r, nil}351 352 case tcell.KeyEsc:226 unmodified linesmodified:src/tui/tui.go+8 added-3 removed
65 unmodified lines66 F767 F868 F969 F1070+ F1171+ F1272 73 AltEnter74 AltSpace75 AltSlash76 AltBS77+75- AltA78+ Alt079+)80+81+const ( // Reset iota82+ AltA = Alt0 + 'a' - '0' + iota83 AltB84 AltC85 AltD86 AltE87 AltF81-88 AltZ = AltA + 'z' - 'a'89 )90 91 const (49 unmodified lines141 Mod bool142 }143 144 var (139- _buf []byte145 _color bool146 _prevDownTime time.Time147 _clickY []int148 Default16 *ColorTheme107 unmodified linesmodified:src/options.go+10 added
328 unmodified lines329 func isAlphabet(char uint8) bool {330 return char >= 'a' && char <= 'z'331 }332 333+func isNumeric(char uint8) bool {334+ return char >= '0' && char <= '9'335+}336+337 func parseAlgo(str string) algo.Algo {338 switch str {339 case "v1":340 return algo.FuzzyMatchV168 unmodified lines409 case "double-click":410 chord = tui.DoubleClick411 case "f10":412 chord = tui.F10413+ case "f11":414+ chord = tui.F11415+ case "f12":416+ chord = tui.F12417 default:418 if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {419 chord = tui.CtrlA + int(lkey[5]) - 'a'420 } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {421 chord = tui.AltA + int(lkey[4]) - 'a'422+ } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isNumeric(lkey[4]) {423+ chord = tui.Alt0 + int(lkey[4]) - '0'424 } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '9' {425 chord = tui.F1 + int(key[1]) - '1'426 } else if utf8.RuneCountInString(key) == 1 {427 chord = tui.AltZ + int([]rune(key)[0])709 unmodified lines