Skip to content

Commit 858dba0

Browse files
author
SqlRush
committed
Parse CSI insert mode
1 parent 7cf90fb commit 858dba0

5 files changed

Lines changed: 23 additions & 0 deletions

File tree

docs/cc-100-roadmap.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ M7 补充:terminal sequence dispatcher 现在把 SS3 application cursor (`ESC
148148

149149
M7 补充:terminal CSI parser 现在把 DEC `?1h/l` application cursor mode 解析成独立 mode action,和 SS3 application cursor key 解析闭环。
150150

151+
M7 补充:terminal CSI parser 现在把普通 `CSI 4h/l` insert/replace mode 解析成 `insertMode` action,避免 ECMA mode set/reset 序列落入 unknown。
152+
151153
M7 补充:terminal CSI parser 现在把 REP repeat-preceding-character (`CSI b`/`CSI 4b`) 解析成 edit action,visible-text/snapshot pipeline 和 ANSI message wrapping/trim 会按重复次数展开前一个可重复 grapheme。
152154

153155
M7 补充:terminal CSI parser 现在按 ANSI 默认参数解析 scroll-region (`CSI r`/`CSI ;10r`),缺失 top 默认为 1,缺失 bottom 保持为 0 表示 reset/full-height,避免把 reset 误判为单行区域。

docs/claude-code-go-rewrite-plan.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ test/parity/ # golden tests against TS/official behavior
255255
- 本轮补充:terminal ESC parser 把 HTS horizontal-tab-set (`ESC H`) 归入 cursor/tab-set action,和 CSI tab-clear 控制序列形成闭环。
256256
- 本轮补充:terminal sequence dispatcher 把 SS3 application cursor (`ESC OA`/`OB`/`OC`/`OD`) 归入结构化 cursor move action,避免 application cursor mode 序列落入 unknown。
257257
- 本轮补充:terminal CSI parser 把 DEC `?1h/l` application cursor mode 解析成独立 mode action,和 SS3 application cursor key 解析闭环。
258+
- 本轮补充:terminal CSI parser 把普通 `CSI 4h/l` insert/replace mode 解析成 `insertMode` action,避免 ECMA mode set/reset 序列落入 unknown。
258259
- 本轮补充:terminal CSI parser 把 REP repeat-preceding-character (`CSI b`) 归入 edit action,visible-text/snapshot pipeline 和 ANSI message wrapping/trim 会按重复次数展开前一个可重复 grapheme。
259260
- 本轮补充:terminal CSI parser 按 ANSI 默认参数解析 scroll-region (`CSI r`/`CSI ;10r`),缺失 top 默认为 1,缺失 bottom 保持 0 表示 reset/full-height,避免 reset scroll region 被误判为单行区域。
260261
- 本轮补充:terminal CSI parser 把 DECSTR soft reset (`CSI !p`) 归入 reset action,并在 terminal parser 中清理 SGR/link 状态。

docs/first-second-parity-audit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ M7 progress now includes:
208208
- `internal/tui`: terminal CSI parser now recognizes DEC highlight, UTF-8, and urxvt numeric mouse modes (`?1001h/l`, `?1005h/l`, `?1015h/l`) as mouse-tracking mode actions.
209209
- `internal/tui`: terminal CSI parser now recognizes xterm alternate scroll mode (`?1007h/l`) as a structured mode action instead of an unknown sequence.
210210
- `internal/tui`: terminal CSI parser now recognizes DEC application cursor mode (`?1h/l`) as a structured mode action, pairing with SS3 application cursor key parsing.
211+
- `internal/tui`: terminal CSI parser now recognizes ordinary ECMA insert/replace mode (`CSI 4h/l`) as a structured `insertMode` action.
211212
- `internal/tui`: keybinding config, keymap resolution, and interaction script named-key input now accept terminal aliases for `ctrl-h`/`ctrl-i`/`ctrl-j`/`ctrl-m`, `ctrl-[`, and `ctrl-?`, including `control-*` and compact/camel variants.
212213
- `internal/tui`: keybinding config now accepts page navigation aliases such as `pgup`, `pg-up`, `prior`, `pgdn`, `pg-down`, and `next`.
213214
- `internal/tui`: terminal key parsing now accepts CSI-u/kitty keyboard protocol sequences for existing ctrl/alt editing keys, shift-enter, shift-tab, and printable shift-only runes.

internal/tui/terminal_csi.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const (
6363
CSICommandRestoreCursor byte = 'u'
6464
CSICommandTerminalParams byte = 'x'
6565

66+
CSIModeInsert = 4
67+
6668
DECModeApplicationCursor = 1
6769
DECModeCursorVisible = 25
6870
DECModeAltScreen = 47
@@ -219,6 +221,7 @@ const (
219221
CSIModeActionApplicationCursor CSIModeActionType = "applicationCursor"
220222
CSIModeActionAlternateScreen CSIModeActionType = "alternateScreen"
221223
CSIModeActionBracketedPaste CSIModeActionType = "bracketedPaste"
224+
CSIModeActionInsert CSIModeActionType = "insertMode"
222225
CSIModeActionMouseTracking CSIModeActionType = "mouseTracking"
223226
CSIModeActionFocusEvents CSIModeActionType = "focusEvents"
224227
CSIModeActionAlternateScroll CSIModeActionType = "alternateScroll"
@@ -402,6 +405,11 @@ func ParseCSISequence(sequence string) (CSIAction, bool) {
402405
return action, true
403406
}
404407
}
408+
if privateMode == 0 && (final == CSICommandSetMode || final == CSICommandResetMode) {
409+
if action, ok := csiModeAction(p0, final == CSICommandSetMode); ok {
410+
return action, true
411+
}
412+
}
405413

406414
return CSIAction{Type: CSIActionUnknown, Sequence: sequence}, true
407415
}
@@ -588,6 +596,15 @@ func csiPrivateModeAction(mode int, enabled bool) (CSIAction, bool) {
588596
}
589597
}
590598

599+
func csiModeAction(mode int, enabled bool) (CSIAction, bool) {
600+
switch mode {
601+
case CSIModeInsert:
602+
return CSIAction{Type: CSIActionMode, Mode: CSIModeAction{Type: CSIModeActionInsert, Enabled: enabled}}, true
603+
default:
604+
return CSIAction{}, false
605+
}
606+
}
607+
591608
func csiMouseModeAction(mode CSIMouseTrackingMode, enabled bool) CSIAction {
592609
if !enabled {
593610
mode = CSIMouseTrackingOff

internal/tui/tui_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,8 @@ func TestParseCSISequenceActions(t *testing.T) {
36423642
seq string
36433643
want CSIModeAction
36443644
}{
3645+
{seq: "\x1b[4h", want: CSIModeAction{Type: CSIModeActionInsert, Enabled: true}},
3646+
{seq: "\x1b[4l", want: CSIModeAction{Type: CSIModeActionInsert, Enabled: false}},
36453647
{seq: "\x1b[?1h", want: CSIModeAction{Type: CSIModeActionApplicationCursor, Enabled: true}},
36463648
{seq: "\x1b[?1l", want: CSIModeAction{Type: CSIModeActionApplicationCursor, Enabled: false}},
36473649
{seq: EnterAlternateScreen, want: CSIModeAction{Type: CSIModeActionAlternateScreen, Enabled: true}},

0 commit comments

Comments
 (0)