Skip to content

Commit

Permalink
refactor: flatten csi sequence parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 13, 2024
1 parent c2c195c commit 4247aac
Showing 1 changed file with 32 additions and 62 deletions.
94 changes: 32 additions & 62 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,64 +236,36 @@ func parseCsi(b []byte) (int, Msg) {
i++

csi.Params = params[:paramsLen]
marker, cmd := csi.Marker(), csi.Command()
switch marker {
case '?':
switch cmd {
case 'y':
switch intermed {
case '$':
// Report Mode (DECRPM)
if paramsLen != 2 {
return i, UnknownMsg(b[:i])
}
return i, ReportModeMsg{Mode: csi.Param(0), Value: csi.Param(1)}
}
case 'c':
// Primary Device Attributes
return i, parsePrimaryDevAttrs(&csi)
case 'u':
// Kitty keyboard flags
if param := csi.Param(0); param != -1 {
return i, KittyKeyboardMsg(param)
}
case 'R':
// This report may return a third parameter representing the page
// number, but we don't really need it.
if paramsLen >= 2 {
return i, CursorPositionMsg{Row: csi.Param(0), Column: csi.Param(1)}
}
}
return i, UnknownMsg(b[:i])
case '<':
switch cmd {
case 'm', 'M':
// Handle SGR mouse
if paramsLen != 3 {
return i, UnknownMsg(b[:i])
}
switch cmd := csi.Cmd; cmd {
case 'y' | '?'<<parser.MarkerShift | '$'<<parser.IntermedShift:
// Report Mode (DECRPM)
if paramsLen == 2 {

Check failure on line 242 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <condition> detected (gomnd)
return i, ReportModeMsg{Mode: csi.Param(0), Value: csi.Param(1)}
}
case 'c' | '?'<<parser.MarkerShift:
// Primary Device Attributes
return i, parsePrimaryDevAttrs(&csi)
case 'u' | '?'<<parser.MarkerShift:
// Kitty keyboard flags
if param := csi.Param(0); param != -1 {
return i, KittyKeyboardMsg(param)
}
case 'R' | '?'<<parser.MarkerShift:
// This report may return a third parameter representing the page
// number, but we don't really need it.
if paramsLen >= 2 {

Check failure on line 256 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <condition> detected (gomnd)
return i, CursorPositionMsg{Row: csi.Param(0), Column: csi.Param(1)}
}
case 'm' | '<'<<parser.MarkerShift, 'M' | '<'<<parser.MarkerShift:
// Handle SGR mouse
if paramsLen == 3 {

Check failure on line 261 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 3, in <condition> detected (gomnd)
return i, parseSGRMouseEvent(&csi)
default:
return i, UnknownMsg(b[:i])
}
case '>':
switch cmd {
case 'm':
// XTerm modifyOtherKeys
if paramsLen != 2 || csi.Param(0) != 4 {
return i, UnknownMsg(b[:i])
}

case 'm' | '>'<<parser.MarkerShift:
// XTerm modifyOtherKeys
if paramsLen == 2 && csi.Param(0) == 4 {
return i, ModifyOtherKeysMsg(csi.Param(1))
default:
return i, UnknownMsg(b[:i])
}
case '=':
// We don't support any of these yet
return i, UnknownMsg(b[:i])
}

switch cmd := csi.Command(); cmd {
case 'I':
return i, FocusMsg{}
case 'O':
Expand All @@ -316,7 +288,7 @@ func parseCsi(b []byte) (int, Msg) {
}

if paramsLen != 0 {
return i, UnknownMsg(b[:i])
break
}

// Unmodified key F3 (CSI R)
Expand Down Expand Up @@ -414,13 +386,11 @@ func parseCsi(b []byte) (int, Msg) {
}

switch param {
case 1, 2, 3, 4, 5, 6, 7, 8:
fallthrough
case 11, 12, 13, 14, 15:
fallthrough
case 17, 18, 19, 20, 21, 23, 24, 25, 26:
fallthrough
case 28, 29, 31, 32, 33, 34:
case 1, 2, 3, 4, 5, 6, 7, 8,

Check failure on line 389 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <case> detected (gomnd)
11, 12, 13, 14, 15,

Check failure on line 390 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 11, in <case> detected (gomnd)
17, 18, 19, 20, 21,

Check failure on line 391 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 17, in <case> detected (gomnd)
23, 24, 25, 26,

Check failure on line 392 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 23, in <case> detected (gomnd)
28, 29, 31, 32, 33, 34:

Check failure on line 393 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 28, in <case> detected (gomnd)
var k KeyPressMsg
switch param {
case 1:
Expand Down

0 comments on commit 4247aac

Please sign in to comment.