Skip to content

Commit 8940a91

Browse files
committed
fix: fix various errors in parser - especially nested win32-input-mode
This seems to fix things quite nicely for WezTerm regardless of running remotely or locally on Windows, or on macOS. The goofy nesting level only needs to be done when dealing with nesting, and we can be reasonably sure that all content requiring the extra parse pass comes together and is complete (and is generally only required for non-keyboard data.)
1 parent 393ed0b commit 8940a91

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

_demos/mouse.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func main() {
139139

140140
for {
141141
drawBox(s, 1, 1, 42, 8, style, ' ')
142-
s.PutStrStyled(2, 2, "Press ESC twice to exit, C to clear.", style)
142+
s.PutStrStyled(2, 2, "Press Ctrl-Q to Quit, C to clear.", style)
143143
s.PutStrStyled(2, 3, fmt.Sprintf(posfmt, mx, my), style)
144144
s.PutStrStyled(2, 4, fmt.Sprintf(btnfmt, bstr), style)
145145
s.PutStrStyled(2, 5, fmt.Sprintf(keyfmt, lks), style)
@@ -196,6 +196,9 @@ func main() {
196196
}
197197
} else if ev.Key() == tcell.KeyCtrlL {
198198
s.Sync()
199+
} else if ev.Key() == tcell.KeyCtrlQ {
200+
s.Fini()
201+
os.Exit(0)
199202
} else if ev.Key() == tcell.KeyCtrlZ {
200203
// CtrlZ doesn't really suspend the process, but we use it to execute a subshell.
201204
if err := s.Suspend(); err == nil {

input.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ func (ip *inputParser) scan() {
584584
ip.post(NewEventKey(KeyEscape, "", ModNone))
585585
} else if ec := ip.escChar; ec != 0 {
586586
ip.post(NewEventKey(KeyRune, string(ec), ModAlt))
587-
} else {
588587
}
589588
// if we take too long between bytes, reset the state machine.
590589
ip.state = istInit
@@ -732,16 +731,21 @@ func (ip *inputParser) handleWinKey(P []int) {
732731
// key up event ignore ignore
733732
return
734733
}
735-
if P[0] == 0 && P[2] == 27 && ip.nested == nil {
736-
ip.nested = &inputParser{
737-
evch: ip.evch,
738-
rows: ip.rows,
739-
cols: ip.cols,
740-
}
741-
}
742734

743-
if ip.nested != nil && P[2] > 0 && P[2] < 0x80 { // only ASCII in win32-input-mode
744-
ip.nested.ScanUTF8([]byte{byte(P[2])})
735+
// these terminals never send ambiguous escapes
736+
ip.escaped = false
737+
738+
if P[0] == 0 && P[1] == 0 && P[2] > 0 && P[2] < 0x80 { // only ASCII in win32-input-mode
739+
if ip.nested == nil {
740+
ip.nested = &inputParser{
741+
evch: ip.evch,
742+
rows: ip.rows,
743+
cols: ip.cols,
744+
}
745+
}
746+
if P[2] > 0 {
747+
ip.nested.ScanUTF8([]byte{byte(P[2])})
748+
}
745749
return
746750
}
747751

@@ -814,6 +818,8 @@ func (ip *inputParser) handleCsi(mode rune, params []byte, intermediate []byte)
814818
if n, e := strconv.ParseInt(parts[i], 10, 32); e == nil {
815819
P = append(P, int(n))
816820
}
821+
} else {
822+
P = append(P, 0)
817823
}
818824
}
819825
}
@@ -827,6 +833,7 @@ func (ip *inputParser) handleCsi(mode rune, params []byte, intermediate []byte)
827833
case 'm', 'M': // mouse event, we only do SGR tracking
828834
ip.handleMouse(mode, P)
829835
}
836+
return
830837
}
831838

832839
switch mode {
@@ -841,8 +848,8 @@ func (ip *inputParser) handleCsi(mode rune, params []byte, intermediate []byte)
841848
ip.state = istLnx
842849
return
843850
case 'u':
844-
// CSI-u kitty keyboard protocol
845-
if len(P) > 0 && !hasLT {
851+
// CSI-u kitty keyboard protocol, is unambiguous
852+
if len(P) > 0 {
846853
mod := ModNone
847854
key := KeyRune
848855
chr := rune(0)
@@ -874,7 +881,7 @@ func (ip *inputParser) handleCsi(mode rune, params []byte, intermediate []byte)
874881
return
875882
}
876883
case '~':
877-
if len(P) >= 2 && !hasLT {
884+
if len(P) >= 2 {
878885
mod := calcModifier(P[1])
879886
if ks, ok := csiAllKeys[csiParamMode{M: mode, P: P0}]; ok {
880887
ip.post(NewEventKey(ks.Key, "", mod))
@@ -891,7 +898,7 @@ func (ip *inputParser) handleCsi(mode rune, params []byte, intermediate []byte)
891898
}
892899
}
893900

894-
if ks, ok := csiAllKeys[csiParamMode{M: mode, P: P0}]; ok && !hasLT {
901+
if ks, ok := csiAllKeys[csiParamMode{M: mode, P: P0}]; ok {
895902
if mode == '~' && len(P) > 1 && ks.Mod == ModNone {
896903
// apply modifiers if present
897904
ks.Mod = calcModifier(P[1])

0 commit comments

Comments
 (0)