@caupulican/pi-tui 0.78.2
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 +779 -0
- package/dist/autocomplete.d.ts +54 -0
- package/dist/autocomplete.d.ts.map +1 -0
- package/dist/autocomplete.js +632 -0
- package/dist/autocomplete.js.map +1 -0
- package/dist/components/box.d.ts +22 -0
- package/dist/components/box.d.ts.map +1 -0
- package/dist/components/box.js +104 -0
- package/dist/components/box.js.map +1 -0
- package/dist/components/cancellable-loader.d.ts +22 -0
- package/dist/components/cancellable-loader.d.ts.map +1 -0
- package/dist/components/cancellable-loader.js +35 -0
- package/dist/components/cancellable-loader.js.map +1 -0
- package/dist/components/editor.d.ts +249 -0
- package/dist/components/editor.d.ts.map +1 -0
- package/dist/components/editor.js +1857 -0
- package/dist/components/editor.js.map +1 -0
- package/dist/components/image.d.ts +28 -0
- package/dist/components/image.d.ts.map +1 -0
- package/dist/components/image.js +89 -0
- package/dist/components/image.js.map +1 -0
- package/dist/components/input.d.ts +37 -0
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +378 -0
- package/dist/components/input.js.map +1 -0
- package/dist/components/loader.d.ts +31 -0
- package/dist/components/loader.d.ts.map +1 -0
- package/dist/components/loader.js +69 -0
- package/dist/components/loader.js.map +1 -0
- package/dist/components/markdown.d.ts +96 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +644 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/select-list.d.ts +50 -0
- package/dist/components/select-list.d.ts.map +1 -0
- package/dist/components/select-list.js +159 -0
- package/dist/components/select-list.js.map +1 -0
- package/dist/components/settings-list.d.ts +50 -0
- package/dist/components/settings-list.d.ts.map +1 -0
- package/dist/components/settings-list.js +185 -0
- package/dist/components/settings-list.js.map +1 -0
- package/dist/components/spacer.d.ts +12 -0
- package/dist/components/spacer.d.ts.map +1 -0
- package/dist/components/spacer.js +23 -0
- package/dist/components/spacer.js.map +1 -0
- package/dist/components/text.d.ts +19 -0
- package/dist/components/text.d.ts.map +1 -0
- package/dist/components/text.js +89 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/truncated-text.d.ts +13 -0
- package/dist/components/truncated-text.d.ts.map +1 -0
- package/dist/components/truncated-text.js +51 -0
- package/dist/components/truncated-text.js.map +1 -0
- package/dist/editor-component.d.ts +39 -0
- package/dist/editor-component.d.ts.map +1 -0
- package/dist/editor-component.js +2 -0
- package/dist/editor-component.js.map +1 -0
- package/dist/fuzzy.d.ts +16 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +110 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/keybindings.d.ts +193 -0
- package/dist/keybindings.d.ts.map +1 -0
- package/dist/keybindings.js +174 -0
- package/dist/keybindings.js.map +1 -0
- package/dist/keys.d.ts +184 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +1173 -0
- package/dist/keys.js.map +1 -0
- package/dist/kill-ring.d.ts +28 -0
- package/dist/kill-ring.d.ts.map +1 -0
- package/dist/kill-ring.js +44 -0
- package/dist/kill-ring.js.map +1 -0
- package/dist/native-modifiers.d.ts +3 -0
- package/dist/native-modifiers.d.ts.map +1 -0
- package/dist/native-modifiers.js +53 -0
- package/dist/native-modifiers.js.map +1 -0
- package/dist/stdin-buffer.d.ts +50 -0
- package/dist/stdin-buffer.d.ts.map +1 -0
- package/dist/stdin-buffer.js +361 -0
- package/dist/stdin-buffer.js.map +1 -0
- package/dist/terminal-image.d.ts +90 -0
- package/dist/terminal-image.d.ts.map +1 -0
- package/dist/terminal-image.js +366 -0
- package/dist/terminal-image.js.map +1 -0
- package/dist/terminal.d.ts +113 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +472 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tui.d.ts +227 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +1077 -0
- package/dist/tui.js.map +1 -0
- package/dist/undo-stack.d.ts +17 -0
- package/dist/undo-stack.d.ts.map +1 -0
- package/dist/undo-stack.js +25 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/utils.d.ts +84 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1029 -0
- package/dist/utils.js.map +1 -0
- package/dist/word-navigation.d.ts +25 -0
- package/dist/word-navigation.d.ts.map +1 -0
- package/dist/word-navigation.js +96 -0
- package/dist/word-navigation.js.map +1 -0
- package/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
- package/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
- package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
- package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
- package/package.json +47 -0
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { setKittyProtocolActive } from "./keys.js";
|
|
6
|
+
import { isNativeModifierPressed } from "./native-modifiers.js";
|
|
7
|
+
import { StdinBuffer } from "./stdin-buffer.js";
|
|
8
|
+
const cjsRequire = createRequire(import.meta.url);
|
|
9
|
+
const TERMINAL_PROGRESS_KEEPALIVE_MS = 1000;
|
|
10
|
+
const TERMINAL_PROGRESS_ACTIVE_SEQUENCE = "\x1b]9;4;3\x07";
|
|
11
|
+
const TERMINAL_PROGRESS_CLEAR_SEQUENCE = "\x1b]9;4;0;\x07";
|
|
12
|
+
const APPLE_TERMINAL_SHIFT_ENTER_SEQUENCE = "\x1b[13;2u";
|
|
13
|
+
const DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS = 7;
|
|
14
|
+
const KITTY_KEYBOARD_PROTOCOL_FALLBACK_TIMEOUT_MS = 150;
|
|
15
|
+
const KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS = 150;
|
|
16
|
+
const KITTY_KEYBOARD_PROTOCOL_QUERY = `\x1b[>${DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS}u\x1b[?u\x1b[c`;
|
|
17
|
+
export function parseKeyboardProtocolNegotiationSequence(sequence) {
|
|
18
|
+
const kittyFlags = sequence.match(/^\x1b\[\?(\d+)u$/);
|
|
19
|
+
if (kittyFlags) {
|
|
20
|
+
return { type: "kitty-flags", flags: Number.parseInt(kittyFlags[1], 10) };
|
|
21
|
+
}
|
|
22
|
+
if (/^\x1b\[\?[\d;]*c$/.test(sequence)) {
|
|
23
|
+
return { type: "device-attributes" };
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
function isKeyboardProtocolNegotiationSequencePrefix(sequence, allowBareEscapePrefix) {
|
|
28
|
+
return (allowBareEscapePrefix && sequence === "\x1b") || sequence === "\x1b[" || /^\x1b\[\?[\d;]*$/.test(sequence);
|
|
29
|
+
}
|
|
30
|
+
export function isAppleTerminalSession() {
|
|
31
|
+
return process.platform === "darwin" && process.env.TERM_PROGRAM === "Apple_Terminal";
|
|
32
|
+
}
|
|
33
|
+
export function normalizeAppleTerminalInput(data, isAppleTerminal, isShiftPressed) {
|
|
34
|
+
if (isAppleTerminal && data === "\r" && isShiftPressed)
|
|
35
|
+
return APPLE_TERMINAL_SHIFT_ENTER_SEQUENCE;
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Real terminal using process.stdin/stdout
|
|
40
|
+
*/
|
|
41
|
+
export class ProcessTerminal {
|
|
42
|
+
wasRaw = false;
|
|
43
|
+
inputHandler;
|
|
44
|
+
resizeHandler;
|
|
45
|
+
_kittyProtocolActive = false;
|
|
46
|
+
_modifyOtherKeysActive = false;
|
|
47
|
+
keyboardProtocolPushed = false;
|
|
48
|
+
keyboardProtocolNegotiationPending = false;
|
|
49
|
+
keyboardProtocolLateResponsePending = false;
|
|
50
|
+
keyboardProtocolNegotiationBuffer = "";
|
|
51
|
+
keyboardProtocolFallbackTimer;
|
|
52
|
+
keyboardProtocolBufferFlushTimer;
|
|
53
|
+
stdinBuffer;
|
|
54
|
+
stdinDataHandler;
|
|
55
|
+
progressInterval;
|
|
56
|
+
writeLogPath = (() => {
|
|
57
|
+
const env = process.env.PI_TUI_WRITE_LOG || "";
|
|
58
|
+
if (!env)
|
|
59
|
+
return "";
|
|
60
|
+
try {
|
|
61
|
+
if (fs.statSync(env).isDirectory()) {
|
|
62
|
+
const now = new Date();
|
|
63
|
+
const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}_${String(now.getHours()).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}-${String(now.getSeconds()).padStart(2, "0")}`;
|
|
64
|
+
return path.join(env, `tui-${ts}-${process.pid}.log`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Not an existing directory - use as-is (file path)
|
|
69
|
+
}
|
|
70
|
+
return env;
|
|
71
|
+
})();
|
|
72
|
+
get kittyProtocolActive() {
|
|
73
|
+
return this._kittyProtocolActive;
|
|
74
|
+
}
|
|
75
|
+
start(onInput, onResize) {
|
|
76
|
+
this.inputHandler = onInput;
|
|
77
|
+
this.resizeHandler = onResize;
|
|
78
|
+
// Save previous state and enable raw mode
|
|
79
|
+
this.wasRaw = process.stdin.isRaw || false;
|
|
80
|
+
if (process.stdin.setRawMode) {
|
|
81
|
+
process.stdin.setRawMode(true);
|
|
82
|
+
}
|
|
83
|
+
process.stdin.setEncoding("utf8");
|
|
84
|
+
process.stdin.resume();
|
|
85
|
+
// Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
|
|
86
|
+
process.stdout.write("\x1b[?2004h");
|
|
87
|
+
// Set up resize handler immediately
|
|
88
|
+
process.stdout.on("resize", this.resizeHandler);
|
|
89
|
+
// Refresh terminal dimensions - they may be stale after suspend/resume
|
|
90
|
+
// (SIGWINCH is lost while process is stopped). Unix only.
|
|
91
|
+
if (process.platform !== "win32") {
|
|
92
|
+
process.kill(process.pid, "SIGWINCH");
|
|
93
|
+
}
|
|
94
|
+
// On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
|
|
95
|
+
// VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
|
|
96
|
+
// events that lose modifier information. Must run AFTER setRawMode(true)
|
|
97
|
+
// since that resets console mode flags.
|
|
98
|
+
this.enableWindowsVTInput();
|
|
99
|
+
// Query and enable Kitty keyboard protocol
|
|
100
|
+
// The query handler intercepts input temporarily, then installs the user's handler
|
|
101
|
+
// See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
|
|
102
|
+
this.queryAndEnableKittyProtocol();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Set up StdinBuffer to split batched input into individual sequences.
|
|
106
|
+
* This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
|
|
107
|
+
*
|
|
108
|
+
* Also watches for Kitty protocol response and enables it when detected.
|
|
109
|
+
* This is done here (after stdinBuffer parsing) rather than on raw stdin
|
|
110
|
+
* to handle the case where the response arrives split across multiple events.
|
|
111
|
+
*/
|
|
112
|
+
setupStdinBuffer() {
|
|
113
|
+
this.stdinBuffer = new StdinBuffer({ timeout: 10 });
|
|
114
|
+
// Forward individual sequences to the input handler
|
|
115
|
+
this.stdinBuffer.on("data", (sequence) => {
|
|
116
|
+
if (this.keyboardProtocolNegotiationPending) {
|
|
117
|
+
const negotiationSequence = this.readKeyboardProtocolNegotiationSequence(sequence, true);
|
|
118
|
+
if (negotiationSequence === "pending") {
|
|
119
|
+
return; // Wait for the rest of a split negotiation response.
|
|
120
|
+
}
|
|
121
|
+
if (this.handleKeyboardProtocolNegotiationSequence(negotiationSequence)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (this.keyboardProtocolLateResponsePending) {
|
|
126
|
+
const negotiationSequence = this.readKeyboardProtocolNegotiationSequence(sequence, false);
|
|
127
|
+
if (negotiationSequence === "pending") {
|
|
128
|
+
this.scheduleKeyboardProtocolNegotiationBufferFlush();
|
|
129
|
+
return; // Wait for the rest of a split late negotiation response.
|
|
130
|
+
}
|
|
131
|
+
if (this.handleKeyboardProtocolNegotiationSequence(negotiationSequence)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
this.forwardInputSequence(sequence);
|
|
136
|
+
});
|
|
137
|
+
// Re-wrap paste content with bracketed paste markers for existing editor handling
|
|
138
|
+
this.stdinBuffer.on("paste", (content) => {
|
|
139
|
+
if (this.inputHandler) {
|
|
140
|
+
this.inputHandler(`\x1b[200~${content}\x1b[201~`);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
// Handler that pipes stdin data through the buffer
|
|
144
|
+
this.stdinDataHandler = (data) => {
|
|
145
|
+
this.stdinBuffer.process(data);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Query terminal for Kitty keyboard protocol support and enable it if available.
|
|
150
|
+
*
|
|
151
|
+
* Kitty's progressive enhancement detection requires requesting the desired
|
|
152
|
+
* flags before querying them. The trailing DA query is a sentinel supported by
|
|
153
|
+
* terminals that do not know Kitty keyboard protocol. A short timeout remains
|
|
154
|
+
* as a backup for terminals, PTYs, and SSH sessions that delay, split, or drop
|
|
155
|
+
* the DA response.
|
|
156
|
+
*
|
|
157
|
+
* The requested flags are:
|
|
158
|
+
* - 1 = disambiguate escape codes
|
|
159
|
+
* - 2 = report event types (press/repeat/release)
|
|
160
|
+
* - 4 = report alternate keys (shifted key, base layout key)
|
|
161
|
+
*/
|
|
162
|
+
queryAndEnableKittyProtocol() {
|
|
163
|
+
this.setupStdinBuffer();
|
|
164
|
+
process.stdin.on("data", this.stdinDataHandler);
|
|
165
|
+
this.keyboardProtocolPushed = true;
|
|
166
|
+
this.keyboardProtocolNegotiationPending = true;
|
|
167
|
+
this.keyboardProtocolLateResponsePending = false;
|
|
168
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
169
|
+
process.stdout.write(KITTY_KEYBOARD_PROTOCOL_QUERY);
|
|
170
|
+
this.keyboardProtocolFallbackTimer = setTimeout(() => {
|
|
171
|
+
this.keyboardProtocolFallbackTimer = undefined;
|
|
172
|
+
this.keyboardProtocolNegotiationPending = false;
|
|
173
|
+
this.keyboardProtocolLateResponsePending = true;
|
|
174
|
+
if (this.keyboardProtocolNegotiationBuffer === "\x1b") {
|
|
175
|
+
this.flushKeyboardProtocolNegotiationBufferAsInput();
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
this.scheduleKeyboardProtocolNegotiationBufferFlush();
|
|
179
|
+
}
|
|
180
|
+
this.enableModifyOtherKeys();
|
|
181
|
+
}, KITTY_KEYBOARD_PROTOCOL_FALLBACK_TIMEOUT_MS);
|
|
182
|
+
}
|
|
183
|
+
handleKeyboardProtocolNegotiationSequence(negotiationSequence) {
|
|
184
|
+
if (!negotiationSequence)
|
|
185
|
+
return false;
|
|
186
|
+
if (negotiationSequence.type === "kitty-flags") {
|
|
187
|
+
if (negotiationSequence.flags !== 0 && !this._kittyProtocolActive) {
|
|
188
|
+
this._kittyProtocolActive = true;
|
|
189
|
+
setKittyProtocolActive(true);
|
|
190
|
+
this.keyboardProtocolNegotiationPending = false;
|
|
191
|
+
this.keyboardProtocolLateResponsePending = true;
|
|
192
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
193
|
+
this.clearKeyboardProtocolFallbackTimer();
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
this.keyboardProtocolNegotiationPending = false;
|
|
198
|
+
this.keyboardProtocolLateResponsePending = true;
|
|
199
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
200
|
+
this.clearKeyboardProtocolFallbackTimer();
|
|
201
|
+
this.enableModifyOtherKeys();
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
readKeyboardProtocolNegotiationSequence(sequence, allowBareEscapePrefix) {
|
|
205
|
+
if (this.keyboardProtocolNegotiationBuffer) {
|
|
206
|
+
const bufferedSequence = this.keyboardProtocolNegotiationBuffer + sequence;
|
|
207
|
+
const negotiationSequence = parseKeyboardProtocolNegotiationSequence(bufferedSequence);
|
|
208
|
+
if (negotiationSequence) {
|
|
209
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
210
|
+
return negotiationSequence;
|
|
211
|
+
}
|
|
212
|
+
if (isKeyboardProtocolNegotiationSequencePrefix(bufferedSequence, allowBareEscapePrefix)) {
|
|
213
|
+
this.setKeyboardProtocolNegotiationBuffer(bufferedSequence);
|
|
214
|
+
return "pending";
|
|
215
|
+
}
|
|
216
|
+
this.flushKeyboardProtocolNegotiationBufferAsInput();
|
|
217
|
+
}
|
|
218
|
+
const negotiationSequence = parseKeyboardProtocolNegotiationSequence(sequence);
|
|
219
|
+
if (negotiationSequence)
|
|
220
|
+
return negotiationSequence;
|
|
221
|
+
if (isKeyboardProtocolNegotiationSequencePrefix(sequence, allowBareEscapePrefix)) {
|
|
222
|
+
this.setKeyboardProtocolNegotiationBuffer(sequence);
|
|
223
|
+
return "pending";
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
setKeyboardProtocolNegotiationBuffer(sequence) {
|
|
228
|
+
this.clearKeyboardProtocolNegotiationBufferFlushTimer();
|
|
229
|
+
this.keyboardProtocolNegotiationBuffer = sequence;
|
|
230
|
+
}
|
|
231
|
+
clearKeyboardProtocolNegotiationBuffer() {
|
|
232
|
+
this.clearKeyboardProtocolNegotiationBufferFlushTimer();
|
|
233
|
+
this.keyboardProtocolNegotiationBuffer = "";
|
|
234
|
+
}
|
|
235
|
+
flushKeyboardProtocolNegotiationBufferAsInput() {
|
|
236
|
+
if (!this.keyboardProtocolNegotiationBuffer)
|
|
237
|
+
return;
|
|
238
|
+
const sequence = this.keyboardProtocolNegotiationBuffer;
|
|
239
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
240
|
+
this.forwardInputSequence(sequence);
|
|
241
|
+
}
|
|
242
|
+
scheduleKeyboardProtocolNegotiationBufferFlush() {
|
|
243
|
+
if (!this.keyboardProtocolNegotiationBuffer || this.keyboardProtocolBufferFlushTimer)
|
|
244
|
+
return;
|
|
245
|
+
this.keyboardProtocolBufferFlushTimer = setTimeout(() => {
|
|
246
|
+
this.keyboardProtocolBufferFlushTimer = undefined;
|
|
247
|
+
this.flushKeyboardProtocolNegotiationBufferAsInput();
|
|
248
|
+
}, KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS);
|
|
249
|
+
}
|
|
250
|
+
clearKeyboardProtocolNegotiationBufferFlushTimer() {
|
|
251
|
+
if (!this.keyboardProtocolBufferFlushTimer)
|
|
252
|
+
return;
|
|
253
|
+
clearTimeout(this.keyboardProtocolBufferFlushTimer);
|
|
254
|
+
this.keyboardProtocolBufferFlushTimer = undefined;
|
|
255
|
+
}
|
|
256
|
+
forwardInputSequence(sequence) {
|
|
257
|
+
if (!this.inputHandler)
|
|
258
|
+
return;
|
|
259
|
+
const isAppleTerminal = sequence === "\r" && isAppleTerminalSession();
|
|
260
|
+
const input = normalizeAppleTerminalInput(sequence, isAppleTerminal, isAppleTerminal && isNativeModifierPressed("shift"));
|
|
261
|
+
this.inputHandler(input);
|
|
262
|
+
}
|
|
263
|
+
enableModifyOtherKeys() {
|
|
264
|
+
if (this._kittyProtocolActive || this._modifyOtherKeysActive)
|
|
265
|
+
return;
|
|
266
|
+
process.stdout.write("\x1b[>4;2m");
|
|
267
|
+
this._modifyOtherKeysActive = true;
|
|
268
|
+
}
|
|
269
|
+
clearKeyboardProtocolFallbackTimer() {
|
|
270
|
+
if (!this.keyboardProtocolFallbackTimer)
|
|
271
|
+
return;
|
|
272
|
+
clearTimeout(this.keyboardProtocolFallbackTimer);
|
|
273
|
+
this.keyboardProtocolFallbackTimer = undefined;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
|
|
277
|
+
* console handle so the terminal sends VT sequences for modified keys
|
|
278
|
+
* (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
|
|
279
|
+
* discards modifier state and Shift+Tab arrives as plain \t.
|
|
280
|
+
*/
|
|
281
|
+
enableWindowsVTInput() {
|
|
282
|
+
if (process.platform !== "win32")
|
|
283
|
+
return;
|
|
284
|
+
try {
|
|
285
|
+
const arch = process.arch;
|
|
286
|
+
if (arch !== "x64" && arch !== "arm64")
|
|
287
|
+
return;
|
|
288
|
+
// Dynamic require so non-Windows and bundled/browser paths never load the
|
|
289
|
+
// native helper. In the npm package native/ is next to dist/; in compiled
|
|
290
|
+
// binary archives native/ is copied next to the executable.
|
|
291
|
+
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
|
292
|
+
const nativePath = path.join("native", "win32", "prebuilds", `win32-${arch}`, "win32-console-mode.node");
|
|
293
|
+
const candidates = [
|
|
294
|
+
path.join(moduleDir, "..", nativePath),
|
|
295
|
+
path.join(moduleDir, nativePath),
|
|
296
|
+
path.join(path.dirname(process.execPath), nativePath),
|
|
297
|
+
];
|
|
298
|
+
for (const modulePath of candidates) {
|
|
299
|
+
try {
|
|
300
|
+
const helper = cjsRequire(modulePath);
|
|
301
|
+
helper.enableVirtualTerminalInput?.();
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
// Try the next possible packaging location.
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
// Native helper not available — Shift+Tab won't be distinguishable from Tab.
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
async drainInput(maxMs = 1000, idleMs = 50) {
|
|
314
|
+
const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive || this.keyboardProtocolNegotiationPending;
|
|
315
|
+
this.keyboardProtocolLateResponsePending = false;
|
|
316
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
317
|
+
this.clearKeyboardProtocolFallbackTimer();
|
|
318
|
+
if (shouldDisableKittyProtocol) {
|
|
319
|
+
// Disable Kitty keyboard protocol first so any late key releases
|
|
320
|
+
// do not generate new Kitty escape sequences.
|
|
321
|
+
process.stdout.write("\x1b[<u");
|
|
322
|
+
this.keyboardProtocolPushed = false;
|
|
323
|
+
this._kittyProtocolActive = false;
|
|
324
|
+
setKittyProtocolActive(false);
|
|
325
|
+
}
|
|
326
|
+
this.keyboardProtocolNegotiationPending = false;
|
|
327
|
+
if (this._modifyOtherKeysActive) {
|
|
328
|
+
process.stdout.write("\x1b[>4;0m");
|
|
329
|
+
this._modifyOtherKeysActive = false;
|
|
330
|
+
}
|
|
331
|
+
const previousHandler = this.inputHandler;
|
|
332
|
+
this.inputHandler = undefined;
|
|
333
|
+
let lastDataTime = Date.now();
|
|
334
|
+
const onData = () => {
|
|
335
|
+
lastDataTime = Date.now();
|
|
336
|
+
};
|
|
337
|
+
process.stdin.on("data", onData);
|
|
338
|
+
const endTime = Date.now() + maxMs;
|
|
339
|
+
try {
|
|
340
|
+
while (true) {
|
|
341
|
+
const now = Date.now();
|
|
342
|
+
const timeLeft = endTime - now;
|
|
343
|
+
if (timeLeft <= 0)
|
|
344
|
+
break;
|
|
345
|
+
if (now - lastDataTime >= idleMs)
|
|
346
|
+
break;
|
|
347
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
finally {
|
|
351
|
+
process.stdin.removeListener("data", onData);
|
|
352
|
+
this.inputHandler = previousHandler;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
stop() {
|
|
356
|
+
if (this.clearProgressInterval()) {
|
|
357
|
+
process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
|
|
358
|
+
}
|
|
359
|
+
// Disable bracketed paste mode
|
|
360
|
+
process.stdout.write("\x1b[?2004l");
|
|
361
|
+
const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive || this.keyboardProtocolNegotiationPending;
|
|
362
|
+
this.keyboardProtocolLateResponsePending = false;
|
|
363
|
+
this.clearKeyboardProtocolNegotiationBuffer();
|
|
364
|
+
this.clearKeyboardProtocolFallbackTimer();
|
|
365
|
+
// Disable Kitty keyboard protocol if not already done by drainInput()
|
|
366
|
+
if (shouldDisableKittyProtocol) {
|
|
367
|
+
process.stdout.write("\x1b[<u");
|
|
368
|
+
this.keyboardProtocolPushed = false;
|
|
369
|
+
this._kittyProtocolActive = false;
|
|
370
|
+
setKittyProtocolActive(false);
|
|
371
|
+
}
|
|
372
|
+
this.keyboardProtocolNegotiationPending = false;
|
|
373
|
+
if (this._modifyOtherKeysActive) {
|
|
374
|
+
process.stdout.write("\x1b[>4;0m");
|
|
375
|
+
this._modifyOtherKeysActive = false;
|
|
376
|
+
}
|
|
377
|
+
// Clean up StdinBuffer
|
|
378
|
+
if (this.stdinBuffer) {
|
|
379
|
+
this.stdinBuffer.destroy();
|
|
380
|
+
this.stdinBuffer = undefined;
|
|
381
|
+
}
|
|
382
|
+
// Remove event handlers
|
|
383
|
+
if (this.stdinDataHandler) {
|
|
384
|
+
process.stdin.removeListener("data", this.stdinDataHandler);
|
|
385
|
+
this.stdinDataHandler = undefined;
|
|
386
|
+
}
|
|
387
|
+
this.inputHandler = undefined;
|
|
388
|
+
if (this.resizeHandler) {
|
|
389
|
+
process.stdout.removeListener("resize", this.resizeHandler);
|
|
390
|
+
this.resizeHandler = undefined;
|
|
391
|
+
}
|
|
392
|
+
// Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
|
|
393
|
+
// re-interpreted after raw mode is disabled. This fixes a race condition
|
|
394
|
+
// where Ctrl+D could close the parent shell over SSH.
|
|
395
|
+
process.stdin.pause();
|
|
396
|
+
// Restore raw mode state
|
|
397
|
+
if (process.stdin.setRawMode) {
|
|
398
|
+
process.stdin.setRawMode(this.wasRaw);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
write(data) {
|
|
402
|
+
process.stdout.write(data);
|
|
403
|
+
if (this.writeLogPath) {
|
|
404
|
+
try {
|
|
405
|
+
fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
|
|
406
|
+
}
|
|
407
|
+
catch {
|
|
408
|
+
// Ignore logging errors
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
get columns() {
|
|
413
|
+
return process.stdout.columns || Number(process.env.COLUMNS) || 80;
|
|
414
|
+
}
|
|
415
|
+
get rows() {
|
|
416
|
+
return process.stdout.rows || Number(process.env.LINES) || 24;
|
|
417
|
+
}
|
|
418
|
+
moveBy(lines) {
|
|
419
|
+
if (lines > 0) {
|
|
420
|
+
// Move down
|
|
421
|
+
process.stdout.write(`\x1b[${lines}B`);
|
|
422
|
+
}
|
|
423
|
+
else if (lines < 0) {
|
|
424
|
+
// Move up
|
|
425
|
+
process.stdout.write(`\x1b[${-lines}A`);
|
|
426
|
+
}
|
|
427
|
+
// lines === 0: no movement
|
|
428
|
+
}
|
|
429
|
+
hideCursor() {
|
|
430
|
+
process.stdout.write("\x1b[?25l");
|
|
431
|
+
}
|
|
432
|
+
showCursor() {
|
|
433
|
+
process.stdout.write("\x1b[?25h");
|
|
434
|
+
}
|
|
435
|
+
clearLine() {
|
|
436
|
+
process.stdout.write("\x1b[K");
|
|
437
|
+
}
|
|
438
|
+
clearFromCursor() {
|
|
439
|
+
process.stdout.write("\x1b[J");
|
|
440
|
+
}
|
|
441
|
+
clearScreen() {
|
|
442
|
+
process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
|
|
443
|
+
}
|
|
444
|
+
setTitle(title) {
|
|
445
|
+
// OSC 0;title BEL - set terminal window title
|
|
446
|
+
process.stdout.write(`\x1b]0;${title}\x07`);
|
|
447
|
+
}
|
|
448
|
+
setProgress(active) {
|
|
449
|
+
if (active) {
|
|
450
|
+
// OSC 9;4;3 - indeterminate progress
|
|
451
|
+
process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
|
|
452
|
+
if (!this.progressInterval) {
|
|
453
|
+
this.progressInterval = setInterval(() => {
|
|
454
|
+
process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);
|
|
455
|
+
}, TERMINAL_PROGRESS_KEEPALIVE_MS);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
this.clearProgressInterval();
|
|
460
|
+
// OSC 9;4;0 - clear progress
|
|
461
|
+
process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
clearProgressInterval() {
|
|
465
|
+
if (!this.progressInterval)
|
|
466
|
+
return false;
|
|
467
|
+
clearInterval(this.progressInterval);
|
|
468
|
+
this.progressInterval = undefined;
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
//# sourceMappingURL=terminal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminal.js","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,UAAU,GAAG,aAAa,CAAC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;YACP,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAC7B,6BAA6B;YAC7B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACxD,CAAC;IAAA,CACD;IAEO,qBAAqB,GAAY;QACxC,IAAI,CAAC,IAAI,CAAC,gBAAgB;YAAE,OAAO,KAAK,CAAC;QACzC,aAAa,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;QACrC,IAAI,CAAC,gBAAgB,GAAG,SAAS,CAAC;QAClC,OAAO,IAAI,CAAC;IAAA,CACZ;CACD","sourcesContent":["import * as fs from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport * as path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { setKittyProtocolActive } from \"./keys.ts\";\nimport { isNativeModifierPressed } from \"./native-modifiers.ts\";\nimport { StdinBuffer } from \"./stdin-buffer.ts\";\n\nconst cjsRequire = createRequire(import.meta.url);\n\nconst TERMINAL_PROGRESS_KEEPALIVE_MS = 1000;\nconst TERMINAL_PROGRESS_ACTIVE_SEQUENCE = \"\\x1b]9;4;3\\x07\";\nconst TERMINAL_PROGRESS_CLEAR_SEQUENCE = \"\\x1b]9;4;0;\\x07\";\nconst APPLE_TERMINAL_SHIFT_ENTER_SEQUENCE = \"\\x1b[13;2u\";\nconst DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS = 7;\nconst KITTY_KEYBOARD_PROTOCOL_FALLBACK_TIMEOUT_MS = 150;\nconst KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS = 150;\nconst KITTY_KEYBOARD_PROTOCOL_QUERY = `\\x1b[>${DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS}u\\x1b[?u\\x1b[c`;\n\nexport type KeyboardProtocolNegotiationSequence =\n\t| { type: \"kitty-flags\"; flags: number }\n\t| { type: \"device-attributes\" };\n\nexport function parseKeyboardProtocolNegotiationSequence(\n\tsequence: string,\n): KeyboardProtocolNegotiationSequence | undefined {\n\tconst kittyFlags = sequence.match(/^\\x1b\\[\\?(\\d+)u$/);\n\tif (kittyFlags) {\n\t\treturn { type: \"kitty-flags\", flags: Number.parseInt(kittyFlags[1]!, 10) };\n\t}\n\tif (/^\\x1b\\[\\?[\\d;]*c$/.test(sequence)) {\n\t\treturn { type: \"device-attributes\" };\n\t}\n\treturn undefined;\n}\n\nfunction isKeyboardProtocolNegotiationSequencePrefix(sequence: string, allowBareEscapePrefix: boolean): boolean {\n\treturn (allowBareEscapePrefix && sequence === \"\\x1b\") || sequence === \"\\x1b[\" || /^\\x1b\\[\\?[\\d;]*$/.test(sequence);\n}\n\nexport function isAppleTerminalSession(): boolean {\n\treturn process.platform === \"darwin\" && process.env.TERM_PROGRAM === \"Apple_Terminal\";\n}\n\nexport function normalizeAppleTerminalInput(data: string, isAppleTerminal: boolean, isShiftPressed: boolean): string {\n\tif (isAppleTerminal && data === \"\\r\" && isShiftPressed) return APPLE_TERMINAL_SHIFT_ENTER_SEQUENCE;\n\treturn data;\n}\n\n/**\n * Minimal terminal interface for TUI\n */\nexport interface Terminal {\n\t// Start the terminal with input and resize handlers\n\tstart(onInput: (data: string) => void, onResize: () => void): void;\n\n\t// Stop the terminal and restore state\n\tstop(): void;\n\n\t/**\n\t * Drain stdin before exiting to prevent Kitty key release events from\n\t * leaking to the parent shell over slow SSH connections.\n\t * @param maxMs - Maximum time to drain (default: 1000ms)\n\t * @param idleMs - Exit early if no input arrives within this time (default: 50ms)\n\t */\n\tdrainInput(maxMs?: number, idleMs?: number): Promise<void>;\n\n\t// Write output to terminal\n\twrite(data: string): void;\n\n\t// Get terminal dimensions\n\tget columns(): number;\n\tget rows(): number;\n\n\t// Whether Kitty keyboard protocol is active\n\tget kittyProtocolActive(): boolean;\n\n\t// Cursor positioning (relative to current position)\n\tmoveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines\n\n\t// Cursor visibility\n\thideCursor(): void; // Hide the cursor\n\tshowCursor(): void; // Show the cursor\n\n\t// Clear operations\n\tclearLine(): void; // Clear current line\n\tclearFromCursor(): void; // Clear from cursor to end of screen\n\tclearScreen(): void; // Clear entire screen and move cursor to (0,0)\n\n\t// Title operations\n\tsetTitle(title: string): void; // Set terminal window title\n\n\t// Progress indicator (OSC 9;4)\n\tsetProgress(active: boolean): void;\n}\n\n/**\n * Real terminal using process.stdin/stdout\n */\nexport class ProcessTerminal implements Terminal {\n\tprivate wasRaw = false;\n\tprivate inputHandler?: (data: string) => void;\n\tprivate resizeHandler?: () => void;\n\tprivate _kittyProtocolActive = false;\n\tprivate _modifyOtherKeysActive = false;\n\tprivate keyboardProtocolPushed = false;\n\tprivate keyboardProtocolNegotiationPending = false;\n\tprivate keyboardProtocolLateResponsePending = false;\n\tprivate keyboardProtocolNegotiationBuffer = \"\";\n\tprivate keyboardProtocolFallbackTimer?: ReturnType<typeof setTimeout>;\n\tprivate keyboardProtocolBufferFlushTimer?: ReturnType<typeof setTimeout>;\n\tprivate stdinBuffer?: StdinBuffer;\n\tprivate stdinDataHandler?: (data: string) => void;\n\tprivate progressInterval?: ReturnType<typeof setInterval>;\n\tprivate writeLogPath = (() => {\n\t\tconst env = process.env.PI_TUI_WRITE_LOG || \"\";\n\t\tif (!env) return \"\";\n\t\ttry {\n\t\t\tif (fs.statSync(env).isDirectory()) {\n\t\t\t\tconst now = new Date();\n\t\t\t\tconst ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, \"0\")}-${String(now.getDate()).padStart(2, \"0\")}_${String(now.getHours()).padStart(2, \"0\")}-${String(now.getMinutes()).padStart(2, \"0\")}-${String(now.getSeconds()).padStart(2, \"0\")}`;\n\t\t\t\treturn path.join(env, `tui-${ts}-${process.pid}.log`);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Not an existing directory - use as-is (file path)\n\t\t}\n\t\treturn env;\n\t})();\n\n\tget kittyProtocolActive(): boolean {\n\t\treturn this._kittyProtocolActive;\n\t}\n\n\tstart(onInput: (data: string) => void, onResize: () => void): void {\n\t\tthis.inputHandler = onInput;\n\t\tthis.resizeHandler = onResize;\n\n\t\t// Save previous state and enable raw mode\n\t\tthis.wasRaw = process.stdin.isRaw || false;\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(true);\n\t\t}\n\t\tprocess.stdin.setEncoding(\"utf8\");\n\t\tprocess.stdin.resume();\n\n\t\t// Enable bracketed paste mode - terminal will wrap pastes in \\x1b[200~ ... \\x1b[201~\n\t\tprocess.stdout.write(\"\\x1b[?2004h\");\n\n\t\t// Set up resize handler immediately\n\t\tprocess.stdout.on(\"resize\", this.resizeHandler);\n\n\t\t// Refresh terminal dimensions - they may be stale after suspend/resume\n\t\t// (SIGWINCH is lost while process is stopped). Unix only.\n\t\tif (process.platform !== \"win32\") {\n\t\t\tprocess.kill(process.pid, \"SIGWINCH\");\n\t\t}\n\n\t\t// On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends\n\t\t// VT escape sequences (e.g. \\x1b[Z for Shift+Tab) instead of raw console\n\t\t// events that lose modifier information. Must run AFTER setRawMode(true)\n\t\t// since that resets console mode flags.\n\t\tthis.enableWindowsVTInput();\n\n\t\t// Query and enable Kitty keyboard protocol\n\t\t// The query handler intercepts input temporarily, then installs the user's handler\n\t\t// See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/\n\t\tthis.queryAndEnableKittyProtocol();\n\t}\n\n\t/**\n\t * Set up StdinBuffer to split batched input into individual sequences.\n\t * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.\n\t *\n\t * Also watches for Kitty protocol response and enables it when detected.\n\t * This is done here (after stdinBuffer parsing) rather than on raw stdin\n\t * to handle the case where the response arrives split across multiple events.\n\t */\n\tprivate setupStdinBuffer(): void {\n\t\tthis.stdinBuffer = new StdinBuffer({ timeout: 10 });\n\n\t\t// Forward individual sequences to the input handler\n\t\tthis.stdinBuffer.on(\"data\", (sequence) => {\n\t\t\tif (this.keyboardProtocolNegotiationPending) {\n\t\t\t\tconst negotiationSequence = this.readKeyboardProtocolNegotiationSequence(sequence, true);\n\t\t\t\tif (negotiationSequence === \"pending\") {\n\t\t\t\t\treturn; // Wait for the rest of a split negotiation response.\n\t\t\t\t}\n\t\t\t\tif (this.handleKeyboardProtocolNegotiationSequence(negotiationSequence)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (this.keyboardProtocolLateResponsePending) {\n\t\t\t\tconst negotiationSequence = this.readKeyboardProtocolNegotiationSequence(sequence, false);\n\t\t\t\tif (negotiationSequence === \"pending\") {\n\t\t\t\t\tthis.scheduleKeyboardProtocolNegotiationBufferFlush();\n\t\t\t\t\treturn; // Wait for the rest of a split late negotiation response.\n\t\t\t\t}\n\t\t\t\tif (this.handleKeyboardProtocolNegotiationSequence(negotiationSequence)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.forwardInputSequence(sequence);\n\t\t});\n\n\t\t// Re-wrap paste content with bracketed paste markers for existing editor handling\n\t\tthis.stdinBuffer.on(\"paste\", (content) => {\n\t\t\tif (this.inputHandler) {\n\t\t\t\tthis.inputHandler(`\\x1b[200~${content}\\x1b[201~`);\n\t\t\t}\n\t\t});\n\n\t\t// Handler that pipes stdin data through the buffer\n\t\tthis.stdinDataHandler = (data: string) => {\n\t\t\tthis.stdinBuffer!.process(data);\n\t\t};\n\t}\n\n\t/**\n\t * Query terminal for Kitty keyboard protocol support and enable it if available.\n\t *\n\t * Kitty's progressive enhancement detection requires requesting the desired\n\t * flags before querying them. The trailing DA query is a sentinel supported by\n\t * terminals that do not know Kitty keyboard protocol. A short timeout remains\n\t * as a backup for terminals, PTYs, and SSH sessions that delay, split, or drop\n\t * the DA response.\n\t *\n\t * The requested flags are:\n\t * - 1 = disambiguate escape codes\n\t * - 2 = report event types (press/repeat/release)\n\t * - 4 = report alternate keys (shifted key, base layout key)\n\t */\n\tprivate queryAndEnableKittyProtocol(): void {\n\t\tthis.setupStdinBuffer();\n\t\tprocess.stdin.on(\"data\", this.stdinDataHandler!);\n\t\tthis.keyboardProtocolPushed = true;\n\t\tthis.keyboardProtocolNegotiationPending = true;\n\t\tthis.keyboardProtocolLateResponsePending = false;\n\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\tprocess.stdout.write(KITTY_KEYBOARD_PROTOCOL_QUERY);\n\t\tthis.keyboardProtocolFallbackTimer = setTimeout(() => {\n\t\t\tthis.keyboardProtocolFallbackTimer = undefined;\n\t\t\tthis.keyboardProtocolNegotiationPending = false;\n\t\t\tthis.keyboardProtocolLateResponsePending = true;\n\t\t\tif (this.keyboardProtocolNegotiationBuffer === \"\\x1b\") {\n\t\t\t\tthis.flushKeyboardProtocolNegotiationBufferAsInput();\n\t\t\t} else {\n\t\t\t\tthis.scheduleKeyboardProtocolNegotiationBufferFlush();\n\t\t\t}\n\t\t\tthis.enableModifyOtherKeys();\n\t\t}, KITTY_KEYBOARD_PROTOCOL_FALLBACK_TIMEOUT_MS);\n\t}\n\n\tprivate handleKeyboardProtocolNegotiationSequence(\n\t\tnegotiationSequence: KeyboardProtocolNegotiationSequence | undefined,\n\t): boolean {\n\t\tif (!negotiationSequence) return false;\n\t\tif (negotiationSequence.type === \"kitty-flags\") {\n\t\t\tif (negotiationSequence.flags !== 0 && !this._kittyProtocolActive) {\n\t\t\t\tthis._kittyProtocolActive = true;\n\t\t\t\tsetKittyProtocolActive(true);\n\t\t\t\tthis.keyboardProtocolNegotiationPending = false;\n\t\t\t\tthis.keyboardProtocolLateResponsePending = true;\n\t\t\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\t\t\tthis.clearKeyboardProtocolFallbackTimer();\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tthis.keyboardProtocolNegotiationPending = false;\n\t\tthis.keyboardProtocolLateResponsePending = true;\n\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\tthis.clearKeyboardProtocolFallbackTimer();\n\t\tthis.enableModifyOtherKeys();\n\t\treturn true;\n\t}\n\n\tprivate readKeyboardProtocolNegotiationSequence(\n\t\tsequence: string,\n\t\tallowBareEscapePrefix: boolean,\n\t): KeyboardProtocolNegotiationSequence | \"pending\" | undefined {\n\t\tif (this.keyboardProtocolNegotiationBuffer) {\n\t\t\tconst bufferedSequence = this.keyboardProtocolNegotiationBuffer + sequence;\n\t\t\tconst negotiationSequence = parseKeyboardProtocolNegotiationSequence(bufferedSequence);\n\t\t\tif (negotiationSequence) {\n\t\t\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\t\t\treturn negotiationSequence;\n\t\t\t}\n\t\t\tif (isKeyboardProtocolNegotiationSequencePrefix(bufferedSequence, allowBareEscapePrefix)) {\n\t\t\t\tthis.setKeyboardProtocolNegotiationBuffer(bufferedSequence);\n\t\t\t\treturn \"pending\";\n\t\t\t}\n\t\t\tthis.flushKeyboardProtocolNegotiationBufferAsInput();\n\t\t}\n\n\t\tconst negotiationSequence = parseKeyboardProtocolNegotiationSequence(sequence);\n\t\tif (negotiationSequence) return negotiationSequence;\n\t\tif (isKeyboardProtocolNegotiationSequencePrefix(sequence, allowBareEscapePrefix)) {\n\t\t\tthis.setKeyboardProtocolNegotiationBuffer(sequence);\n\t\t\treturn \"pending\";\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate setKeyboardProtocolNegotiationBuffer(sequence: string): void {\n\t\tthis.clearKeyboardProtocolNegotiationBufferFlushTimer();\n\t\tthis.keyboardProtocolNegotiationBuffer = sequence;\n\t}\n\n\tprivate clearKeyboardProtocolNegotiationBuffer(): void {\n\t\tthis.clearKeyboardProtocolNegotiationBufferFlushTimer();\n\t\tthis.keyboardProtocolNegotiationBuffer = \"\";\n\t}\n\n\tprivate flushKeyboardProtocolNegotiationBufferAsInput(): void {\n\t\tif (!this.keyboardProtocolNegotiationBuffer) return;\n\t\tconst sequence = this.keyboardProtocolNegotiationBuffer;\n\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\tthis.forwardInputSequence(sequence);\n\t}\n\n\tprivate scheduleKeyboardProtocolNegotiationBufferFlush(): void {\n\t\tif (!this.keyboardProtocolNegotiationBuffer || this.keyboardProtocolBufferFlushTimer) return;\n\t\tthis.keyboardProtocolBufferFlushTimer = setTimeout(() => {\n\t\t\tthis.keyboardProtocolBufferFlushTimer = undefined;\n\t\t\tthis.flushKeyboardProtocolNegotiationBufferAsInput();\n\t\t}, KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS);\n\t}\n\n\tprivate clearKeyboardProtocolNegotiationBufferFlushTimer(): void {\n\t\tif (!this.keyboardProtocolBufferFlushTimer) return;\n\t\tclearTimeout(this.keyboardProtocolBufferFlushTimer);\n\t\tthis.keyboardProtocolBufferFlushTimer = undefined;\n\t}\n\n\tprivate forwardInputSequence(sequence: string): void {\n\t\tif (!this.inputHandler) return;\n\t\tconst isAppleTerminal = sequence === \"\\r\" && isAppleTerminalSession();\n\t\tconst input = normalizeAppleTerminalInput(\n\t\t\tsequence,\n\t\t\tisAppleTerminal,\n\t\t\tisAppleTerminal && isNativeModifierPressed(\"shift\"),\n\t\t);\n\t\tthis.inputHandler(input);\n\t}\n\n\tprivate enableModifyOtherKeys(): void {\n\t\tif (this._kittyProtocolActive || this._modifyOtherKeysActive) return;\n\t\tprocess.stdout.write(\"\\x1b[>4;2m\");\n\t\tthis._modifyOtherKeysActive = true;\n\t}\n\n\tprivate clearKeyboardProtocolFallbackTimer(): void {\n\t\tif (!this.keyboardProtocolFallbackTimer) return;\n\t\tclearTimeout(this.keyboardProtocolFallbackTimer);\n\t\tthis.keyboardProtocolFallbackTimer = undefined;\n\t}\n\n\t/**\n\t * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin\n\t * console handle so the terminal sends VT sequences for modified keys\n\t * (e.g. \\x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW\n\t * discards modifier state and Shift+Tab arrives as plain \\t.\n\t */\n\tprivate enableWindowsVTInput(): void {\n\t\tif (process.platform !== \"win32\") return;\n\t\ttry {\n\t\t\tconst arch = process.arch;\n\t\t\tif (arch !== \"x64\" && arch !== \"arm64\") return;\n\n\t\t\t// Dynamic require so non-Windows and bundled/browser paths never load the\n\t\t\t// native helper. In the npm package native/ is next to dist/; in compiled\n\t\t\t// binary archives native/ is copied next to the executable.\n\t\t\tconst moduleDir = path.dirname(fileURLToPath(import.meta.url));\n\t\t\tconst nativePath = path.join(\"native\", \"win32\", \"prebuilds\", `win32-${arch}`, \"win32-console-mode.node\");\n\t\t\tconst candidates = [\n\t\t\t\tpath.join(moduleDir, \"..\", nativePath),\n\t\t\t\tpath.join(moduleDir, nativePath),\n\t\t\t\tpath.join(path.dirname(process.execPath), nativePath),\n\t\t\t];\n\t\t\tfor (const modulePath of candidates) {\n\t\t\t\ttry {\n\t\t\t\t\tconst helper = cjsRequire(modulePath) as { enableVirtualTerminalInput?: () => boolean };\n\t\t\t\t\thelper.enableVirtualTerminalInput?.();\n\t\t\t\t\treturn;\n\t\t\t\t} catch {\n\t\t\t\t\t// Try the next possible packaging location.\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Native helper not available — Shift+Tab won't be distinguishable from Tab.\n\t\t}\n\t}\n\n\tasync drainInput(maxMs = 1000, idleMs = 50): Promise<void> {\n\t\tconst shouldDisableKittyProtocol =\n\t\t\tthis.keyboardProtocolPushed || this._kittyProtocolActive || this.keyboardProtocolNegotiationPending;\n\t\tthis.keyboardProtocolLateResponsePending = false;\n\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\tthis.clearKeyboardProtocolFallbackTimer();\n\t\tif (shouldDisableKittyProtocol) {\n\t\t\t// Disable Kitty keyboard protocol first so any late key releases\n\t\t\t// do not generate new Kitty escape sequences.\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis.keyboardProtocolPushed = false;\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tthis.keyboardProtocolNegotiationPending = false;\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\tconst previousHandler = this.inputHandler;\n\t\tthis.inputHandler = undefined;\n\n\t\tlet lastDataTime = Date.now();\n\t\tconst onData = () => {\n\t\t\tlastDataTime = Date.now();\n\t\t};\n\n\t\tprocess.stdin.on(\"data\", onData);\n\t\tconst endTime = Date.now() + maxMs;\n\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tconst now = Date.now();\n\t\t\t\tconst timeLeft = endTime - now;\n\t\t\t\tif (timeLeft <= 0) break;\n\t\t\t\tif (now - lastDataTime >= idleMs) break;\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));\n\t\t\t}\n\t\t} finally {\n\t\t\tprocess.stdin.removeListener(\"data\", onData);\n\t\t\tthis.inputHandler = previousHandler;\n\t\t}\n\t}\n\n\tstop(): void {\n\t\tif (this.clearProgressInterval()) {\n\t\t\tprocess.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);\n\t\t}\n\n\t\t// Disable bracketed paste mode\n\t\tprocess.stdout.write(\"\\x1b[?2004l\");\n\n\t\tconst shouldDisableKittyProtocol =\n\t\t\tthis.keyboardProtocolPushed || this._kittyProtocolActive || this.keyboardProtocolNegotiationPending;\n\t\tthis.keyboardProtocolLateResponsePending = false;\n\t\tthis.clearKeyboardProtocolNegotiationBuffer();\n\t\tthis.clearKeyboardProtocolFallbackTimer();\n\n\t\t// Disable Kitty keyboard protocol if not already done by drainInput()\n\t\tif (shouldDisableKittyProtocol) {\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis.keyboardProtocolPushed = false;\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tthis.keyboardProtocolNegotiationPending = false;\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\t// Clean up StdinBuffer\n\t\tif (this.stdinBuffer) {\n\t\t\tthis.stdinBuffer.destroy();\n\t\t\tthis.stdinBuffer = undefined;\n\t\t}\n\n\t\t// Remove event handlers\n\t\tif (this.stdinDataHandler) {\n\t\t\tprocess.stdin.removeListener(\"data\", this.stdinDataHandler);\n\t\t\tthis.stdinDataHandler = undefined;\n\t\t}\n\t\tthis.inputHandler = undefined;\n\t\tif (this.resizeHandler) {\n\t\t\tprocess.stdout.removeListener(\"resize\", this.resizeHandler);\n\t\t\tthis.resizeHandler = undefined;\n\t\t}\n\n\t\t// Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being\n\t\t// re-interpreted after raw mode is disabled. This fixes a race condition\n\t\t// where Ctrl+D could close the parent shell over SSH.\n\t\tprocess.stdin.pause();\n\n\t\t// Restore raw mode state\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(this.wasRaw);\n\t\t}\n\t}\n\n\twrite(data: string): void {\n\t\tprocess.stdout.write(data);\n\t\tif (this.writeLogPath) {\n\t\t\ttry {\n\t\t\t\tfs.appendFileSync(this.writeLogPath, data, { encoding: \"utf8\" });\n\t\t\t} catch {\n\t\t\t\t// Ignore logging errors\n\t\t\t}\n\t\t}\n\t}\n\n\tget columns(): number {\n\t\treturn process.stdout.columns || Number(process.env.COLUMNS) || 80;\n\t}\n\n\tget rows(): number {\n\t\treturn process.stdout.rows || Number(process.env.LINES) || 24;\n\t}\n\n\tmoveBy(lines: number): void {\n\t\tif (lines > 0) {\n\t\t\t// Move down\n\t\t\tprocess.stdout.write(`\\x1b[${lines}B`);\n\t\t} else if (lines < 0) {\n\t\t\t// Move up\n\t\t\tprocess.stdout.write(`\\x1b[${-lines}A`);\n\t\t}\n\t\t// lines === 0: no movement\n\t}\n\n\thideCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25l\");\n\t}\n\n\tshowCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25h\");\n\t}\n\n\tclearLine(): void {\n\t\tprocess.stdout.write(\"\\x1b[K\");\n\t}\n\n\tclearFromCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[J\");\n\t}\n\n\tclearScreen(): void {\n\t\tprocess.stdout.write(\"\\x1b[2J\\x1b[H\"); // Clear screen and move to home (1,1)\n\t}\n\n\tsetTitle(title: string): void {\n\t\t// OSC 0;title BEL - set terminal window title\n\t\tprocess.stdout.write(`\\x1b]0;${title}\\x07`);\n\t}\n\n\tsetProgress(active: boolean): void {\n\t\tif (active) {\n\t\t\t// OSC 9;4;3 - indeterminate progress\n\t\t\tprocess.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);\n\t\t\tif (!this.progressInterval) {\n\t\t\t\tthis.progressInterval = setInterval(() => {\n\t\t\t\t\tprocess.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE);\n\t\t\t\t}, TERMINAL_PROGRESS_KEEPALIVE_MS);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.clearProgressInterval();\n\t\t\t// OSC 9;4;0 - clear progress\n\t\t\tprocess.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE);\n\t\t}\n\t}\n\n\tprivate clearProgressInterval(): boolean {\n\t\tif (!this.progressInterval) return false;\n\t\tclearInterval(this.progressInterval);\n\t\tthis.progressInterval = undefined;\n\t\treturn true;\n\t}\n}\n"]}
|