Skip to content

Commit

Permalink
Fix for Issue nsf#1 : Mouse scrollwheel outputs "A" or "M" in the file
Browse files Browse the repository at this point in the history
  • Loading branch information
tcolar committed Jun 25, 2016
1 parent 22b64e9 commit 7744ff9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions termbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

package termbox

import "unicode/utf8"
import (
"bytes"
"io"
"strconv"
"unicode/utf8"
"unsafe"
)

import "bytes"
import "syscall"
import "unsafe"

import "strings"
import "strconv"

import "os"
import "io"

// private API

Expand Down Expand Up @@ -239,21 +243,24 @@ func parse_escape_sequence(event *Event, buf []byte) (int, bool) {
y := 0
consumed := 0
mouse := false
if len(bufstr) >= 9 && strings.HasPrefix(bufstr, "\033[<") {
if len(bufstr) >= 3 && strings.HasPrefix(bufstr, "\033[<") {
// SGR format : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Extended-coordinates
consumed = 3
event.MouseBtnState = MouseBtnUp
v := 0
idx := 0
valid := false
for _, c := range bufstr[3:] {
consumed++
switch c {
case 'm':
y = v - 1
valid = true
break
case 'M':
event.MouseBtnState = MouseBtnDown
y = v - 1
valid = true
break
case ';':
if idx == 0 {
Expand All @@ -267,6 +274,10 @@ func parse_escape_sequence(event *Event, buf []byte) (int, bool) {
v = v*10 + int(c) - 48
}
}
if !valid {
// event seems incomplete, we will try again once we have more data
return 0, false
}
mouse = true
} else if len(bufstr) >= 6 && strings.HasPrefix(bufstr, "\033[M") {
// X10 format : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-X10-compatbility-mode
Expand Down Expand Up @@ -371,6 +382,9 @@ func extract_event(event *Event) bool {
if inbuf[0] == '\033' {
// possible escape sequence
n, ok := parse_escape_sequence(event, inbuf)
if !ok {
return false
}
if n != 0 {
copy(inbuf, inbuf[n:])
inbuf = inbuf[:len(inbuf)-n]
Expand Down

0 comments on commit 7744ff9

Please sign in to comment.