chivgent 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/README.zh-CN.md +7 -7
- package/dist/agent.d.ts +10 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +57 -7
- package/dist/agent.js.map +1 -1
- package/dist/cli-options.d.ts +2 -0
- package/dist/cli-options.d.ts.map +1 -1
- package/dist/cli-options.js +4 -0
- package/dist/cli-options.js.map +1 -1
- package/dist/cli-runtime.d.ts +3 -0
- package/dist/cli-runtime.d.ts.map +1 -1
- package/dist/cli-runtime.js +31 -6
- package/dist/cli-runtime.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +47 -15
- package/dist/cli.js.map +1 -1
- package/dist/evals/cli.js +5 -5
- package/dist/evals/cli.js.map +1 -1
- package/dist/events.d.ts +5 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -1
- package/dist/permissions.d.ts +3 -2
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +10 -5
- package/dist/permissions.js.map +1 -1
- package/dist/prompts.d.ts +8 -3
- package/dist/prompts.d.ts.map +1 -1
- package/dist/prompts.js +12 -3
- package/dist/prompts.js.map +1 -1
- package/dist/providers/deepseek.d.ts +1 -0
- package/dist/providers/deepseek.d.ts.map +1 -1
- package/dist/providers/deepseek.js +2 -2
- package/dist/providers/deepseek.js.map +1 -1
- package/dist/providers/interactive.d.ts +9 -3
- package/dist/providers/interactive.d.ts.map +1 -1
- package/dist/providers/interactive.js +116 -19
- package/dist/providers/interactive.js.map +1 -1
- package/dist/questions.d.ts +58 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +155 -0
- package/dist/questions.js.map +1 -0
- package/dist/remote/server.d.ts +6 -1
- package/dist/remote/server.d.ts.map +1 -1
- package/dist/remote/server.js +100 -16
- package/dist/remote/server.js.map +1 -1
- package/dist/repl.d.ts +8 -7
- package/dist/repl.d.ts.map +1 -1
- package/dist/repl.js +317 -310
- package/dist/repl.js.map +1 -1
- package/dist/runtime/desktop-runtime.d.ts +18 -0
- package/dist/runtime/desktop-runtime.d.ts.map +1 -1
- package/dist/runtime/desktop-runtime.js +19 -5
- package/dist/runtime/desktop-runtime.js.map +1 -1
- package/dist/session.d.ts +3 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +5 -0
- package/dist/session.js.map +1 -1
- package/dist/shell/approval.d.ts +12 -2
- package/dist/shell/approval.d.ts.map +1 -1
- package/dist/shell/approval.js +16 -5
- package/dist/shell/approval.js.map +1 -1
- package/dist/shell/output-store.d.ts +49 -0
- package/dist/shell/output-store.d.ts.map +1 -0
- package/dist/shell/output-store.js +250 -0
- package/dist/shell/output-store.js.map +1 -0
- package/dist/shell/output.d.ts +23 -2
- package/dist/shell/output.d.ts.map +1 -1
- package/dist/shell/output.js +106 -24
- package/dist/shell/output.js.map +1 -1
- package/dist/tools/bash.d.ts +6 -4
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/bash.js +39 -19
- package/dist/tools/bash.js.map +1 -1
- package/dist/tools/list-files.d.ts.map +1 -1
- package/dist/tools/list-files.js +3 -0
- package/dist/tools/list-files.js.map +1 -1
- package/dist/tools/parallel.d.ts.map +1 -1
- package/dist/tools/parallel.js +5 -1
- package/dist/tools/parallel.js.map +1 -1
- package/dist/tools/question.d.ts +72 -0
- package/dist/tools/question.d.ts.map +1 -0
- package/dist/tools/question.js +52 -0
- package/dist/tools/question.js.map +1 -0
- package/dist/tools/read-shell-output.d.ts +32 -0
- package/dist/tools/read-shell-output.d.ts.map +1 -0
- package/dist/tools/read-shell-output.js +39 -0
- package/dist/tools/read-shell-output.js.map +1 -0
- package/dist/tools/search-text.d.ts +6 -2
- package/dist/tools/search-text.d.ts.map +1 -1
- package/dist/tools/search-text.js +24 -8
- package/dist/tools/search-text.js.map +1 -1
- package/dist/tools/tool.d.ts +9 -0
- package/dist/tools/tool.d.ts.map +1 -1
- package/dist/tools/tool.js.map +1 -1
- package/dist/tui/activity.d.ts.map +1 -1
- package/dist/tui/activity.js +52 -13
- package/dist/tui/activity.js.map +1 -1
- package/dist/tui/animation.d.ts +21 -0
- package/dist/tui/animation.d.ts.map +1 -0
- package/dist/tui/animation.js +97 -0
- package/dist/tui/animation.js.map +1 -0
- package/dist/tui/editor-input.d.ts +20 -38
- package/dist/tui/editor-input.d.ts.map +1 -1
- package/dist/tui/editor-input.js +136 -347
- package/dist/tui/editor-input.js.map +1 -1
- package/dist/tui/editor.d.ts +11 -24
- package/dist/tui/editor.d.ts.map +1 -1
- package/dist/tui/editor.js +79 -147
- package/dist/tui/editor.js.map +1 -1
- package/dist/tui/git-diff.d.ts.map +1 -1
- package/dist/tui/git-diff.js +2 -23
- package/dist/tui/git-diff.js.map +1 -1
- package/dist/tui/input.d.ts +12 -16
- package/dist/tui/input.d.ts.map +1 -1
- package/dist/tui/input.js +89 -127
- package/dist/tui/input.js.map +1 -1
- package/dist/tui/live.d.ts +4 -0
- package/dist/tui/live.d.ts.map +1 -1
- package/dist/tui/live.js +18 -8
- package/dist/tui/live.js.map +1 -1
- package/dist/tui/paint.d.ts +3 -0
- package/dist/tui/paint.d.ts.map +1 -1
- package/dist/tui/paint.js +9 -2
- package/dist/tui/paint.js.map +1 -1
- package/dist/tui/provider-picker.d.ts +9 -6
- package/dist/tui/provider-picker.d.ts.map +1 -1
- package/dist/tui/provider-picker.js +11 -9
- package/dist/tui/provider-picker.js.map +1 -1
- package/dist/tui/state.d.ts +1 -1
- package/dist/tui/state.d.ts.map +1 -1
- package/dist/tui/state.js +9 -1
- package/dist/tui/state.js.map +1 -1
- package/dist/tui/view.d.ts +34 -0
- package/dist/tui/view.d.ts.map +1 -1
- package/dist/tui/view.js +142 -57
- package/dist/tui/view.js.map +1 -1
- package/dist/tui/welcome.d.ts +4 -4
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +58 -25
- package/dist/tui/welcome.js.map +1 -1
- package/dist/websearch/backend.d.ts.map +1 -1
- package/dist/websearch/backend.js +10 -6
- package/dist/websearch/backend.js.map +1 -1
- package/dist/workspace/fs.d.ts +4 -0
- package/dist/workspace/fs.d.ts.map +1 -0
- package/dist/workspace/fs.js +6 -0
- package/dist/workspace/fs.js.map +1 -0
- package/dist/workspace/git-read.d.ts +3 -0
- package/dist/workspace/git-read.d.ts.map +1 -0
- package/dist/workspace/git-read.js +23 -0
- package/dist/workspace/git-read.js.map +1 -0
- package/dist/workspace/list.d.ts.map +1 -1
- package/dist/workspace/list.js +9 -5
- package/dist/workspace/list.js.map +1 -1
- package/dist/workspace/search.d.ts +1 -0
- package/dist/workspace/search.d.ts.map +1 -1
- package/dist/workspace/search.js +38 -6
- package/dist/workspace/search.js.map +1 -1
- package/dist/workspace/text.d.ts +1 -1
- package/dist/workspace/text.d.ts.map +1 -1
- package/dist/workspace/text.js +2 -3
- package/dist/workspace/text.js.map +1 -1
- package/dist/workspace/types.d.ts +7 -1
- package/dist/workspace/types.d.ts.map +1 -1
- package/dist/workspace/types.js.map +1 -1
- package/package.json +1 -1
- package/dist/tui/menu.d.ts +0 -26
- package/dist/tui/menu.d.ts.map +0 -1
- package/dist/tui/menu.js +0 -75
- package/dist/tui/menu.js.map +0 -1
package/dist/tui/editor-input.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { StringDecoder } from "node:string_decoder";
|
|
2
2
|
import { EditorDocument, EditorPainter, editorFrame } from "./editor.js";
|
|
3
3
|
import { slashSuggestions } from "./slash.js";
|
|
4
|
-
const MOUSE_ENABLE = "\u001b[?1000h\u001b[?1002h\u001b[?1006h";
|
|
5
|
-
const MOUSE_DISABLE = "\u001b[?1006l\u001b[?1002l\u001b[?1000l";
|
|
6
4
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* Keyboard-only parser for the inline rounded composer.
|
|
6
|
+
* Never enables terminal mouse tracking so native selection and scrollback
|
|
7
|
+
* continue to work undisturbed.
|
|
10
8
|
*/
|
|
11
9
|
export class EditorController {
|
|
12
|
-
output;
|
|
13
10
|
done;
|
|
14
11
|
options;
|
|
15
12
|
document = new EditorDocument();
|
|
@@ -19,26 +16,18 @@ export class EditorController {
|
|
|
19
16
|
pasting = false;
|
|
20
17
|
escapeTimer;
|
|
21
18
|
completed = false;
|
|
22
|
-
previousCR = false;
|
|
23
19
|
completionIndex = 0;
|
|
24
20
|
dismissedCommand;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
mouseDragging = false;
|
|
28
|
-
mouseSuspended = false;
|
|
29
|
-
exitRestore;
|
|
21
|
+
historyIndex;
|
|
22
|
+
historyDraft = "";
|
|
30
23
|
modeSwitchPending = false;
|
|
31
24
|
modeSwitchCancelPending = false;
|
|
32
25
|
modeSwitchQueue = [];
|
|
33
26
|
modeSwitchQueueBytes = 0;
|
|
34
|
-
searching = false;
|
|
35
|
-
searchQuery = "";
|
|
36
|
-
searchOffset = 0;
|
|
37
27
|
exitRequested = { value: false };
|
|
38
28
|
maxWidth;
|
|
39
29
|
maxHeight;
|
|
40
30
|
constructor(output, done, options = {}) {
|
|
41
|
-
this.output = output;
|
|
42
31
|
this.done = done;
|
|
43
32
|
this.options = options;
|
|
44
33
|
this.painter = new EditorPainter(output);
|
|
@@ -46,154 +35,11 @@ export class EditorController {
|
|
|
46
35
|
this.maxHeight = () => Math.max(6, output.rows ?? 24);
|
|
47
36
|
}
|
|
48
37
|
begin() {
|
|
49
|
-
if (this.options.mouse) {
|
|
50
|
-
this.painterMouse(true);
|
|
51
|
-
this.installExitRestore();
|
|
52
|
-
}
|
|
53
38
|
this.draw();
|
|
54
39
|
}
|
|
55
|
-
painterMouse(enabled) {
|
|
56
|
-
// Opt-in only while editor owns the terminal; restore normal selection on exit.
|
|
57
|
-
this.outputWrite(enabled ? MOUSE_ENABLE : MOUSE_DISABLE);
|
|
58
|
-
}
|
|
59
|
-
installExitRestore() {
|
|
60
|
-
if (this.exitRestore !== undefined)
|
|
61
|
-
return;
|
|
62
|
-
const restore = () => {
|
|
63
|
-
try {
|
|
64
|
-
this.outputWrite(MOUSE_DISABLE);
|
|
65
|
-
}
|
|
66
|
-
catch { /* the terminal may already be gone */ }
|
|
67
|
-
};
|
|
68
|
-
this.exitRestore = restore;
|
|
69
|
-
process.once("exit", restore);
|
|
70
|
-
}
|
|
71
|
-
removeExitRestore() {
|
|
72
|
-
if (this.exitRestore === undefined)
|
|
73
|
-
return;
|
|
74
|
-
process.off("exit", this.exitRestore);
|
|
75
|
-
this.exitRestore = undefined;
|
|
76
|
-
}
|
|
77
|
-
/** Hand the wheel to the terminal so its own scrollback moves. */
|
|
78
|
-
suspendMouseTracking() {
|
|
79
|
-
if (!this.options.mouse || this.mouseSuspended)
|
|
80
|
-
return;
|
|
81
|
-
this.mouseSuspended = true;
|
|
82
|
-
this.originRow = undefined;
|
|
83
|
-
this.mouseDragging = false;
|
|
84
|
-
this.painterMouse(false);
|
|
85
|
-
}
|
|
86
|
-
resumeMouseTracking() {
|
|
87
|
-
if (!this.options.mouse || !this.mouseSuspended)
|
|
88
|
-
return;
|
|
89
|
-
this.mouseSuspended = false;
|
|
90
|
-
this.originRow = undefined;
|
|
91
|
-
this.painterMouse(true);
|
|
92
|
-
}
|
|
93
|
-
outputWrite(chunk) { this.output.write(chunk); }
|
|
94
|
-
suggestions() {
|
|
95
|
-
if (this.dismissedCommand === this.document.text)
|
|
96
|
-
return [];
|
|
97
|
-
return slashSuggestions(this.document.text, this.document.cursor, this.options.slashCommands ?? []);
|
|
98
|
-
}
|
|
99
|
-
insertSelectedCompletion() {
|
|
100
|
-
const matches = this.suggestions();
|
|
101
|
-
const match = matches[this.completionIndex % matches.length];
|
|
102
|
-
if (match === undefined)
|
|
103
|
-
return false;
|
|
104
|
-
this.document.loadDraft(`/${match.value}`);
|
|
105
|
-
this.dismissedCommand = this.document.text;
|
|
106
|
-
this.completionIndex = 0;
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
/** True only when the rendered frame needs repainting. */
|
|
110
|
-
processMouse(sequence) {
|
|
111
|
-
const answer = /^\u001b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(sequence);
|
|
112
|
-
if (answer === null)
|
|
113
|
-
return false;
|
|
114
|
-
if (this.mouseSuspended)
|
|
115
|
-
return false;
|
|
116
|
-
const button = Number(answer[1]);
|
|
117
|
-
const column = Number(answer[2]);
|
|
118
|
-
const row = Number(answer[3]);
|
|
119
|
-
if ((button & 64) !== 0) {
|
|
120
|
-
// Wheel over a visible completion changes the selection; over a draft row
|
|
121
|
-
// it moves the caret. Anywhere else belongs to the terminal's scrollback.
|
|
122
|
-
if (answer[4] !== "M")
|
|
123
|
-
return false;
|
|
124
|
-
if ((button & 2) !== 0)
|
|
125
|
-
return false;
|
|
126
|
-
const direction = (button & 1) === 0 ? -1 : 1;
|
|
127
|
-
const frameRow = this.frameRowAt(row);
|
|
128
|
-
if (frameRow !== undefined) {
|
|
129
|
-
const suggestion = this.lastFrame?.suggestionRows?.find(item => item.row === frameRow);
|
|
130
|
-
if (suggestion !== undefined && column >= 1 && column <= this.maxWidth()) {
|
|
131
|
-
const max = this.suggestions().length - 1;
|
|
132
|
-
this.completionIndex = Math.max(0, Math.min(max, this.completionIndex + direction * 3));
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
const inputRow = this.lastFrame?.inputRows?.[frameRow - 1];
|
|
136
|
-
if (inputRow !== undefined) {
|
|
137
|
-
for (let i = 0; i < 3; i += 1)
|
|
138
|
-
this.document.moveVertical(direction);
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
// Outside the draft and its menu: hand wheel-up to the terminal.
|
|
142
|
-
if (direction < 0)
|
|
143
|
-
this.suspendMouseTracking();
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
// No frame mapping yet: preserve the existing fallback.
|
|
147
|
-
for (let i = 0; i < 3; i += 1)
|
|
148
|
-
this.document.moveVertical(direction);
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
if (button === 0 && answer[4] === "m") {
|
|
152
|
-
const wasDragging = this.mouseDragging;
|
|
153
|
-
this.mouseDragging = false;
|
|
154
|
-
return wasDragging;
|
|
155
|
-
}
|
|
156
|
-
if (button !== 0 && button !== 32)
|
|
157
|
-
return false;
|
|
158
|
-
if (button === 32 && !this.mouseDragging)
|
|
159
|
-
return false;
|
|
160
|
-
if (this.originRow === undefined || this.lastFrame === undefined)
|
|
161
|
-
return false;
|
|
162
|
-
if (column < 1 || column > this.maxWidth())
|
|
163
|
-
return false;
|
|
164
|
-
const relative = row - 1 - this.originRow;
|
|
165
|
-
const commandRow = this.lastFrame.suggestionRows?.find(item => item.row === relative);
|
|
166
|
-
if (button === 0 && answer[4] === "M" && commandRow !== undefined) {
|
|
167
|
-
this.completionIndex = commandRow.commandIndex;
|
|
168
|
-
this.insertSelectedCompletion();
|
|
169
|
-
return true;
|
|
170
|
-
}
|
|
171
|
-
const inputRow = this.lastFrame.inputRows?.[relative - 1];
|
|
172
|
-
// Only visible editable rows map to document text; header, popup and footer
|
|
173
|
-
// coordinates must never be interpreted as text positions.
|
|
174
|
-
if (inputRow === undefined)
|
|
175
|
-
return false;
|
|
176
|
-
const frameColumn = Math.max(0, column - 1 - inputRow.gutterCells);
|
|
177
|
-
const visibleTextColumn = inputRow.scrollCells > 0
|
|
178
|
-
? Math.max(0, frameColumn - 1)
|
|
179
|
-
: frameColumn;
|
|
180
|
-
this.document.placeAt(inputRow.documentRow, visibleTextColumn + inputRow.scrollCells, button === 32);
|
|
181
|
-
this.mouseDragging = true;
|
|
182
|
-
return true;
|
|
183
|
-
}
|
|
184
|
-
frameRowAt(screenRow) {
|
|
185
|
-
if (this.originRow === undefined || this.lastFrame === undefined)
|
|
186
|
-
return undefined;
|
|
187
|
-
return screenRow - 1 - this.originRow;
|
|
188
|
-
}
|
|
189
40
|
resized() {
|
|
190
|
-
if (this.completed)
|
|
191
|
-
|
|
192
|
-
// A resize invalidates the mapping and needs a repaint. Take the mouse back
|
|
193
|
-
// so the new frame can be hit-tested.
|
|
194
|
-
this.resumeMouseTracking();
|
|
195
|
-
this.originRow = undefined;
|
|
196
|
-
this.draw();
|
|
41
|
+
if (!this.completed)
|
|
42
|
+
this.draw();
|
|
197
43
|
}
|
|
198
44
|
receive(chunk) {
|
|
199
45
|
if (this.completed)
|
|
@@ -224,96 +70,125 @@ export class EditorController {
|
|
|
224
70
|
if (!this.completed && changed)
|
|
225
71
|
this.draw();
|
|
226
72
|
}
|
|
227
|
-
|
|
73
|
+
cancel() {
|
|
74
|
+
if (this.modeSwitchPending) {
|
|
75
|
+
this.modeSwitchCancelPending = true;
|
|
76
|
+
this.modeSwitchQueue = [];
|
|
77
|
+
this.modeSwitchQueueBytes = 0;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.finish(undefined);
|
|
81
|
+
}
|
|
82
|
+
suggestions() {
|
|
83
|
+
if (this.dismissedCommand === this.document.text)
|
|
84
|
+
return [];
|
|
85
|
+
return slashSuggestions(this.document.text, this.document.cursor, this.options.slashCommands ?? []);
|
|
86
|
+
}
|
|
87
|
+
insertSelectedCompletion() {
|
|
88
|
+
const matches = this.suggestions();
|
|
89
|
+
const match = matches[this.completionIndex % matches.length];
|
|
90
|
+
if (match === undefined)
|
|
91
|
+
return false;
|
|
92
|
+
this.document.loadDraft(`/${match.value}`);
|
|
93
|
+
this.dismissedCommand = this.document.text;
|
|
94
|
+
this.completionIndex = 0;
|
|
95
|
+
this.historyIndex = undefined;
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
navigateVertical(direction) {
|
|
99
|
+
const matches = this.suggestions();
|
|
100
|
+
if (matches.length > 0) {
|
|
101
|
+
this.completionIndex = Math.max(0, Math.min(matches.length - 1, this.completionIndex + direction));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
228
104
|
const history = this.options.history ?? [];
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
105
|
+
if (direction < 0 && this.document.position.row === 0 && history.length > 0) {
|
|
106
|
+
if (this.historyIndex === undefined) {
|
|
107
|
+
this.historyDraft = this.document.text;
|
|
108
|
+
this.historyIndex = history.length - 1;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
this.historyIndex = Math.max(0, this.historyIndex - 1);
|
|
112
|
+
}
|
|
113
|
+
this.document.loadDraft(history[this.historyIndex]);
|
|
114
|
+
this.dismissedCommand = undefined;
|
|
115
|
+
this.completionIndex = 0;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (direction > 0 && this.historyIndex !== undefined &&
|
|
119
|
+
this.document.position.row === this.document.text.split("\n").length - 1) {
|
|
120
|
+
if (this.historyIndex < history.length - 1) {
|
|
121
|
+
this.historyIndex += 1;
|
|
122
|
+
this.document.loadDraft(history[this.historyIndex]);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this.historyIndex = undefined;
|
|
126
|
+
this.document.loadDraft(this.historyDraft);
|
|
127
|
+
}
|
|
128
|
+
this.dismissedCommand = undefined;
|
|
129
|
+
this.completionIndex = 0;
|
|
232
130
|
return;
|
|
233
131
|
}
|
|
234
|
-
this.
|
|
235
|
-
|
|
236
|
-
|
|
132
|
+
this.document.moveVertical(direction);
|
|
133
|
+
}
|
|
134
|
+
triggerShiftTab() {
|
|
135
|
+
if (this.modeSwitchPending)
|
|
136
|
+
return;
|
|
137
|
+
this.modeSwitchPending = true;
|
|
138
|
+
Promise.resolve().then(() => this.options.onShiftTab?.()).catch(error => {
|
|
139
|
+
this.document.warning = `Could not change mode: ${error instanceof Error ? error.message : String(error)}`;
|
|
140
|
+
}).finally(() => {
|
|
141
|
+
this.modeSwitchPending = false;
|
|
142
|
+
if (this.completed)
|
|
143
|
+
return;
|
|
144
|
+
if (this.modeSwitchCancelPending) {
|
|
145
|
+
this.modeSwitchCancelPending = false;
|
|
146
|
+
this.modeSwitchQueue = [];
|
|
147
|
+
this.modeSwitchQueueBytes = 0;
|
|
148
|
+
this.finish(undefined);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.parse();
|
|
152
|
+
const queued = this.modeSwitchQueue;
|
|
153
|
+
this.modeSwitchQueue = [];
|
|
154
|
+
this.modeSwitchQueueBytes = 0;
|
|
155
|
+
for (const chunk of queued)
|
|
156
|
+
this.receive(chunk);
|
|
157
|
+
if (!this.completed)
|
|
158
|
+
this.draw();
|
|
159
|
+
});
|
|
237
160
|
}
|
|
238
161
|
parse() {
|
|
239
162
|
let changed = false;
|
|
240
163
|
const startPaste = "\u001b[200~";
|
|
241
164
|
const endPaste = "\u001b[201~";
|
|
242
165
|
const escapes = {
|
|
243
|
-
"\u001b[A": () => this.
|
|
244
|
-
"\
|
|
166
|
+
"\u001b[A": () => this.navigateVertical(-1),
|
|
167
|
+
"\u001bOA": () => this.navigateVertical(-1),
|
|
168
|
+
"\u001b[B": () => this.navigateVertical(1),
|
|
169
|
+
"\u001bOB": () => this.navigateVertical(1),
|
|
245
170
|
"\u001b[C": () => this.document.right(),
|
|
246
171
|
"\u001b[D": () => this.document.left(),
|
|
247
172
|
"\u001b[H": () => this.document.home(),
|
|
248
173
|
"\u001b[F": () => this.document.end(),
|
|
249
174
|
"\u001b[1~": () => this.document.home(),
|
|
250
175
|
"\u001b[4~": () => this.document.end(),
|
|
251
|
-
"\u001b[3~": () =>
|
|
176
|
+
"\u001b[3~": () => {
|
|
177
|
+
this.document.delete();
|
|
178
|
+
this.historyIndex = undefined;
|
|
179
|
+
},
|
|
252
180
|
"\u001bOH": () => this.document.home(),
|
|
253
181
|
"\u001bOF": () => this.document.end(),
|
|
254
|
-
...(this.options.
|
|
255
|
-
"\u001b[Z": () =>
|
|
256
|
-
|
|
257
|
-
return;
|
|
258
|
-
this.modeSwitchPending = true;
|
|
259
|
-
Promise.resolve().then(() => this.options.onShiftTab?.()).catch(error => {
|
|
260
|
-
this.document.warning = `Could not change mode: ${error instanceof Error ? error.message : String(error)}`;
|
|
261
|
-
}).finally(() => {
|
|
262
|
-
this.modeSwitchPending = false;
|
|
263
|
-
if (!this.completed) {
|
|
264
|
-
if (this.modeSwitchCancelPending) {
|
|
265
|
-
this.modeSwitchCancelPending = false;
|
|
266
|
-
this.modeSwitchQueue = [];
|
|
267
|
-
this.modeSwitchQueueBytes = 0;
|
|
268
|
-
this.finish(undefined);
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
this.parse();
|
|
272
|
-
const queued = this.modeSwitchQueue;
|
|
273
|
-
this.modeSwitchQueue = [];
|
|
274
|
-
this.modeSwitchQueueBytes = 0;
|
|
275
|
-
for (const chunk of queued)
|
|
276
|
-
this.receive(chunk);
|
|
277
|
-
if (!this.completed)
|
|
278
|
-
this.draw();
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
},
|
|
282
|
-
} : {}),
|
|
182
|
+
...(this.options.onShiftTab === undefined ? {} : {
|
|
183
|
+
"\u001b[Z": () => this.triggerShiftTab(),
|
|
184
|
+
}),
|
|
283
185
|
};
|
|
284
186
|
while (this.pending.length > 0 && !this.completed && !this.modeSwitchPending) {
|
|
285
|
-
if (this.mouseSuspended) {
|
|
286
|
-
// While suspended, consume in-flight mouse and cursor-position reports
|
|
287
|
-
// without repainting. The first real input takes the mouse back.
|
|
288
|
-
const report = /^\u001b\[<\d+;\d+;\d+[Mm]|^\u001b\[\d+;\d+R/.exec(this.pending);
|
|
289
|
-
if (report !== null) {
|
|
290
|
-
this.pending = this.pending.slice(report[0].length);
|
|
291
|
-
continue;
|
|
292
|
-
}
|
|
293
|
-
if (this.pending === "\u001b" || this.pending === "\u001b[" || this.pending === "\u001bO" ||
|
|
294
|
-
/^\u001b\[<[\d;]*$/.test(this.pending) || /^\u001b\[\d+(;\d+)?$/.test(this.pending)) {
|
|
295
|
-
// Hold a chunked report or a real Escape for the usual short window.
|
|
296
|
-
if (this.pending.length < 16) {
|
|
297
|
-
this.escapeTimer = setTimeout(() => {
|
|
298
|
-
if (this.pending === "\u001b") {
|
|
299
|
-
this.pending = "";
|
|
300
|
-
this.finish(undefined);
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
this.pending = "";
|
|
304
|
-
this.draw();
|
|
305
|
-
}
|
|
306
|
-
}, 40);
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
this.resumeMouseTracking();
|
|
311
|
-
changed = true;
|
|
312
|
-
}
|
|
313
187
|
if (this.pasting) {
|
|
314
188
|
const index = this.pending.indexOf(endPaste);
|
|
315
189
|
if (index !== -1) {
|
|
316
190
|
this.document.insert(this.pending.slice(0, index));
|
|
191
|
+
this.historyIndex = undefined;
|
|
317
192
|
changed = true;
|
|
318
193
|
this.pending = this.pending.slice(index + endPaste.length);
|
|
319
194
|
this.pasting = false;
|
|
@@ -323,6 +198,7 @@ export class EditorController {
|
|
|
323
198
|
const safe = Math.max(0, this.pending.length - endPaste.length);
|
|
324
199
|
if (safe > 0) {
|
|
325
200
|
this.document.insert(this.pending.slice(0, safe));
|
|
201
|
+
this.historyIndex = undefined;
|
|
326
202
|
changed = true;
|
|
327
203
|
this.pending = this.pending.slice(safe);
|
|
328
204
|
}
|
|
@@ -335,56 +211,27 @@ export class EditorController {
|
|
|
335
211
|
continue;
|
|
336
212
|
}
|
|
337
213
|
if (this.pending[0] === "\u001b") {
|
|
338
|
-
const dsr = /^\u001b\[(\d+);(\d+)R/.exec(this.pending);
|
|
339
|
-
if (dsr !== null) {
|
|
340
|
-
if (this.lastFrame !== undefined)
|
|
341
|
-
this.originRow = Number(dsr[1]) - 1 - this.lastFrame.cursorRow;
|
|
342
|
-
this.pending = this.pending.slice(dsr[0].length);
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
const mouse = /^\u001b\[<\d+;\d+;\d+[Mm]/.exec(this.pending);
|
|
346
|
-
if (mouse !== null) {
|
|
347
|
-
if (this.processMouse(mouse[0]))
|
|
348
|
-
changed = true;
|
|
349
|
-
this.pending = this.pending.slice(mouse[0].length);
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
352
214
|
const matched = Object.keys(escapes).find(sequence => this.pending.startsWith(sequence));
|
|
353
215
|
if (matched !== undefined) {
|
|
354
|
-
if (matched === "\u001b[Z" && this.searching) {
|
|
355
|
-
this.pending = this.pending.slice(matched.length);
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
216
|
changed = true;
|
|
359
|
-
|
|
360
|
-
const matches = this.suggestions();
|
|
361
|
-
this.completionIndex = Math.max(0, Math.min(matches.length - 1, this.completionIndex + (matched === "\u001b[B" ? 1 : -1)));
|
|
362
|
-
}
|
|
363
|
-
else
|
|
364
|
-
escapes[matched]?.();
|
|
217
|
+
escapes[matched]?.();
|
|
365
218
|
this.pending = this.pending.slice(matched.length);
|
|
366
219
|
continue;
|
|
367
220
|
}
|
|
368
221
|
if (this.pending === "\u001b" || this.pending.startsWith("\u001b[") ||
|
|
369
222
|
this.pending.startsWith("\u001bO") || startPaste.startsWith(this.pending)) {
|
|
370
|
-
// An isolated Escape
|
|
371
|
-
// arrows and bracketed-paste delimiters.
|
|
223
|
+
// An isolated Escape dismisses suggestions or cancels, after briefly waiting for split CSI sequences.
|
|
372
224
|
if (this.pending.length < 16) {
|
|
373
225
|
this.escapeTimer = setTimeout(() => {
|
|
374
226
|
if (this.pending === "\u001b") {
|
|
375
|
-
if (this.
|
|
376
|
-
this.searching = false;
|
|
377
|
-
this.pending = "";
|
|
378
|
-
this.document.warning = "";
|
|
379
|
-
this.draw();
|
|
380
|
-
}
|
|
381
|
-
else if (this.suggestions().length > 0) {
|
|
227
|
+
if (this.suggestions().length > 0) {
|
|
382
228
|
this.dismissedCommand = this.document.text;
|
|
383
229
|
this.pending = "";
|
|
384
230
|
this.draw();
|
|
385
231
|
}
|
|
386
|
-
else
|
|
232
|
+
else {
|
|
387
233
|
this.finish(undefined);
|
|
234
|
+
}
|
|
388
235
|
}
|
|
389
236
|
else {
|
|
390
237
|
this.pending = "";
|
|
@@ -394,7 +241,7 @@ export class EditorController {
|
|
|
394
241
|
break;
|
|
395
242
|
}
|
|
396
243
|
}
|
|
397
|
-
// Unknown CSI: discard its bytes
|
|
244
|
+
// Unknown CSI/ESC sequence: discard its bytes without corrupting the draft.
|
|
398
245
|
const end = this.pending.slice(1).search(/[A-Za-z~]/);
|
|
399
246
|
this.pending = end < 0 ? "" : this.pending.slice(end + 2);
|
|
400
247
|
changed = true;
|
|
@@ -403,71 +250,31 @@ export class EditorController {
|
|
|
403
250
|
const char = this.pending[0] ?? "";
|
|
404
251
|
this.pending = this.pending.slice(1);
|
|
405
252
|
changed = true;
|
|
406
|
-
if (this.searching) {
|
|
407
|
-
if (char === "\r" || char === "\n") {
|
|
408
|
-
const match = [...(this.options.history ?? [])].reverse().filter(value => value.toLowerCase().includes(this.searchQuery.toLowerCase()))[this.searchOffset];
|
|
409
|
-
if (match !== undefined)
|
|
410
|
-
this.document.loadDraft(match);
|
|
411
|
-
this.searching = false;
|
|
412
|
-
this.document.warning = "";
|
|
413
|
-
}
|
|
414
|
-
else if (char === "\u0003" || char === "\u001b") {
|
|
415
|
-
this.searching = false;
|
|
416
|
-
this.document.warning = "";
|
|
417
|
-
}
|
|
418
|
-
else if (char === "\u0012") {
|
|
419
|
-
this.searchOffset += 1;
|
|
420
|
-
this.updateSearch();
|
|
421
|
-
}
|
|
422
|
-
else if (char === "\u007f" || char === "\b") {
|
|
423
|
-
this.searchQuery = this.searchQuery.slice(0, -1);
|
|
424
|
-
this.searchOffset = 0;
|
|
425
|
-
this.updateSearch();
|
|
426
|
-
}
|
|
427
|
-
else if (char >= " ") {
|
|
428
|
-
this.searchQuery += char;
|
|
429
|
-
this.searchOffset = 0;
|
|
430
|
-
this.updateSearch();
|
|
431
|
-
}
|
|
432
|
-
continue;
|
|
433
|
-
}
|
|
434
253
|
switch (char) {
|
|
435
|
-
case "\u0003": // Ctrl+C cancels only this
|
|
254
|
+
case "\u0003": // Ctrl+C clears/cancels only this draft, not the REPL.
|
|
436
255
|
this.finish(undefined);
|
|
437
256
|
return changed;
|
|
438
|
-
case "\u0004": // Ctrl+D exits
|
|
257
|
+
case "\u0004": // Ctrl+D exits only when the draft is empty.
|
|
439
258
|
if (this.document.text.length === 0) {
|
|
440
259
|
this.exitRequested.value = true;
|
|
441
260
|
this.finish(undefined);
|
|
442
261
|
}
|
|
443
262
|
break;
|
|
444
|
-
case "\
|
|
445
|
-
this.searching = true;
|
|
446
|
-
this.searchQuery = "";
|
|
447
|
-
this.searchOffset = 0;
|
|
448
|
-
this.updateSearch();
|
|
449
|
-
break;
|
|
450
|
-
case "\u001a":
|
|
263
|
+
case "\u001a": // Ctrl+Z
|
|
451
264
|
this.document.undo();
|
|
452
|
-
|
|
453
|
-
|
|
265
|
+
this.historyIndex = undefined;
|
|
266
|
+
break;
|
|
267
|
+
case "\u0019": // Ctrl+Y
|
|
454
268
|
this.document.redo();
|
|
455
|
-
|
|
456
|
-
case "\u000a": // Ctrl+J when emitted by terminals (same byte as LF).
|
|
457
|
-
if (this.options.submitOnEnter) {
|
|
458
|
-
this.finish(this.document.text.trim().length > 0 ? this.document.text : undefined);
|
|
459
|
-
return changed;
|
|
460
|
-
}
|
|
461
|
-
if (!this.previousCR)
|
|
462
|
-
this.document.insert("\n");
|
|
463
|
-
this.previousCR = false;
|
|
269
|
+
this.historyIndex = undefined;
|
|
464
270
|
break;
|
|
465
|
-
case "\
|
|
466
|
-
this.document.insert("\n");
|
|
467
|
-
break; // Ctrl+O: newline when Enter submits.
|
|
468
|
-
case "\u0013": // Ctrl+S submits once, with no implicit shell action.
|
|
271
|
+
case "\u000a": // LF / Ctrl+J
|
|
469
272
|
this.finish(this.document.text.trim().length > 0 ? this.document.text : undefined);
|
|
470
273
|
return changed;
|
|
274
|
+
case "\u000f": // Ctrl+O inserts a newline inside the draft.
|
|
275
|
+
this.document.insert("\n");
|
|
276
|
+
this.historyIndex = undefined;
|
|
277
|
+
break;
|
|
471
278
|
case "\u0001":
|
|
472
279
|
this.document.home();
|
|
473
280
|
break; // Ctrl+A
|
|
@@ -477,6 +284,7 @@ export class EditorController {
|
|
|
477
284
|
case "\u0008":
|
|
478
285
|
case "\u007f":
|
|
479
286
|
this.document.backspace();
|
|
287
|
+
this.historyIndex = undefined;
|
|
480
288
|
break;
|
|
481
289
|
case "\r":
|
|
482
290
|
if (this.suggestions().length > 0 &&
|
|
@@ -484,52 +292,38 @@ export class EditorController {
|
|
|
484
292
|
this.insertSelectedCompletion();
|
|
485
293
|
break;
|
|
486
294
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
return changed;
|
|
490
|
-
}
|
|
491
|
-
this.document.insert("\n");
|
|
492
|
-
this.previousCR = true;
|
|
493
|
-
break;
|
|
295
|
+
this.finish(this.document.text.trim().length > 0 ? this.document.text : undefined);
|
|
296
|
+
return changed;
|
|
494
297
|
case "\t":
|
|
495
298
|
if (this.insertSelectedCompletion())
|
|
496
299
|
break;
|
|
497
300
|
this.document.insert(" ");
|
|
498
|
-
this.
|
|
301
|
+
this.historyIndex = undefined;
|
|
499
302
|
break;
|
|
500
303
|
default:
|
|
501
|
-
this.previousCR = false;
|
|
502
304
|
if (char >= " " && char !== "\u007f") {
|
|
503
305
|
this.document.insert(char, true);
|
|
504
306
|
this.dismissedCommand = undefined;
|
|
505
307
|
this.completionIndex = 0;
|
|
308
|
+
this.historyIndex = undefined;
|
|
506
309
|
}
|
|
507
310
|
}
|
|
508
311
|
}
|
|
509
312
|
return changed;
|
|
510
313
|
}
|
|
511
314
|
draw() {
|
|
512
|
-
if (this.mouseSuspended)
|
|
513
|
-
return;
|
|
514
315
|
const matches = this.suggestions();
|
|
515
316
|
this.completionIndex = matches.length > 0 ? Math.min(this.completionIndex, matches.length - 1) : 0;
|
|
516
|
-
const
|
|
517
|
-
|
|
317
|
+
const footerStatus = this.options.footerStatus?.();
|
|
318
|
+
const footerIdentity = this.options.footerIdentity?.();
|
|
319
|
+
const frame = editorFrame(this.document, this.maxWidth(), this.maxHeight(), {
|
|
320
|
+
suggestions: matches,
|
|
321
|
+
selectedSuggestion: this.completionIndex,
|
|
322
|
+
...(footerStatus === undefined ? {} : { footerStatus }),
|
|
323
|
+
color: this.options.color === true,
|
|
324
|
+
...(footerIdentity === undefined ? {} : { footerIdentity }),
|
|
325
|
+
});
|
|
518
326
|
this.painter.render(frame);
|
|
519
|
-
if (this.options.mouse) {
|
|
520
|
-
// The painter keeps the frame's top row anchored across redraws. A resize
|
|
521
|
-
// invalidates that mapping; resized() clears it before requesting a new DSR.
|
|
522
|
-
this.outputWrite("\u001b[6n");
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
cancel() {
|
|
526
|
-
if (this.modeSwitchPending) {
|
|
527
|
-
this.modeSwitchCancelPending = true;
|
|
528
|
-
this.modeSwitchQueue = [];
|
|
529
|
-
this.modeSwitchQueueBytes = 0;
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
this.finish(undefined);
|
|
533
327
|
}
|
|
534
328
|
finish(value) {
|
|
535
329
|
if (this.completed)
|
|
@@ -539,11 +333,6 @@ export class EditorController {
|
|
|
539
333
|
clearTimeout(this.escapeTimer);
|
|
540
334
|
this.escapeTimer = undefined;
|
|
541
335
|
this.painter.finish();
|
|
542
|
-
if (this.options.mouse) {
|
|
543
|
-
this.painterMouse(false);
|
|
544
|
-
this.removeExitRestore();
|
|
545
|
-
}
|
|
546
|
-
this.mouseSuspended = false;
|
|
547
336
|
this.done(value);
|
|
548
337
|
}
|
|
549
338
|
}
|