feat: extended Coordinates mouse reporting & additional buttons support (#594)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(mouse): add extended mouse & shift key support Support SGR(1006) mouse mode Support parsing shift key press Support additional mouse buttons Report which button was released Report button motion * fix: key.go sgr len missing calculation (#841) * chore(test): add sgr mouse msg detect test ---------
authored by Ayman Bagabas · committed by GitHub · parent 2bcb0af · Browse files · 13 files·+1052 added−229 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: robinsamuel <96998379+robin-samuel@users.noreply.github.com>
Diff stats
13 files+1052 added−229 removed
- other:+1052 added−229 removed
Provenance
- Commit
a154847- Tree
3244a4d- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2bcb0af
13 files changed+1052 added−229 removed
modified:examples/mouse/main.go+3 added-11 removed
2 unmodified lines3 // A simple program that opens the alternate screen buffer and displays mouse4 // coordinates and events.5 6 import (7- "fmt"7 "log"8 9 tea "github.com/charmbracelet/bubbletea"10 )11 12 func main() {14- p := tea.NewProgram(model{}, tea.WithAltScreen(), tea.WithMouseAllMotion())13+ p := tea.NewProgram(model{}, tea.WithMouseAllMotion())14 if _, err := p.Run(); err != nil {15 log.Fatal(err)16 }17 }18 19 type model struct {21- init bool20 mouseEvent tea.MouseEvent21 }22 23 func (m model) Init() tea.Cmd {7 unmodified lines31 return m, tea.Quit32 }33 34 case tea.MouseMsg:37- m.init = true38- m.mouseEvent = tea.MouseEvent(msg)35+ return m, tea.Printf("(X: %d, Y: %d) %s", msg.X, msg.Y, tea.MouseEvent(msg))36 }37 38 return m, nil39 }40 41 func (m model) View() string {45- s := "Do mouse stuff. When you're done press q to quit.\n\n"42+ s := "Do mouse stuff. When you're done press q to quit.\n"43 47- if m.init {48- e := m.mouseEvent49- s += fmt.Sprintf("(X: %d, Y: %d) %s", e.X, e.Y, e)50- }51-44 return s45 }modified:examples/simple/testdata/TestApp.golden+1 added-1 removed
1 [?25lHi. This program will exit in 10 seconds. To quit sooner press any key2 [70D[1A[70D[2KHi. This program will exit in 9 seconds. To quit sooner press any key.3-[70D[2K[?25h[?1002l[?1003l3+[70D[2K[?25h[?1002l[?1003l[?1006lmodified:key_test.go+22 added-11 removed
135 unmodified lines136 td = append(td,137 // Mouse event.138 seqTest{139 []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},140- MouseMsg{X: 32, Y: 16, Type: MouseWheelUp},140+ MouseMsg{X: 32, Y: 16, Type: MouseWheelUp, Button: MouseButtonWheelUp, Action: MouseActionPress},141 },142+ // SGR Mouse event.143+ seqTest{144+ []byte("\x1b[<0;33;17M"),145+ MouseMsg{X: 32, Y: 16, Type: MouseLeft, Button: MouseButtonLeft, Action: MouseActionPress},146+ },147 // Runes.148 seqTest{149 []byte{'a'},150 KeyMsg{Type: KeyRunes, Runes: []rune("a")},169 unmodified lines320 {"wheel up",321 []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},322 []Msg{323 MouseMsg{319- X: 32,320- Y: 16,321- Type: MouseWheelUp,324+ X: 32,325+ Y: 16,326+ Type: MouseWheelUp,327+ Button: MouseButtonWheelUp,328+ Action: MouseActionPress,329 },330 },331 },325- {"left release",332+ {"left motion release",333 []byte{334 '\x1b', '[', 'M', byte(32) + 0b0010_0000, byte(32 + 33), byte(16 + 33),335 '\x1b', '[', 'M', byte(32) + 0b0000_0011, byte(64 + 33), byte(32 + 33),336 },337 []Msg{338 MouseMsg(MouseEvent{332- X: 32,333- Y: 16,334- Type: MouseLeft,339+ X: 32,340+ Y: 16,341+ Type: MouseLeft,342+ Button: MouseButtonLeft,343+ Action: MouseActionMotion,344 }),345 MouseMsg(MouseEvent{337- X: 64,338- Y: 32,339- Type: MouseRelease,346+ X: 64,347+ Y: 32,348+ Type: MouseRelease,349+ Button: MouseButtonNone,350+ Action: MouseActionRelease,351 }),352 },353 },354 {"shift+tab",338 unmodified linesmodified:key.go+18 added-5 removed
564 unmodified lines565 // the left over data in the next iteration.566 canHaveMoreData := numBytes == len(buf)567 568 var i, w int569- for i, w = 0, 0; i < len(b); i += w {569+ for i, w = 0, 07; i < len(b); i += w {570 var msg Msg571 w, msg = detectOneMsg(b[i:], canHaveMoreData)572 if w == 0 {573 // Expecting more bytes beyond the current buffer. Try waiting16 unmodified lines590 leftOverFromPrevIteration = nil591 }592 }593 594+var (594-var unknownCSIRe = regexp.MustCompile(`^\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]`)595+ unknownCSIRe = regexp.MustCompile(`^\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]`)596+ mouseSGRRegex = regexp.MustCompile(`(\d+);(\d+);(\d+)([Mm])`)597+)598 599 func detectOneMsg(b []byte, canHaveMoreData bool) (w int, msg Msg) {600 // Detect mouse events.598- const mouseEventLen = 6599- if len(b) >= mouseEventLen && b[0] == '\x1b' && b[1] == '[' && b[2] == 'M' {600- return mouseEventLen, MouseMsg(parseX10MouseEvent(b))601+ // X10 mouse events have a length of 6 bytes602+ const mouseEventX10Len = 6603+ if len(b) >= mouseEventX10Len && b[0] == '\x1b' && b[1] == '[' {604+ switch b[2] {605+ case 'M':606+ return mouseEventX10Len, MouseMsg(parseX10MouseEvent(b))607+ case '<':608+ if matchIndices := mouseSGRRegex.FindSubmatchIndex(b[3:]); matchIndices != nil {609+ // SGR mouse events length is the length of the match plus the length of the escape sequence610+ mouseEventSGRLen := matchIndices[1] + 3611+ return mouseEventSGRLen, MouseMsg(parseSGRMouseEvent(b))612+ }613+ }614 }615 616 // Detect escape sequence and control characters other than NUL,617 // possibly with an escape character in front to mark the Alt69 unmodified linesmodified:mouse_test.go+717 added-124 removed
1 package tea2 3-import "testing"3+import (4+ "fmt"5+ "testing"6+)7 8 func TestMouseEvent_String(t *testing.T) {9 tt := []struct {10 name string11 event MouseEvent12 expected string13 }{14 {12- name: "unknown",13- event: MouseEvent{Type: MouseUnknown},15+ name: "unknown",16+ event: MouseEvent{17+ Action: MouseActionPress,18+ Button: MouseButtonNone,19+ Type: MouseUnknown,20+ },21 expected: "unknown",22 },23 {17- name: "left",18- event: MouseEvent{Type: MouseLeft},19- expected: "left",24+ name: "left",25+ event: MouseEvent{26+ Action: MouseActionPress,27+ Button: MouseButtonLeft,28+ Type: MouseLeft,29+ },30+ expected: "left press",31 },32 {22- name: "right",23- event: MouseEvent{Type: MouseRight},24- expected: "right",33+ name: "right",34+ event: MouseEvent{35+ Action: MouseActionPress,36+ Button: MouseButtonRight,37+ Type: MouseRight,38+ },39+ expected: "right press",40 },41 {27- name: "middle",28- event: MouseEvent{Type: MouseMiddle},29- expected: "middle",42+ name: "middle",43+ event: MouseEvent{44+ Action: MouseActionPress,45+ Button: MouseButtonMiddle,46+ Type: MouseMiddle,47+ },48+ expected: "middle press",49 },50 {32- name: "release",33- event: MouseEvent{Type: MouseRelease},51+ name: "release",52+ event: MouseEvent{53+ Action: MouseActionRelease,54+ Button: MouseButtonNone,55+ Type: MouseRelease,56+ },57 expected: "release",58 },59 {37- name: "wheel up",38- event: MouseEvent{Type: MouseWheelUp},60+ name: "wheel up",61+ event: MouseEvent{62+ Action: MouseActionPress,63+ Button: MouseButtonWheelUp,64+ Type: MouseWheelUp,65+ },66 expected: "wheel up",67 },68 {42- name: "wheel down",43- event: MouseEvent{Type: MouseWheelDown},69+ name: "wheel down",70+ event: MouseEvent{71+ Action: MouseActionPress,72+ Button: MouseButtonWheelDown,73+ Type: MouseWheelDown,74+ },75 expected: "wheel down",76 },77 {47- name: "motion",48- event: MouseEvent{Type: MouseMotion},78+ name: "wheel left",79+ event: MouseEvent{80+ Action: MouseActionPress,81+ Button: MouseButtonWheelLeft,82+ Type: MouseWheelLeft,83+ },84+ expected: "wheel left",85+ },86+ {87+ name: "wheel right",88+ event: MouseEvent{89+ Action: MouseActionPress,90+ Button: MouseButtonWheelRight,91+ Type: MouseWheelRight,92+ },93+ expected: "wheel right",94+ },95+ {96+ name: "motion",97+ event: MouseEvent{98+ Action: MouseActionMotion,99+ Button: MouseButtonNone,100+ Type: MouseMotion,101+ },102 expected: "motion",103 },104 {105+ name: "shift+left release",106+ event: MouseEvent{107+ Type: MouseLeft,108+ Action: MouseActionRelease,109+ Button: MouseButtonLeft,110+ Shift: true,111+ },112+ expected: "shift+left release",113+ },114+ {115+ name: "shift+left",116+ event: MouseEvent{117+ Type: MouseLeft,118+ Action: MouseActionPress,119+ Button: MouseButtonLeft,120+ Shift: true,121+ },122+ expected: "shift+left press",123+ },124+ {125+ name: "ctrl+shift+left",126+ event: MouseEvent{127+ Type: MouseLeft,128+ Action: MouseActionPress,129+ Button: MouseButtonLeft,130+ Shift: true,131+ Ctrl: true,132+ },133+ expected: "ctrl+shift+left press",134+ },135+ {136 name: "alt+left",137 event: MouseEvent{54- Type: MouseLeft,55- Alt: true,138+ Type: MouseLeft,139+ Action: MouseActionPress,140+ Button: MouseButtonLeft,141+ Alt: true,142 },57- expected: "alt+left",143+ expected: "alt+left press",144 },145 {146 name: "ctrl+left",147 event: MouseEvent{62- Type: MouseLeft,63- Ctrl: true,148+ Type: MouseLeft,149+ Action: MouseActionPress,150+ Button: MouseButtonLeft,151+ Ctrl: true,152 },65- expected: "ctrl+left",153+ expected: "ctrl+left press",154 },155 {156 name: "ctrl+alt+left",157 event: MouseEvent{70- Type: MouseLeft,71- Alt: true,72- Ctrl: true,158+ Type: MouseLeft,159+ Action: MouseActionPress,160+ Button: MouseButtonLeft,161+ Alt: true,162+ Ctrl: true,163 },74- expected: "ctrl+alt+left",164+ expected: "ctrl+alt+left press",165 },166 {167+ name: "ctrl+alt+shift+left",168+ event: MouseEvent{169+ Type: MouseLeft,170+ Action: MouseActionPress,171+ Button: MouseButtonLeft,172+ Alt: true,173+ Ctrl: true,174+ Shift: true,175+ },176+ expected: "ctrl+alt+shift+left press",177+ },178+ {179 name: "ignore coordinates",180 event: MouseEvent{79- X: 100,80- Y: 200,81- Type: MouseLeft,181+ X: 100,182+ Y: 200,183+ Type: MouseLeft,184+ Action: MouseActionPress,185+ Button: MouseButtonLeft,186 },83- expected: "left",187+ expected: "left press",188 },189 {190 name: "broken type",191 event: MouseEvent{88- Type: MouseEventType(-1000),192+ Type: MouseEventType(-100),193+ Action: MouseAction(-110),194+ Button: MouseButton(-120),195 },196 expected: "",197 },198 }33 unmodified lines232 }{233 // Position.234 {235 name: "zero position",130- buf: encode(0b0010_0000, 0, 0),236+ buf: encode(0b0000_0000, 0, 0),237 expected: MouseEvent{132- X: 0,133- Y: 0,134- Type: MouseLeft,238+ X: 0,239+ Y: 0,240+ Type: MouseLeft,241+ Action: MouseActionPress,242+ Button: MouseButtonLeft,243 },244 },245 {246 name: "max position",139- buf: encode(0b0010_0000, 222, 222), // Because 255 (max int8) - 32 - 1.247+ buf: encode(0b0000_0000, 222, 222), // Because 255 (max int8) - 32 - 1.248 expected: MouseEvent{141- X: 222,142- Y: 222,143- Type: MouseLeft,249+ X: 222,250+ Y: 222,251+ Type: MouseLeft,252+ Action: MouseActionPress,253+ Button: MouseButtonLeft,254 },255 },256 // Simple.257 {258 name: "left",259 buf: encode(0b0000_0000, 32, 16),260 expected: MouseEvent{151- X: 32,152- Y: 16,153- Type: MouseLeft,261+ X: 32,262+ Y: 16,263+ Type: MouseLeft,264+ Action: MouseActionPress,265+ Button: MouseButtonLeft,266 },267 },268 {269 name: "left in motion",270 buf: encode(0b0010_0000, 32, 16),271 expected: MouseEvent{160- X: 32,161- Y: 16,162- Type: MouseLeft,272+ X: 32,273+ Y: 16,274+ Type: MouseLeft,275+ Action: MouseActionMotion,276+ Button: MouseButtonLeft,277 },278 },279 {280 name: "middle",281 buf: encode(0b0000_0001, 32, 16),282 expected: MouseEvent{169- X: 32,170- Y: 16,171- Type: MouseMiddle,283+ X: 32,284+ Y: 16,285+ Type: MouseMiddle,286+ Action: MouseActionPress,287+ Button: MouseButtonMiddle,288 },289 },290 {291 name: "middle in motion",292 buf: encode(0b0010_0001, 32, 16),293 expected: MouseEvent{178- X: 32,179- Y: 16,180- Type: MouseMiddle,294+ X: 32,295+ Y: 16,296+ Type: MouseMiddle,297+ Action: MouseActionMotion,298+ Button: MouseButtonMiddle,299 },300 },301 {302 name: "right",303 buf: encode(0b0000_0010, 32, 16),304 expected: MouseEvent{187- X: 32,188- Y: 16,189- Type: MouseRight,305+ X: 32,306+ Y: 16,307+ Type: MouseRight,308+ Action: MouseActionPress,309+ Button: MouseButtonRight,310 },311 },312 {313 name: "right in motion",314 buf: encode(0b0010_0010, 32, 16),315 expected: MouseEvent{196- X: 32,197- Y: 16,198- Type: MouseRight,316+ X: 32,317+ Y: 16,318+ Type: MouseRight,319+ Action: MouseActionMotion,320+ Button: MouseButtonRight,321 },322 },323 {324 name: "motion",325 buf: encode(0b0010_0011, 32, 16),326 expected: MouseEvent{205- X: 32,206- Y: 16,207- Type: MouseMotion,327+ X: 32,328+ Y: 16,329+ Type: MouseMotion,330+ Action: MouseActionMotion,331+ Button: MouseButtonNone,332 },333 },334 {335 name: "wheel up",336 buf: encode(0b0100_0000, 32, 16),337 expected: MouseEvent{214- X: 32,215- Y: 16,216- Type: MouseWheelUp,338+ X: 32,339+ Y: 16,340+ Type: MouseWheelUp,341+ Action: MouseActionPress,342+ Button: MouseButtonWheelUp,343 },344 },345 {346 name: "wheel down",347 buf: encode(0b0100_0001, 32, 16),348 expected: MouseEvent{223- X: 32,224- Y: 16,225- Type: MouseWheelDown,349+ X: 32,350+ Y: 16,351+ Type: MouseWheelDown,352+ Action: MouseActionPress,353+ Button: MouseButtonWheelDown,354 },355 },356 {357+ name: "wheel left",358+ buf: encode(0b0100_0010, 32, 16),359+ expected: MouseEvent{360+ X: 32,361+ Y: 16,362+ Type: MouseWheelLeft,363+ Action: MouseActionPress,364+ Button: MouseButtonWheelLeft,365+ },366+ },367+ {368+ name: "wheel right",369+ buf: encode(0b0100_0011, 32, 16),370+ expected: MouseEvent{371+ X: 32,372+ Y: 16,373+ Type: MouseWheelRight,374+ Action: MouseActionPress,375+ Button: MouseButtonWheelRight,376+ },377+ },378+ {379 name: "release",380 buf: encode(0b0000_0011, 32, 16),381 expected: MouseEvent{232- X: 32,233- Y: 16,234- Type: MouseRelease,382+ X: 32,383+ Y: 16,384+ Type: MouseRelease,385+ Action: MouseActionRelease,386+ Button: MouseButtonNone,387 },388 },389+ {390+ name: "backward",391+ buf: encode(0b1000_0000, 32, 16),392+ expected: MouseEvent{393+ X: 32,394+ Y: 16,395+ Type: MouseBackward,396+ Action: MouseActionPress,397+ Button: MouseButtonBackward,398+ },399+ },400+ {401+ name: "forward",402+ buf: encode(0b1000_0001, 32, 16),403+ expected: MouseEvent{404+ X: 32,405+ Y: 16,406+ Type: MouseForward,407+ Action: MouseActionPress,408+ Button: MouseButtonForward,409+ },410+ },411+ {412+ name: "button 10",413+ buf: encode(0b1000_0010, 32, 16),414+ expected: MouseEvent{415+ X: 32,416+ Y: 16,417+ Type: MouseUnknown,418+ Action: MouseActionPress,419+ Button: MouseButton10,420+ },421+ },422+ {423+ name: "button 11",424+ buf: encode(0b1000_0011, 32, 16),425+ expected: MouseEvent{426+ X: 32,427+ Y: 16,428+ Type: MouseUnknown,429+ Action: MouseActionPress,430+ Button: MouseButton11,431+ },432+ },433 // Combinations.434 {435 name: "alt+right",240- buf: encode(0b0010_1010, 32, 16),436+ buf: encode(0b0000_1010, 32, 16),437 expected: MouseEvent{242- X: 32,243- Y: 16,244- Type: MouseRight,245- Alt: true,438+ X: 32,439+ Y: 16,440+ Alt: true,441+ Type: MouseRight,442+ Action: MouseActionPress,443+ Button: MouseButtonRight,444 },445 },446 {447 name: "ctrl+right",250- buf: encode(0b0011_0010, 32, 16),448+ buf: encode(0b0001_0010, 32, 16),449 expected: MouseEvent{252- X: 32,253- Y: 16,254- Type: MouseRight,255- Ctrl: true,450+ X: 32,451+ Y: 16,452+ Ctrl: true,453+ Type: MouseRight,454+ Action: MouseActionPress,455+ Button: MouseButtonRight,456 },457 },458 {259- name: "ctrl+alt+right",260- buf: encode(0b0011_1010, 32, 16),459+ name: "left in motion",460+ buf: encode(0b0010_0000, 32, 16),461 expected: MouseEvent{262- X: 32,263- Y: 16,264- Type: MouseRight,265- Alt: true,266- Ctrl: true,462+ X: 32,463+ Y: 16,464+ Alt: false,465+ Type: MouseLeft,466+ Action: MouseActionMotion,467+ Button: MouseButtonLeft,468 },469 },470 {270- name: "alt+wheel down",271- buf: encode(0b0100_1001, 32, 16),471+ name: "alt+right in motion",472+ buf: encode(0b0010_1010, 32, 16),473 expected: MouseEvent{273- X: 32,274- Y: 16,275- Type: MouseWheelDown,276- Alt: true,474+ X: 32,475+ Y: 16,476+ Alt: true,477+ Type: MouseRight,478+ Action: MouseActionMotion,479+ Button: MouseButtonRight,480 },481 },482 {280- name: "ctrl+wheel down",281- buf: encode(0b0101_0001, 32, 16),483+ name: "ctrl+right in motion",484+ buf: encode(0b0011_0010, 32, 16),485 expected: MouseEvent{283- X: 32,284- Y: 16,285- Type: MouseWheelDown,286- Ctrl: true,486+ X: 32,487+ Y: 16,488+ Ctrl: true,489+ Type: MouseRight,490+ Action: MouseActionMotion,491+ Button: MouseButtonRight,492 },493 },494 {290- name: "ctrl+alt+wheel down",291- buf: encode(0b0101_1001, 32, 16),495+ name: "ctrl+alt+right",496+ buf: encode(0b0001_1010, 32, 16),497 expected: MouseEvent{293- X: 32,294- Y: 16,295- Type: MouseWheelDown,296- Alt: true,297- Ctrl: true,498+ X: 32,499+ Y: 16,500+ Alt: true,501+ Ctrl: true,502+ Type: MouseRight,503+ Action: MouseActionPress,504+ Button: MouseButtonRight,505 },506 },300- // Unknown.507 {302- name: "wheel with unknown bit",303- buf: encode(0b0100_0010, 32, 16),508+ name: "ctrl+wheel up",509+ buf: encode(0b0101_0000, 32, 16),510 expected: MouseEvent{305- X: 32,306- Y: 16,307- Type: MouseUnknown,511+ X: 32,512+ Y: 16,513+ Ctrl: true,514+ Type: MouseWheelUp,515+ Action: MouseActionPress,516+ Button: MouseButtonWheelUp,517 },518 },519 {311- name: "unknown with modifier",312- buf: encode(0b0100_1010, 32, 16),520+ name: "alt+wheel down",521+ buf: encode(0b0100_1001, 32, 16),522 expected: MouseEvent{314- X: 32,315- Y: 16,316- Type: MouseUnknown,317- Alt: true,523+ X: 32,524+ Y: 16,525+ Alt: true,526+ Type: MouseWheelDown,527+ Action: MouseActionPress,528+ Button: MouseButtonWheelDown,529 },530 },531+ {532+ name: "ctrl+alt+wheel down",533+ buf: encode(0b0101_1001, 32, 16),534+ expected: MouseEvent{535+ X: 32,536+ Y: 16,537+ Alt: true,538+ Ctrl: true,539+ Type: MouseWheelDown,540+ Action: MouseActionPress,541+ Button: MouseButtonWheelDown,542+ },543+ },544 // Overflow position.545 {546 name: "overflow position",547 buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1.548 expected: MouseEvent{325- X: -6,326- Y: -33,327- Type: MouseLeft,549+ X: -6,550+ Y: -33,551+ Type: MouseLeft,552+ Action: MouseActionMotion,553+ Button: MouseButtonLeft,554 },555 },556 }557 11 unmodified lines569 }570 })571 }572 }573+574+// func TestParseX10MouseEvent_error(t *testing.T) {575+// tt := []struct {576+// name string577+// buf []byte578+// }{579+// {580+// name: "empty buf",581+// buf: nil,582+// },583+// {584+// name: "wrong high bit",585+// buf: []byte("\x1a[M@A1"),586+// },587+// {588+// name: "short buf",589+// buf: []byte("\x1b[M@A"),590+// },591+// {592+// name: "long buf",593+// buf: []byte("\x1b[M@A11"),594+// },595+// }596+//597+// for i := range tt {598+// tc := tt[i]599+//600+// t.Run(tc.name, func(t *testing.T) {601+// _, err := parseX10MouseEvent(tc.buf)602+//603+// if err == nil {604+// t.Fatalf("expected error but got nil")605+// }606+// })607+// }608+// }609+610+func TestParseSGRMouseEvent(t *testing.T) {611+ encode := func(b, x, y int, r bool) []byte {612+ re := 'M'613+ if r {614+ re = 'm'615+ }616+ return []byte(fmt.Sprintf("\x1b[<%d;%d;%d%c", b, x+1, y+1, re))617+ }618+619+ tt := []struct {620+ name string621+ buf []byte622+ expected MouseEvent623+ }{624+ // Position.625+ {626+ name: "zero position",627+ buf: encode(0, 0, 0, false),628+ expected: MouseEvent{629+ X: 0,630+ Y: 0,631+ Type: MouseLeft,632+ Action: MouseActionPress,633+ Button: MouseButtonLeft,634+ },635+ },636+ {637+ name: "225 position",638+ buf: encode(0, 225, 225, false),639+ expected: MouseEvent{640+ X: 225,641+ Y: 225,642+ Type: MouseLeft,643+ Action: MouseActionPress,644+ Button: MouseButtonLeft,645+ },646+ },647+ // Simple.648+ {649+ name: "left",650+ buf: encode(0, 32, 16, false),651+ expected: MouseEvent{652+ X: 32,653+ Y: 16,654+ Type: MouseLeft,655+ Action: MouseActionPress,656+ Button: MouseButtonLeft,657+ },658+ },659+ {660+ name: "left in motion",661+ buf: encode(32, 32, 16, false),662+ expected: MouseEvent{663+ X: 32,664+ Y: 16,665+ Type: MouseLeft,666+ Action: MouseActionMotion,667+ Button: MouseButtonLeft,668+ },669+ },670+ {671+ name: "left release",672+ buf: encode(0, 32, 16, true),673+ expected: MouseEvent{674+ X: 32,675+ Y: 16,676+ Type: MouseRelease,677+ Action: MouseActionRelease,678+ Button: MouseButtonLeft,679+ },680+ },681+ {682+ name: "middle",683+ buf: encode(1, 32, 16, false),684+ expected: MouseEvent{685+ X: 32,686+ Y: 16,687+ Type: MouseMiddle,688+ Action: MouseActionPress,689+ Button: MouseButtonMiddle,690+ },691+ },692+ {693+ name: "middle in motion",694+ buf: encode(33, 32, 16, false),695+ expected: MouseEvent{696+ X: 32,697+ Y: 16,698+ Type: MouseMiddle,699+ Action: MouseActionMotion,700+ Button: MouseButtonMiddle,701+ },702+ },703+ {704+ name: "middle release",705+ buf: encode(1, 32, 16, true),706+ expected: MouseEvent{707+ X: 32,708+ Y: 16,709+ Type: MouseRelease,710+ Action: MouseActionRelease,711+ Button: MouseButtonMiddle,712+ },713+ },714+ {715+ name: "right",716+ buf: encode(2, 32, 16, false),717+ expected: MouseEvent{718+ X: 32,719+ Y: 16,720+ Type: MouseRight,721+ Action: MouseActionPress,722+ Button: MouseButtonRight,723+ },724+ },725+ {726+ name: "right release",727+ buf: encode(2, 32, 16, true),728+ expected: MouseEvent{729+ X: 32,730+ Y: 16,731+ Type: MouseRelease,732+ Action: MouseActionRelease,733+ Button: MouseButtonRight,734+ },735+ },736+ {737+ name: "motion",738+ buf: encode(35, 32, 16, false),739+ expected: MouseEvent{740+ X: 32,741+ Y: 16,742+ Type: MouseMotion,743+ Action: MouseActionMotion,744+ Button: MouseButtonNone,745+ },746+ },747+ {748+ name: "wheel up",749+ buf: encode(64, 32, 16, false),750+ expected: MouseEvent{751+ X: 32,752+ Y: 16,753+ Type: MouseWheelUp,754+ Action: MouseActionPress,755+ Button: MouseButtonWheelUp,756+ },757+ },758+ {759+ name: "wheel down",760+ buf: encode(65, 32, 16, false),761+ expected: MouseEvent{762+ X: 32,763+ Y: 16,764+ Type: MouseWheelDown,765+ Action: MouseActionPress,766+ Button: MouseButtonWheelDown,767+ },768+ },769+ {770+ name: "wheel left",771+ buf: encode(66, 32, 16, false),772+ expected: MouseEvent{773+ X: 32,774+ Y: 16,775+ Type: MouseWheelLeft,776+ Action: MouseActionPress,777+ Button: MouseButtonWheelLeft,778+ },779+ },780+ {781+ name: "wheel right",782+ buf: encode(67, 32, 16, false),783+ expected: MouseEvent{784+ X: 32,785+ Y: 16,786+ Type: MouseWheelRight,787+ Action: MouseActionPress,788+ Button: MouseButtonWheelRight,789+ },790+ },791+ {792+ name: "backward",793+ buf: encode(128, 32, 16, false),794+ expected: MouseEvent{795+ X: 32,796+ Y: 16,797+ Type: MouseBackward,798+ Action: MouseActionPress,799+ Button: MouseButtonBackward,800+ },801+ },802+ {803+ name: "backward in motion",804+ buf: encode(160, 32, 16, false),805+ expected: MouseEvent{806+ X: 32,807+ Y: 16,808+ Type: MouseBackward,809+ Action: MouseActionMotion,810+ Button: MouseButtonBackward,811+ },812+ },813+ {814+ name: "forward",815+ buf: encode(129, 32, 16, false),816+ expected: MouseEvent{817+ X: 32,818+ Y: 16,819+ Type: MouseForward,820+ Action: MouseActionPress,821+ Button: MouseButtonForward,822+ },823+ },824+ {825+ name: "forward in motion",826+ buf: encode(161, 32, 16, false),827+ expected: MouseEvent{828+ X: 32,829+ Y: 16,830+ Type: MouseForward,831+ Action: MouseActionMotion,832+ Button: MouseButtonForward,833+ },834+ },835+ // Combinations.836+ {837+ name: "alt+right",838+ buf: encode(10, 32, 16, false),839+ expected: MouseEvent{840+ X: 32,841+ Y: 16,842+ Alt: true,843+ Type: MouseRight,844+ Action: MouseActionPress,845+ Button: MouseButtonRight,846+ },847+ },848+ {849+ name: "ctrl+right",850+ buf: encode(18, 32, 16, false),851+ expected: MouseEvent{852+ X: 32,853+ Y: 16,854+ Ctrl: true,855+ Type: MouseRight,856+ Action: MouseActionPress,857+ Button: MouseButtonRight,858+ },859+ },860+ {861+ name: "ctrl+alt+right",862+ buf: encode(26, 32, 16, false),863+ expected: MouseEvent{864+ X: 32,865+ Y: 16,866+ Alt: true,867+ Ctrl: true,868+ Type: MouseRight,869+ Action: MouseActionPress,870+ Button: MouseButtonRight,871+ },872+ },873+ {874+ name: "alt+wheel press",875+ buf: encode(73, 32, 16, false),876+ expected: MouseEvent{877+ X: 32,878+ Y: 16,879+ Alt: true,880+ Type: MouseWheelDown,881+ Action: MouseActionPress,882+ Button: MouseButtonWheelDown,883+ },884+ },885+ {886+ name: "ctrl+wheel press",887+ buf: encode(81, 32, 16, false),888+ expected: MouseEvent{889+ X: 32,890+ Y: 16,891+ Ctrl: true,892+ Type: MouseWheelDown,893+ Action: MouseActionPress,894+ Button: MouseButtonWheelDown,895+ },896+ },897+ {898+ name: "ctrl+alt+wheel press",899+ buf: encode(89, 32, 16, false),900+ expected: MouseEvent{901+ X: 32,902+ Y: 16,903+ Alt: true,904+ Ctrl: true,905+ Type: MouseWheelDown,906+ Action: MouseActionPress,907+ Button: MouseButtonWheelDown,908+ },909+ },910+ {911+ name: "ctrl+alt+shift+wheel press",912+ buf: encode(93, 32, 16, false),913+ expected: MouseEvent{914+ X: 32,915+ Y: 16,916+ Shift: true,917+ Alt: true,918+ Ctrl: true,919+ Type: MouseWheelDown,920+ Action: MouseActionPress,921+ Button: MouseButtonWheelDown,922+ },923+ },924+ }925+926+ for i := range tt {927+ tc := tt[i]928+929+ t.Run(tc.name, func(t *testing.T) {930+ actual := parseSGRMouseEvent(tc.buf)931+ if tc.expected != actual {932+ t.Fatalf("expected %#v but got %#v",933+ tc.expected,934+ actual,935+ )936+ }937+ })938+ }939+}modified:mouse.go+233 added-57 removed
1 package tea2 3+import "strconv"4+5 // MouseMsg contains information about a mouse event and are sent to a programs6 // update function when mouse activity occurs. Note that the mouse must first7 // be enabled in order for the mouse events to be received.8 type MouseMsg MouseEvent5 unmodified lines14 15 // MouseEvent represents a mouse event, which could be a click, a scroll wheel16 // movement, a cursor movement, or a combination.17 type MouseEvent struct {16- X int17- Y int18+ X int19+ Y int20+ Shift bool21+ Alt bool22+ Ctrl bool23+ Action MouseAction24+ Button MouseButton25+26+ // Deprecated: Use MouseAction & MouseButton instead.27 Type MouseEventType19- Alt bool20- Ctrl bool28 }29 30+// IsWheel returns true if the mouse event is a wheel event.31+func (m MouseEvent) IsWheel() bool {32+ return m.Button == MouseButtonWheelUp || m.Button == MouseButtonWheelDown ||33+ m.Button == MouseButtonWheelLeft || m.Button == MouseButtonWheelRight34+}35+36 // String returns a string representation of a mouse event.37 func (m MouseEvent) String() (s string) {38 if m.Ctrl {39 s += "ctrl+"40 }41 if m.Alt {42 s += "alt+"43 }31- s += mouseEventTypes[m.Type]44+ if m.Shift {45+ s += "shift+"46+ }47+48+ if m.Button == MouseButtonNone {49+ if m.Action == MouseActionMotion || m.Action == MouseActionRelease {50+ s += mouseActions[m.Action]51+ } else {52+ s += "unknown"53+ }54+ } else if m.IsWheel() {55+ s += mouseButtons[m.Button]56+ } else {57+ btn := mouseButtons[m.Button]58+ if btn != "" {59+ s += btn60+ }61+ act := mouseActions[m.Action]62+ if act != "" {63+ s += " " + act64+ }65+ }66+67 return s68 }69 70+// MouseAction represents the action that occurred during a mouse event.71+type MouseAction int72+73+// Mouse event actions.74+const (75+ MouseActionPress MouseAction = iota76+ MouseActionRelease77+ MouseActionMotion78+)79+80+var mouseActions = map[MouseAction]string{81+ MouseActionPress: "press",82+ MouseActionRelease: "release",83+ MouseActionMotion: "motion",84+}85+86+// MouseButton represents the button that was pressed during a mouse event.87+type MouseButton int88+89+// Mouse event buttons90+//91+// This is based on X11 mouse button codes.92+//93+// 1 = left button94+// 2 = middle button (pressing the scroll wheel)95+// 3 = right button96+// 4 = turn scroll wheel up97+// 5 = turn scroll wheel down98+// 6 = push scroll wheel left99+// 7 = push scroll wheel right100+// 8 = 4th button (aka browser backward button)101+// 9 = 5th button (aka browser forward button)102+// 10103+// 11104+//105+// Other buttons are not supported.106+const (107+ MouseButtonNone MouseButton = iota108+ MouseButtonLeft109+ MouseButtonMiddle110+ MouseButtonRight111+ MouseButtonWheelUp112+ MouseButtonWheelDown113+ MouseButtonWheelLeft114+ MouseButtonWheelRight115+ MouseButtonBackward116+ MouseButtonForward117+ MouseButton10118+ MouseButton11119+)120+121+var mouseButtons = map[MouseButton]string{122+ MouseButtonNone: "none",123+ MouseButtonLeft: "left",124+ MouseButtonMiddle: "middle",125+ MouseButtonRight: "right",126+ MouseButtonWheelUp: "wheel up",127+ MouseButtonWheelDown: "wheel down",128+ MouseButtonWheelLeft: "wheel left",129+ MouseButtonWheelRight: "wheel right",130+ MouseButtonBackward: "backward",131+ MouseButtonForward: "forward",132+ MouseButton10: "button 10",133+ MouseButton11: "button 11",134+}135+136 // MouseEventType indicates the type of mouse event occurring.137+//138+// Deprecated: Use MouseAction & MouseButton instead.139 type MouseEventType int140 141 // Mouse event types.142+//143+// Deprecated: Use MouseAction & MouseButton instead.144 const (145 MouseUnknown MouseEventType = iota146 MouseLeft147 MouseRight148 MouseMiddle44- MouseRelease149+ MouseRelease // mouse button release (X10 only)150 MouseWheelUp151 MouseWheelDown152+ MouseWheelLeft153+ MouseWheelRight154+ MouseBackward155+ MouseForward156 MouseMotion157 )158 50-var mouseEventTypes = map[MouseEventType]string{51- MouseUnknown: "unknown",52- MouseLeft: "left",53- MouseRight: "right",54- MouseMiddle: "middle",55- MouseRelease: "release",56- MouseWheelUp: "wheel up",57- MouseWheelDown: "wheel down",58- MouseMotion: "motion",159+// Parse SGR-encoded mouse events; SGR extended mouse events. SGR mouse events160+// look like:161+//162+// ESC [ < Cb ; Cx ; Cy (M or m)163+//164+// where:165+//166+// Cb is the encoded button code167+// Cx is the x-coordinate of the mouse168+// Cy is the y-coordinate of the mouse169+// M is for button press, m is for button release170+//171+// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates172+func parseSGRMouseEvent(buf []byte) MouseEvent {173+ str := string(buf[3:])174+ matches := mouseSGRRegex.FindStringSubmatch(str)175+ if len(matches) != 5 {176+ // Unreachable, we already checked the regex in `detectOneMsg`.177+ panic("invalid mouse event")178+ }179+180+ b, _ := strconv.Atoi(matches[1])181+ px := matches[2]182+ py := matches[3]183+ release := matches[4] == "m"184+ m := parseMouseButton(b, true)185+186+ // Wheel buttons don't have release events187+ // Motion can be reported as a release event in some terminals (Windows Terminal)188+ if m.Action != MouseActionMotion && !m.IsWheel() && release {189+ m.Action = MouseActionRelease190+ m.Type = MouseRelease191+ }192+193+ x, _ := strconv.Atoi(px)194+ y, _ := strconv.Atoi(py)195+196+ // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).197+ m.X = x - 1198+ m.Y = y - 1199+200+ return m201 }202 203+const x10MouseByteOffset = 32204+205 // Parse X10-encoded mouse events; the simplest kind. The last release of X1062-// was December 1986, by the way.206+// was December 1986, by the way. The original X10 mouse protocol limits the Cx207+// and Cy coordinates to 223 (=255-032).208 //209 // X10 mouse events look like:210 //211 // ESC [M Cb Cx Cy212 //213 // See: http://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking214 func parseX10MouseEvent(buf []byte) MouseEvent {215 v := buf[3:6]216+ m := parseMouseButton(int(v[0]), false)217+218+ // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).219+ m.X = int(v[1]) - x10MouseByteOffset - 1220+ m.Y = int(v[2]) - x10MouseByteOffset - 1221+222+ return m223+}224+225+// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates226+func parseMouseButton(b int, isSGR bool) MouseEvent {227 var m MouseEvent72- const byteOffset = 3273- e := v[0] - byteOffset228+ e := b229+ if !isSGR {230+ e -= x10MouseByteOffset231+ }232 233 const (234 bitShift = 0b0000_0100235 bitAlt = 0b0000_1000236 bitCtrl = 0b0001_0000237 bitMotion = 0b0010_0000238 bitWheel = 0b0100_0000239+ bitAdd = 0b1000_0000 // additional buttons 8-11240 241 bitsMask = 0b0000_001183-84- bitsLeft = 0b0000_000085- bitsMiddle = 0b0000_000186- bitsRight = 0b0000_001087- bitsRelease = 0b0000_001188-89- bitsWheelUp = 0b0000_000090- bitsWheelDown = 0b0000_0001242 )243 93- if e&bitWheel != 0 {94- // Check the low two bits.95- switch e & bitsMask {96- case bitsWheelUp:97- m.Type = MouseWheelUp98- case bitsWheelDown:99- m.Type = MouseWheelDown100- }244+ if e&bitAdd != 0 {245+ m.Button = MouseButtonBackward + MouseButton(e&bitsMask)246+ } else if e&bitWheel != 0 {247+ m.Button = MouseButtonWheelUp + MouseButton(e&bitsMask)248 } else {102- // Check the low two bits.103- // We do not separate clicking and dragging.104- switch e & bitsMask {105- case bitsLeft:106- m.Type = MouseLeft107- case bitsMiddle:108- m.Type = MouseMiddle109- case bitsRight:110- m.Type = MouseRight111- case bitsRelease:112- if e&bitMotion != 0 {113- m.Type = MouseMotion114- } else {115- m.Type = MouseRelease116- }249+ m.Button = MouseButtonLeft + MouseButton(e&bitsMask)250+ // X10 reports a button release as 0b0000_0011 (3)251+ if e&bitsMask == bitsMask {252+ m.Action = MouseActionRelease253+ m.Button = MouseButtonNone254 }255 }256 120- if e&bitAlt != 0 {121- m.Alt = true257+ // Motion bit doesn't get reported for wheel events.258+ if e&bitMotion != 0 && !m.IsWheel() {259+ m.Action = MouseActionMotion260 }123- if e&bitCtrl != 0 {124- m.Ctrl = true125- }261 127- // (1,1) is the upper left. We subtract 1 to normalize it to (0,0).128- m.X = int(v[1]) - byteOffset - 1129- m.Y = int(v[2]) - byteOffset - 1262+ // Modifiers263+ m.Alt = e&bitAlt != 0264+ m.Ctrl = e&bitCtrl != 0265+ m.Shift = e&bitShift != 0266 267+ // backward compatibility268+ switch {269+ case m.Button == MouseButtonLeft && m.Action == MouseActionPress:270+ m.Type = MouseLeft271+ case m.Button == MouseButtonMiddle && m.Action == MouseActionPress:272+ m.Type = MouseMiddle273+ case m.Button == MouseButtonRight && m.Action == MouseActionPress:274+ m.Type = MouseRight275+ case m.Button == MouseButtonNone && m.Action == MouseActionRelease:276+ m.Type = MouseRelease277+ case m.Button == MouseButtonWheelUp && m.Action == MouseActionPress:278+ m.Type = MouseWheelUp279+ case m.Button == MouseButtonWheelDown && m.Action == MouseActionPress:280+ m.Type = MouseWheelDown281+ case m.Button == MouseButtonWheelLeft && m.Action == MouseActionPress:282+ m.Type = MouseWheelLeft283+ case m.Button == MouseButtonWheelRight && m.Action == MouseActionPress:284+ m.Type = MouseWheelRight285+ case m.Button == MouseButtonBackward && m.Action == MouseActionPress:286+ m.Type = MouseBackward287+ case m.Button == MouseButtonForward && m.Action == MouseActionPress:288+ m.Type = MouseForward289+ case m.Action == MouseActionMotion:290+ m.Type = MouseMotion291+ switch m.Button {292+ case MouseButtonLeft:293+ m.Type = MouseLeft294+ case MouseButtonMiddle:295+ m.Type = MouseMiddle296+ case MouseButtonRight:297+ m.Type = MouseRight298+ case MouseButtonBackward:299+ m.Type = MouseBackward300+ case MouseButtonForward:301+ m.Type = MouseForward302+ }303+ default:304+ m.Type = MouseUnknown305+ }306+307 return m308 }modified:nil_renderer.go+2 added
15 unmodified lines16 func (n nilRenderer) enableMouseCellMotion() {}17 func (n nilRenderer) disableMouseCellMotion() {}18 func (n nilRenderer) enableMouseAllMotion() {}19 func (n nilRenderer) disableMouseAllMotion() {}20+func (n nilRenderer) enableMouseSGRMode() {}21+func (n nilRenderer) disableMouseSGRMode() {}modified:options.go+6 added
106 unmodified lines107 // Cell motion mode enables mouse click, release, and wheel events. Mouse108 // movement events are also captured if a mouse button is pressed (i.e., drag109 // events). Cell motion mode is better supported than all motion mode.110 //111+// This will try to enable the mouse in extended mode (SGR), if that is not112+// supported by the terminal it will fall back to normal mode (X10).113+//114 // To enable mouse cell motion once the program has already started running use115 // the EnableMouseCellMotion command. To disable the mouse when the program is116 // running use the DisableMouse command.117 //11 unmodified lines129 // EnableMouseAllMotion is a special command that enables mouse click, release,130 // wheel, and motion events, which are delivered regardless of whether a mouse131 // button is pressed, effectively enabling support for hover interactions.132 //133+// This will try to enable the mouse in extended mode (SGR), if that is not134+// supported by the terminal it will fall back to normal mode (X10).135+//136 // Many modern terminals support this, but not all. If in doubt, use137 // EnableMouseCellMotion instead.138 //139 // To enable the mouse once the program has already started running use the79 unmodified linesmodified:renderer.go+9 added-3 removed
38 unmodified lines39 // enableMouseCellMotion enables mouse click, release, wheel and motion40 // events if a mouse button is pressed (i.e., drag events).41 enableMouseCellMotion()42 43- // DisableMouseCellMotion disables Mouse Cell Motion tracking.43+ // disableMouseCellMotion disables Mouse Cell Motion tracking.44 disableMouseCellMotion()45 46- // EnableMouseAllMotion enables mouse click, release, wheel and motion46+ // enableMouseAllMotion enables mouse click, release, wheel and motion47 // events, regardless of whether a mouse button is pressed. Many modern48 // terminals support this, but not all.49 enableMouseAllMotion()50 51- // DisableMouseAllMotion disables All Motion mouse tracking.51+ // disableMouseAllMotion disables All Motion mouse tracking.52 disableMouseAllMotion()53+54+ // enableMouseSGRMode enables mouse extended mode (SGR).55+ enableMouseSGRMode()56+57+ // disableMouseSGRMode disables mouse extended mode (SGR).58+ disableMouseSGRMode()59 }60 61 // repaintMsg forces a full repaint.62 type repaintMsg struct{}modified:screen_test.go+8 added-8 removed
12 unmodified lines13 }{14 {15 name: "clear_screen",16 cmds: []Cmd{ClearScreen},17- expected: "\x1b[?25l\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",17+ expected: "\x1b[?25l\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",18 },19 {20 name: "altscreen",21 cmds: []Cmd{EnterAltScreen, ExitAltScreen},22- expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",22+ expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",23 },24 {25 name: "altscreen_autoexit",26 cmds: []Cmd{EnterAltScreen},27- expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25lsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1049l\x1b[?25h",27+ expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25lsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h",28 },29 {30 name: "mouse_cellmotion",31 cmds: []Cmd{EnableMouseCellMotion},32- expected: "\x1b[?25l\x1b[?1002hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",32+ expected: "\x1b[?25l\x1b[?1002h\x1b[?1006hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",33 },34 {35 name: "mouse_allmotion",36 cmds: []Cmd{EnableMouseAllMotion},37- expected: "\x1b[?25l\x1b[?1003hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",37+ expected: "\x1b[?25l\x1b[?1003h\x1b[?1006hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",38 },39 {40 name: "mouse_disable",41 cmds: []Cmd{EnableMouseAllMotion, DisableMouse},42- expected: "\x1b[?25l\x1b[?1003h\x1b[?1002l\x1b[?1003lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",42+ expected: "\x1b[?25l\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",43 },44 {45 name: "cursor_hide",46 cmds: []Cmd{HideCursor},47- expected: "\x1b[?25l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",47+ expected: "\x1b[?25l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",48 },49 {50 name: "cursor_hideshow",51 cmds: []Cmd{HideCursor, ShowCursor},52- expected: "\x1b[?25l\x1b[?25l\x1b[?25hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l",52+ expected: "\x1b[?25l\x1b[?25l\x1b[?25hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",53 },54 }55 56 for _, test := range tests {20 unmodified linesmodified:standard_renderer.go+14 added
394 unmodified lines395 396 r.out.DisableMouseAllMotion()397 }398 399+func (r *standardRenderer) enableMouseSGRMode() {400+ r.mtx.Lock()401+ defer r.mtx.Unlock()402+403+ r.out.EnableMouseExtendedMode()404+}405+406+func (r *standardRenderer) disableMouseSGRMode() {407+ r.mtx.Lock()408+ defer r.mtx.Unlock()409+410+ r.out.DisableMouseExtendedMode()411+}412+413 // setIgnoredLines specifies lines not to be touched by the standard Bubble Tea414 // renderer.415 func (r *standardRenderer) setIgnoredLines(from int, to int) {416 // Lock if we're going to be clearing some lines since we don't want263 unmodified linesmodified:tea.go+18 added-7 removed
299 unmodified lines300 301 return ch302 }303 304+func (p *Program) disableMouse() {305+ p.renderer.disableMouseCellMotion()306+ p.renderer.disableMouseAllMotion()307+ p.renderer.disableMouseSGRMode()308+}309+310 // eventLoop is the central message loop. It receives and handles the default311 // Bubble Tea messages, update the model and triggers redraws.312 func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {313 for {26 unmodified lines340 341 case exitAltScreenMsg:342 p.renderer.exitAltScreen()343 344+ case enableMouseCellMotionMsg, enableMouseAllMotionMsg:345+ switch msg.(type) {338- case enableMouseCellMotionMsg:339- p.renderer.enableMouseCellMotion()346+ case enableMouseCellMotionMsg:347+ p.renderer.enableMouseCellMotion()348+ case enableMouseAllMotionMsg:349+ p.renderer.enableMouseAllMotion()350+ }351+ // mouse mode (1006) is a no-op if the terminal doesn't support it.352+ p.renderer.enableMouseSGRMode()353 341- case enableMouseAllMotionMsg:342- p.renderer.enableMouseAllMotion()343-354 case disableMouseMsg:345- p.renderer.disableMouseCellMotion()346- p.renderer.disableMouseAllMotion()355+ p.disableMouse()356 357 case showCursorMsg:358 p.renderer.showCursor()359 137 unmodified lines497 p.renderer.enterAltScreen()498 }499 if p.startupOptions&withMouseCellMotion != 0 {500 p.renderer.enableMouseCellMotion()501+ p.renderer.enableMouseSGRMode()502 } else if p.startupOptions&withMouseAllMotion != 0 {503 p.renderer.enableMouseAllMotion()504+ p.renderer.enableMouseSGRMode()505 }506 507 // Initialize the program.508 model := p.initialModel208 unmodified linesmodified:tty.go+1 added-2 removed
33 unmodified lines34 // Bubble Tea program.35 func (p *Program) restoreTerminalState() error {36 if p.renderer != nil {37 p.renderer.showCursor()38- p.renderer.disableMouseCellMotion()39- p.renderer.disableMouseAllMotion()38+ p.disableMouse()39 40 if p.renderer.altScreen() {41 p.renderer.exitAltScreen()42 77 unmodified lines