Skip to content
Myr

Use SGR mouse mode for larger terminals

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

Fix #2840

authored by Junegunn Choi · committed by Junegunn Choi · parent 3b7a962 · Browse files · 2 files+69 added−35 removed
This commit

State

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

Diff stats

2 files+69 added−35 removed

  • other:+69 added−35 removed

2 files+69 added−35 removed

modified:src/tui/light.go+64 added-35 removed
174 unmodified lines
175 }176177 if r.mouse {178 r.csi("?1000h")179 r.csi("?1006h")180 }181 r.csi(fmt.Sprintf("%dA", r.MaxY()-1))182 r.csi("G")183 r.csi("K")
194 unmodified lines
378 case 'H':379 return Event{Home, 0, nil}380 case 'F':381 return Event{End, 0, nil}381 case 'M':382 case '<':383 return r.mouseSequence(sz)384 case 'P':385 return Event{F1, 0, nil}386 case 'Q':
132 unmodified lines
519 }520 return Event{Invalid, 0, nil}521}522523// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking524func (r *LightRenderer) mouseSequence(sz *int) Event {525 // "\e[<0;0;0M"523 if len(r.buffer) < 6 || !r.mouse {526 if len(r.buffer) < 9 || !r.mouse {527 return Event{Invalid, 0, nil}528 }526 *sz = 6527 switch r.buffer[3] {528 case 32, 34, 36, 40, 48, // mouse-down / shift / cmd / ctrl529 35, 39, 43, 51: // mouse-up / shift / cmd / ctrl530 mod := r.buffer[3] >= 36531 left := r.buffer[3] == 32532 down := r.buffer[3]%2 == 0533 x := int(r.buffer[4] - 33)534 y := int(r.buffer[5]-33) - r.yoffset535 double := false536 if down {537 now := time.Now()538 if !left { // Right double click is not allowed539 r.clickY = []int{}540 } else if now.Sub(r.prevDownTime) < doubleClickDuration {541 r.clickY = append(r.clickY, y)542 } else {543 r.clickY = []int{y}544 }545 r.prevDownTime = now529530 rest := r.buffer[*sz:]531 end := bytes.IndexAny(rest, "mM")532 if end == -1 {533 return Event{Invalid, 0, nil}534 }535536 elems := strings.SplitN(string(rest[:end]), ";", 3)537 if len(elems) != 3 {538 return Event{Invalid, 0, nil}539 }540541 t := atoi(elems[0], -1)542 x := atoi(elems[1], -1) - 1543 y := atoi(elems[2], -1) - 1544 if t < 0 || x < 0 || y < 0 {545 return Event{Invalid, 0, nil}546 }547 *sz += end + 1548549 down := rest[end] == 'M'550551 scroll := 0552 if t >= 64 {553 t -= 64554 if t&0b1 == 1 {555 scroll = -1556 } else {547 if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] &&548 time.Since(r.prevDownTime) < doubleClickDuration {549 double = true550 }557 scroll = 1558 }559 }560553 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}}554 case 96, 100, 104, 112, // scroll-up / shift / cmd / ctrl555 97, 101, 105, 113: // scroll-down / shift / cmd / ctrl556 mod := r.buffer[3] >= 100557 s := 1 - int(r.buffer[3]%2)*2558 x := int(r.buffer[4] - 33)559 y := int(r.buffer[5]-33) - r.yoffset560 return Event{Mouse, 0, &MouseEvent{y, x, s, false, false, false, mod}}561 // middle := t & 0b1562 left := t&0b11 == 0563564 // shift := t & 0b100565 // ctrl := t & 0b1000566 mod := t&0b1100 > 0567568 if scroll != 0 {569 return Event{Mouse, 0, &MouseEvent{y, x, scroll, false, false, false, mod}}570 }562 return Event{Invalid, 0, nil}571572 double := false573 if down {574 now := time.Now()575 if !left { // Right double click is not allowed576 r.clickY = []int{}577 } else if now.Sub(r.prevDownTime) < doubleClickDuration {578 r.clickY = append(r.clickY, y)579 } else {580 r.clickY = []int{y}581 }582 r.prevDownTime = now583 } else {584 if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] &&585 time.Since(r.prevDownTime) < doubleClickDuration {586 double = true587 }588 }589 return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}}590}591592func (r *LightRenderer) smcup() {593 r.csi("?1049h")
29 unmodified lines
623 // NOTE: SIGCONT (Coming back from CTRL-Z):624 // It's highly likely that the offset we obtained at the beginning is625 // no longer correct, so we simply disable mouse input.626 r.csi("?1000l")627 r.csi("?1006l")628 r.mouse = false629 }630}631
31 unmodified lines
663 r.csi("u")664 }665 if r.mouse {666 r.csi("?1000l")667 r.csi("?1006l")668 }669 r.flush()670 r.closePlatform()671 r.restoreTerminal()
345 unmodified lines
modified:CHANGELOG.md+5 added
1CHANGELOG2=========340.31.05------6- Use SGR mouse mode to support larger terminals7- Bug fixes and improvements890.30.010------11- Fixed cursor flickering over the screen by hiding it during rendering12- Added `--ellipsis` option. You can take advantage of it to make fzf
1214 unmodified lines