neonctl 2.22.2 → 2.23.0

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.
Files changed (113) hide show
  1. package/README.md +84 -0
  2. package/analytics.js +5 -2
  3. package/commands/branches.js +9 -1
  4. package/commands/connection_string.js +9 -1
  5. package/commands/functions.js +277 -0
  6. package/commands/index.js +4 -0
  7. package/commands/neon_auth.js +1013 -0
  8. package/commands/projects.js +9 -1
  9. package/commands/psql.js +6 -1
  10. package/functions_api.js +44 -0
  11. package/package.json +15 -5
  12. package/psql/cli.js +51 -0
  13. package/psql/command/cmd_cond.js +437 -0
  14. package/psql/command/cmd_connect.js +815 -0
  15. package/psql/command/cmd_copy.js +1025 -0
  16. package/psql/command/cmd_describe.js +1810 -0
  17. package/psql/command/cmd_format.js +909 -0
  18. package/psql/command/cmd_io.js +2187 -0
  19. package/psql/command/cmd_lo.js +385 -0
  20. package/psql/command/cmd_meta.js +970 -0
  21. package/psql/command/cmd_misc.js +187 -0
  22. package/psql/command/cmd_pipeline.js +1141 -0
  23. package/psql/command/cmd_restrict.js +171 -0
  24. package/psql/command/cmd_show.js +751 -0
  25. package/psql/command/dispatch.js +343 -0
  26. package/psql/command/inputQueue.js +42 -0
  27. package/psql/command/shared.js +71 -0
  28. package/psql/complete/filenames.js +139 -0
  29. package/psql/complete/index.js +104 -0
  30. package/psql/complete/matcher.js +314 -0
  31. package/psql/complete/psqlVars.js +247 -0
  32. package/psql/complete/queries.js +491 -0
  33. package/psql/complete/rules.js +2387 -0
  34. package/psql/core/common.js +1250 -0
  35. package/psql/core/help.js +576 -0
  36. package/psql/core/mainloop.js +1353 -0
  37. package/psql/core/prompt.js +437 -0
  38. package/psql/core/settings.js +684 -0
  39. package/psql/core/sqlHelp.js +1066 -0
  40. package/psql/core/startup.js +840 -0
  41. package/psql/core/syncVars.js +116 -0
  42. package/psql/core/variables.js +287 -0
  43. package/psql/describe/formatters.js +1277 -0
  44. package/psql/describe/processNamePattern.js +270 -0
  45. package/psql/describe/queries.js +2373 -0
  46. package/psql/describe/versionGate.js +43 -0
  47. package/psql/index.js +2005 -0
  48. package/psql/io/history.js +299 -0
  49. package/psql/io/input.js +120 -0
  50. package/psql/io/lineEditor/buffer.js +323 -0
  51. package/psql/io/lineEditor/complete.js +227 -0
  52. package/psql/io/lineEditor/filename.js +159 -0
  53. package/psql/io/lineEditor/index.js +891 -0
  54. package/psql/io/lineEditor/keymap.js +738 -0
  55. package/psql/io/lineEditor/vt100.js +363 -0
  56. package/psql/io/pgpass.js +202 -0
  57. package/psql/io/pgservice.js +194 -0
  58. package/psql/io/psqlrc.js +422 -0
  59. package/psql/print/aligned.js +1756 -0
  60. package/psql/print/asciidoc.js +248 -0
  61. package/psql/print/crosstab.js +460 -0
  62. package/psql/print/csv.js +92 -0
  63. package/psql/print/html.js +258 -0
  64. package/psql/print/json.js +96 -0
  65. package/psql/print/latex.js +396 -0
  66. package/psql/print/pager.js +265 -0
  67. package/psql/print/troff.js +258 -0
  68. package/psql/print/unaligned.js +118 -0
  69. package/psql/print/units.js +135 -0
  70. package/psql/scanner/slash.js +513 -0
  71. package/psql/scanner/sql.js +910 -0
  72. package/psql/scanner/stringutils.js +390 -0
  73. package/psql/types/backslash.js +1 -0
  74. package/psql/types/connection.js +1 -0
  75. package/psql/types/index.js +7 -0
  76. package/psql/types/printer.js +1 -0
  77. package/psql/types/repl.js +1 -0
  78. package/psql/types/scanner.js +24 -0
  79. package/psql/types/settings.js +1 -0
  80. package/psql/types/variables.js +1 -0
  81. package/psql/wire/connection.js +2844 -0
  82. package/psql/wire/copy.js +108 -0
  83. package/psql/wire/notify.js +59 -0
  84. package/psql/wire/pipeline.js +519 -0
  85. package/psql/wire/protocol.js +466 -0
  86. package/psql/wire/sasl.js +296 -0
  87. package/psql/wire/tls.js +596 -0
  88. package/test_utils/fixtures.js +1 -0
  89. package/utils/esbuild.js +147 -0
  90. package/utils/psql.js +107 -11
  91. package/utils/zip.js +4 -0
  92. package/writer.js +1 -1
  93. package/commands/auth.test.js +0 -211
  94. package/commands/branches.test.js +0 -460
  95. package/commands/checkout.test.js +0 -170
  96. package/commands/connection_string.test.js +0 -196
  97. package/commands/data_api.test.js +0 -169
  98. package/commands/databases.test.js +0 -39
  99. package/commands/help.test.js +0 -9
  100. package/commands/init.test.js +0 -56
  101. package/commands/ip_allow.test.js +0 -59
  102. package/commands/link.test.js +0 -381
  103. package/commands/operations.test.js +0 -7
  104. package/commands/orgs.test.js +0 -7
  105. package/commands/projects.test.js +0 -144
  106. package/commands/psql.test.js +0 -49
  107. package/commands/roles.test.js +0 -37
  108. package/commands/set_context.test.js +0 -159
  109. package/commands/vpc_endpoints.test.js +0 -69
  110. package/context.test.js +0 -119
  111. package/env.test.js +0 -55
  112. package/utils/formats.test.js +0 -32
  113. package/writer.test.js +0 -104
@@ -0,0 +1,891 @@
1
+ /**
2
+ * Public-facing line editor.
3
+ *
4
+ * Glues the streaming VT100 decoder, the keymap, the line buffer, and
5
+ * the completion engine into a `readLine(prompt)` Promise-returning API.
6
+ *
7
+ * Architecture
8
+ * ------------
9
+ *
10
+ * stdin (raw mode)
11
+ * │ bytes
12
+ * ▼
13
+ * Vt100Decoder ─── KeyEvent[]
14
+ * │
15
+ * ▼
16
+ * dispatch(state, ev) ─── EditorAction
17
+ * │
18
+ * ▼
19
+ * render(prompt, state, stdout) (CSI writes)
20
+ * │
21
+ * └── on submit: resolve readLine() with state.buffer.text
22
+ * └── on ^C: reject with SignalError
23
+ * └── on ^D: resolve with EOF symbol
24
+ *
25
+ * Rendering strategy
26
+ * ------------------
27
+ *
28
+ * Naive but robust: track the number of rows last drawn, on every
29
+ * redraw move the cursor up to the prompt's anchor row and rewrite the
30
+ * whole `prompt + buffer.text` block. This avoids per-keystroke diffing
31
+ * bugs at the cost of a few extra bytes per keystroke. For the
32
+ * ~80-char lines users actually type interactively this is invisible.
33
+ *
34
+ * Wrapping uses the inlined `displayWidth` (port of WP-09's table) so
35
+ * East-Asian wide characters and zero-width combining marks render
36
+ * correctly. We never call `stdout.write` with embedded `\n`; line
37
+ * breaks come from explicit `\r\n` only on submit or when we need to
38
+ * display multi-row output (paste-mode newlines, candidate listings).
39
+ */
40
+ import { LineBuffer } from './buffer.js';
41
+ import { dispatch, makeState, } from './keymap.js';
42
+ import { CompletionState, formatCandidates, } from './complete.js';
43
+ import { BEL, CR, LF, Vt100Decoder, csiClearScreen, csiDown, csiEraseToEol, csiLeft, csiRight, csiUp, disableBracketedPaste, enableBracketedPaste, } from './vt100.js';
44
+ /** Thrown when ^C cancels the current line. */
45
+ export class SignalError extends Error {
46
+ constructor() {
47
+ super('SIGINT');
48
+ this.name = 'SignalError';
49
+ this.signal = 'SIGINT';
50
+ }
51
+ }
52
+ /**
53
+ * Sentinel for "Ctrl-D on an empty line". Compared with `===` by callers
54
+ * so we don't accidentally match a literal string.
55
+ */
56
+ const EOF_SYMBOL = Symbol('LineEditor.EOF');
57
+ export class LineEditor {
58
+ constructor(opts = {}) {
59
+ this.EOF = EOF_SYMBOL;
60
+ /**
61
+ * Pending mode change requested via `setMode()`. Applied at the next
62
+ * `readLine()` boundary so an in-flight line keeps its current dispatch
63
+ * (mirrors upstream readline's "next prompt" semantics for `set
64
+ * editing-mode`).
65
+ */
66
+ this.pendingMode = null;
67
+ this.completion = new CompletionState();
68
+ /** Event queue; processed serially so async completion blocks subsequent keys. */
69
+ this.eventQueue = [];
70
+ this.processing = false;
71
+ /** Active readLine, if any. */
72
+ this.active = null;
73
+ /** Listeners attached to stdin while readLine is active. */
74
+ this.dataListener = null;
75
+ this.wasRaw = false;
76
+ /** TTY state restoration handlers. */
77
+ this.exitListener = null;
78
+ this.stdin = opts.stdin ?? process.stdin;
79
+ this.stdout = opts.stdout ?? process.stdout;
80
+ this.bracketedPaste = opts.bracketedPaste ?? true;
81
+ this.completer = opts.completer;
82
+ this.mode = opts.mode ?? 'emacs';
83
+ this.state = makeState(opts.history ?? [], this.mode === 'vi' ? 'insert' : 'emacs');
84
+ this.decoder = new Vt100Decoder({
85
+ // LineEditor default: 50ms matches GNU readline's `keyseq-timeout`
86
+ // default — enough to disambiguate Alt-X across all modern terminals
87
+ // without making bare Esc noticeably laggy. Callers can override (or
88
+ // set 0 to restore the legacy "emit Esc immediately" behaviour).
89
+ escTimeoutMs: opts.escTimeoutMs ?? 50,
90
+ onTimeoutEvent: (ev) => {
91
+ this.handleDecoderTimeout(ev);
92
+ },
93
+ });
94
+ }
95
+ /** Read one line. Resolves on Enter; rejects on Ctrl-C. */
96
+ readLine(prompt) {
97
+ if (this.active !== null) {
98
+ return Promise.reject(new Error('LineEditor.readLine called re-entrantly'));
99
+ }
100
+ // Apply any pending setMode() request at this readLine boundary so an
101
+ // in-flight line keeps its prior dispatch.
102
+ if (this.pendingMode !== null) {
103
+ this.mode = this.pendingMode;
104
+ this.pendingMode = null;
105
+ }
106
+ return new Promise((resolve, reject) => {
107
+ // Fresh buffer for the new prompt.
108
+ this.state.buffer = new LineBuffer();
109
+ this.state.historyIndex = -1;
110
+ this.state.liveSnapshot = null;
111
+ // Switch state.mode to match the editor mode: vi → start in 'insert',
112
+ // emacs → 'emacs'. This must happen on every readLine so a mid-session
113
+ // VI_MODE flip is observed (the state machine has its own per-line
114
+ // mode field that needs to be reset).
115
+ if (this.mode === 'vi') {
116
+ this.state.mode = 'insert';
117
+ this.state.viPending = null;
118
+ }
119
+ else {
120
+ this.state.mode = 'emacs';
121
+ this.state.viPending = null;
122
+ this.state.exBuffer = '';
123
+ }
124
+ this.completion.reset();
125
+ this.active = {
126
+ prompt,
127
+ resolve,
128
+ reject,
129
+ rowsDrawn: 0,
130
+ cursorRow: 0,
131
+ cursorCol: 0,
132
+ search: null,
133
+ listingRowsDrawn: 0,
134
+ };
135
+ try {
136
+ this.enterRaw();
137
+ }
138
+ catch (err) {
139
+ this.active = null;
140
+ reject(err);
141
+ return;
142
+ }
143
+ this.render();
144
+ // If a previous readLine submitted mid-chunk and left residual key
145
+ // events queued (e.g. the user pasted `analyze (\n\t\t` — after the
146
+ // `\n` triggered submit, the `\t\t` was already decoded but sat in
147
+ // `eventQueue` because `active` had been nulled), drain them now
148
+ // against the new line buffer. Mirrors readline's natural behaviour
149
+ // of carrying buffered input across the line boundary.
150
+ if (this.eventQueue.length > 0)
151
+ void this.drainQueue();
152
+ });
153
+ }
154
+ /** Force redraw (call from SIGWINCH handler). */
155
+ redraw() {
156
+ if (this.active !== null)
157
+ this.render(true);
158
+ }
159
+ /**
160
+ * Inject an out-of-band line into the terminal while a prompt is being
161
+ * edited. Used by callers that produce async output (NOTIFY messages,
162
+ * notices, etc.) that would otherwise clobber the prompt rendering.
163
+ *
164
+ * Behaviour:
165
+ * - No active readLine: pass-through to stdout.
166
+ * - Active readLine: move cursor to end-of-block, write a fresh newline
167
+ * so the injected text starts on its own row, write the text, then
168
+ * redraw the prompt + buffer below it (re-attaching the cursor).
169
+ *
170
+ * The injected text should NOT have a trailing newline — we add one as
171
+ * part of the move-to-end + LF dance. If the caller's payload already
172
+ * ends with `\n`, we strip it once.
173
+ */
174
+ interject(text) {
175
+ if (this.active === null) {
176
+ this.stdout.write(text);
177
+ return;
178
+ }
179
+ const a = this.active;
180
+ this.moveCursorToEnd();
181
+ this.stdout.write(LF);
182
+ const body = text.endsWith('\n') ? text.slice(0, -1) : text;
183
+ this.stdout.write(body);
184
+ this.stdout.write(LF);
185
+ // Reset drawn-state so render() lays out a fresh block under the
186
+ // injected text instead of trying to overwrite the area we just used.
187
+ a.rowsDrawn = 0;
188
+ a.cursorRow = 0;
189
+ a.cursorCol = 0;
190
+ // Interjected text overwrites any candidate listing that was on screen.
191
+ a.listingRowsDrawn = 0;
192
+ this.render(true);
193
+ }
194
+ /** Cleanup raw mode and restore TTY. Idempotent. */
195
+ close() {
196
+ this.exitRaw();
197
+ }
198
+ /**
199
+ * Request a switch between vi and emacs editing modes. The change takes
200
+ * effect at the NEXT `readLine()` boundary, not retroactively on the line
201
+ * currently being edited — matching upstream readline's `set editing-mode`
202
+ * semantics. Calling this with the currently-active mode is a no-op.
203
+ */
204
+ setMode(mode) {
205
+ this.pendingMode = mode;
206
+ }
207
+ /** Read the editor's currently effective mode. Exposed for tests. */
208
+ getMode() {
209
+ return this.mode;
210
+ }
211
+ /** Push a line into the in-memory history. */
212
+ pushHistory(line) {
213
+ if (line.length === 0)
214
+ return;
215
+ const last = this.state.history[this.state.history.length - 1];
216
+ if (last === line)
217
+ return;
218
+ this.state.history.push(line);
219
+ }
220
+ /** Replace the in-memory history list. */
221
+ setHistory(lines) {
222
+ this.state.history = lines.slice();
223
+ this.state.historyIndex = -1;
224
+ this.state.liveSnapshot = null;
225
+ }
226
+ // -------------------------------------------------------------------------
227
+ // I/O wiring
228
+ // -------------------------------------------------------------------------
229
+ enterRaw() {
230
+ const s = this.stdin;
231
+ if (isTtyReadStream(s)) {
232
+ this.wasRaw = Boolean(s.isRaw);
233
+ s.setRawMode(true);
234
+ }
235
+ s.resume();
236
+ this.decoder.reset();
237
+ if (this.bracketedPaste)
238
+ this.stdout.write(enableBracketedPaste());
239
+ this.dataListener = (chunk) => {
240
+ this.handleChunk(chunk);
241
+ };
242
+ s.on('data', this.dataListener);
243
+ if (!this.exitListener) {
244
+ this.exitListener = () => {
245
+ this.exitRaw();
246
+ };
247
+ process.once('exit', this.exitListener);
248
+ process.once('SIGTERM', this.exitListener);
249
+ }
250
+ }
251
+ exitRaw() {
252
+ const s = this.stdin;
253
+ if (this.dataListener !== null) {
254
+ s.off('data', this.dataListener);
255
+ this.dataListener = null;
256
+ }
257
+ if (isTtyReadStream(s) && !this.wasRaw) {
258
+ try {
259
+ s.setRawMode(false);
260
+ }
261
+ catch {
262
+ /* ignore */
263
+ }
264
+ }
265
+ if (this.bracketedPaste) {
266
+ try {
267
+ this.stdout.write(disableBracketedPaste());
268
+ }
269
+ catch {
270
+ /* ignore */
271
+ }
272
+ }
273
+ if (this.exitListener !== null) {
274
+ process.off('exit', this.exitListener);
275
+ process.off('SIGTERM', this.exitListener);
276
+ this.exitListener = null;
277
+ }
278
+ }
279
+ // -------------------------------------------------------------------------
280
+ // Chunk processing
281
+ // -------------------------------------------------------------------------
282
+ handleChunk(chunk) {
283
+ if (this.active === null)
284
+ return;
285
+ const events = this.decoder.push(new Uint8Array(chunk));
286
+ for (const ev of events)
287
+ this.eventQueue.push(ev);
288
+ void this.drainQueue();
289
+ }
290
+ /** Called when the decoder's bare-Esc timer fires with a buffered event. */
291
+ handleDecoderTimeout(ev) {
292
+ if (this.active === null)
293
+ return;
294
+ this.eventQueue.push(ev);
295
+ void this.drainQueue();
296
+ }
297
+ /**
298
+ * Serially drain the event queue. Each event may kick off async work
299
+ * (notably Tab completion); we await it before processing the next
300
+ * event so keystrokes don't race ahead of pending completion results.
301
+ */
302
+ async drainQueue() {
303
+ if (this.processing)
304
+ return;
305
+ this.processing = true;
306
+ try {
307
+ while (this.eventQueue.length > 0 && this.active !== null) {
308
+ const ev = this.eventQueue.shift();
309
+ if (ev === undefined)
310
+ break;
311
+ await this.handleEvent(ev);
312
+ }
313
+ }
314
+ finally {
315
+ this.processing = false;
316
+ }
317
+ }
318
+ async handleEvent(ev) {
319
+ if (this.active === null)
320
+ return;
321
+ const a = this.active;
322
+ // Search mode handling is local: events go into the search state machine
323
+ // instead of the keymap.
324
+ if (a.search !== null) {
325
+ await this.handleSearchKey(ev);
326
+ return;
327
+ }
328
+ const action = dispatch(this.state, ev);
329
+ await this.applyAction(action, ev);
330
+ }
331
+ /**
332
+ * Reset the completion engine and forget any listing geometry: the next Tab
333
+ * will request a fresh result, and the next `list` / `cycled` will emit a
334
+ * new block on a fresh row instead of trying to overwrite a stale listing
335
+ * whose coordinates are no longer valid.
336
+ */
337
+ resetCompletion() {
338
+ this.completion.reset();
339
+ if (this.active !== null)
340
+ this.active.listingRowsDrawn = 0;
341
+ }
342
+ async applyAction(action, ev) {
343
+ if (this.active === null)
344
+ return;
345
+ const a = this.active;
346
+ switch (action.kind) {
347
+ case 'noop':
348
+ return;
349
+ case 'redraw':
350
+ this.resetCompletion();
351
+ this.render();
352
+ return;
353
+ case 'bell':
354
+ this.stdout.write(BEL);
355
+ return;
356
+ case 'submit': {
357
+ this.resetCompletion();
358
+ const text = this.state.buffer.text;
359
+ // Move cursor past the end of the rendered block and emit a newline.
360
+ this.moveCursorToEnd();
361
+ this.stdout.write(LF);
362
+ const resolve = a.resolve;
363
+ this.active = null;
364
+ this.exitRaw();
365
+ resolve(text);
366
+ return;
367
+ }
368
+ case 'cancel': {
369
+ // Upstream psql doesn't echo `^C` to the screen on Ctrl-C — it just
370
+ // breaks to the next prompt line silently. Match that behaviour.
371
+ this.resetCompletion();
372
+ this.moveCursorToEnd();
373
+ this.stdout.write(LF);
374
+ const reject = a.reject;
375
+ this.active = null;
376
+ this.exitRaw();
377
+ reject(new SignalError());
378
+ return;
379
+ }
380
+ case 'eof': {
381
+ this.resetCompletion();
382
+ this.moveCursorToEnd();
383
+ this.stdout.write(LF);
384
+ const resolve = a.resolve;
385
+ this.active = null;
386
+ this.exitRaw();
387
+ resolve(EOF_SYMBOL);
388
+ return;
389
+ }
390
+ case 'complete': {
391
+ if (!this.completer) {
392
+ this.stdout.write(BEL);
393
+ return;
394
+ }
395
+ await this.runCompletion();
396
+ return;
397
+ }
398
+ case 'clear-screen':
399
+ this.resetCompletion();
400
+ this.stdout.write(csiClearScreen());
401
+ a.rowsDrawn = 0;
402
+ a.cursorRow = 0;
403
+ a.cursorCol = 0;
404
+ this.render(true);
405
+ return;
406
+ case 'search-start':
407
+ this.resetCompletion();
408
+ a.search = {
409
+ pattern: '',
410
+ matchIndex: null,
411
+ savedBuffer: this.state.buffer.text,
412
+ };
413
+ this.render();
414
+ return;
415
+ case 'paste-start':
416
+ case 'paste-end':
417
+ // Bracketed paste markers are otherwise transparent.
418
+ void ev;
419
+ return;
420
+ case 'ex-update':
421
+ // Vi `:`-ex prompt text changed (entered/typed/backspaced). The
422
+ // renderer reads `state.mode === 'ex'` and swaps in a `: <buf>` line.
423
+ this.resetCompletion();
424
+ this.render();
425
+ return;
426
+ }
427
+ }
428
+ // -------------------------------------------------------------------------
429
+ // Completion
430
+ // -------------------------------------------------------------------------
431
+ async runCompletion() {
432
+ if (this.completer === undefined || this.active === null)
433
+ return;
434
+ const step = await this.completion.apply(this.state.buffer, this.completer);
435
+ if (this.active === null)
436
+ return;
437
+ switch (step.kind) {
438
+ case 'bell':
439
+ this.stdout.write(BEL);
440
+ return;
441
+ case 'inserted':
442
+ this.render();
443
+ return;
444
+ case 'cycled': {
445
+ // In-place rewrite of the candidate listing with the new highlight.
446
+ // If we already have a listing on screen (placed above the current
447
+ // prompt block by an earlier `list` or `cycled`), navigate up to its
448
+ // first row, erase each row, and reprint with the cycled highlight.
449
+ // Otherwise fall back to the "print listing below" path, matching
450
+ // first-time `list` behaviour.
451
+ const cands = this.completion.getCandidates();
452
+ const cycleIndex = this.completion.getCycleIndex();
453
+ if (cands.length > 0) {
454
+ const w = this.termWidth();
455
+ const block = formatCandidates(cands, w, cycleIndex);
456
+ const blockRows = block.split('\n').length;
457
+ if (this.active.listingRowsDrawn > 0) {
458
+ this.rewriteListingInPlace(block, blockRows);
459
+ }
460
+ else {
461
+ this.emitListingBelow(block, blockRows);
462
+ }
463
+ }
464
+ else {
465
+ this.render();
466
+ }
467
+ return;
468
+ }
469
+ case 'list': {
470
+ // Print listing on a new row, then redraw the prompt+line below it.
471
+ const w = this.termWidth();
472
+ const block = formatCandidates(step.candidates, w);
473
+ const blockRows = block.split('\n').length;
474
+ this.emitListingBelow(block, blockRows);
475
+ return;
476
+ }
477
+ }
478
+ }
479
+ /**
480
+ * Print `block` on fresh rows below the prompt block, then redraw the
481
+ * prompt + buffer below it. Remembers how many rows the listing occupies
482
+ * in `listingRowsDrawn` so a subsequent cycle can rewrite it in place.
483
+ */
484
+ emitListingBelow(block, blockRows) {
485
+ if (this.active === null)
486
+ return;
487
+ this.moveCursorToEnd();
488
+ this.stdout.write(LF);
489
+ this.stdout.write(block + LF);
490
+ this.active.rowsDrawn = 0;
491
+ this.active.cursorRow = 0;
492
+ this.active.cursorCol = 0;
493
+ this.active.listingRowsDrawn = blockRows;
494
+ this.render(true);
495
+ }
496
+ /**
497
+ * Rewrite the candidate listing in place. Pre-condition: a listing of
498
+ * `this.active.listingRowsDrawn` rows is currently drawn just above the
499
+ * prompt block.
500
+ *
501
+ * 1) Step up to the FIRST row of the listing (past the prompt block).
502
+ * 2) Erase + reprint each listing row.
503
+ * 3) Move back down past any trailing erase, then redraw prompt + buffer.
504
+ *
505
+ * If `block` has a different row count from the old listing the difference
506
+ * is absorbed by clearing extra rows (shrinking) or by accepting some
507
+ * scroll (growing — rare in practice because the candidate list is fixed
508
+ * for the duration of a cycle).
509
+ */
510
+ rewriteListingInPlace(block, blockRows) {
511
+ if (this.active === null)
512
+ return;
513
+ const a = this.active;
514
+ const oldRows = a.listingRowsDrawn;
515
+ // 1. Cursor is somewhere inside the prompt block. Anchor to row 0 of the
516
+ // prompt block first.
517
+ this.stdout.write(CR);
518
+ if (a.cursorRow > 0)
519
+ this.stdout.write(csiUp(a.cursorRow));
520
+ // 2. Step up `oldRows` more rows so the cursor sits on the first row of
521
+ // the listing.
522
+ this.stdout.write(csiUp(oldRows));
523
+ // 3. Erase each listing row and write the new block. We treat the listing
524
+ // as `blockRows` lines separated by LF; on the last line we DON'T emit
525
+ // a trailing LF (otherwise we'd push the prompt down by one row).
526
+ const newLines = block.split('\n');
527
+ for (let i = 0; i < newLines.length; i++) {
528
+ this.stdout.write(csiEraseToEol());
529
+ this.stdout.write(newLines[i]);
530
+ if (i < newLines.length - 1)
531
+ this.stdout.write(LF + CR);
532
+ }
533
+ // 4. If the new block is shorter than the old one, clear the leftover
534
+ // rows below.
535
+ if (blockRows < oldRows) {
536
+ const extra = oldRows - blockRows;
537
+ for (let i = 0; i < extra; i++) {
538
+ this.stdout.write(LF + CR + csiEraseToEol());
539
+ }
540
+ // Step back up so the cursor sits right under the last listing line.
541
+ this.stdout.write(csiUp(extra));
542
+ }
543
+ // 5. Move past the listing onto the row where the prompt should start.
544
+ this.stdout.write(LF + CR);
545
+ // 6. The prompt's geometry needs to be redrawn fresh below the listing.
546
+ a.rowsDrawn = 0;
547
+ a.cursorRow = 0;
548
+ a.cursorCol = 0;
549
+ a.listingRowsDrawn = blockRows;
550
+ this.render(true);
551
+ }
552
+ // -------------------------------------------------------------------------
553
+ // Reverse-incremental-search
554
+ // -------------------------------------------------------------------------
555
+ async handleSearchKey(ev) {
556
+ if (this.active?.search == null)
557
+ return;
558
+ const s = this.active.search;
559
+ // ^G or Escape cancels and restores the saved line.
560
+ if ((ev.key === 'char' && ev.ctrl && ev.char === 'g') ||
561
+ ev.key === 'escape') {
562
+ this.state.buffer.setText(s.savedBuffer);
563
+ this.active.search = null;
564
+ this.render();
565
+ return;
566
+ }
567
+ // Enter accepts the current match (whatever's in the buffer).
568
+ if (ev.key === 'enter') {
569
+ this.active.search = null;
570
+ await this.applyAction({ kind: 'submit' }, ev);
571
+ return;
572
+ }
573
+ // ^C bubbles to cancel.
574
+ if (ev.key === 'char' && ev.ctrl && ev.char === 'c') {
575
+ await this.applyAction({ kind: 'cancel' }, ev);
576
+ return;
577
+ }
578
+ // ^R again: search further back.
579
+ if (ev.key === 'char' && ev.ctrl && ev.char === 'r') {
580
+ this.searchStep(-1);
581
+ return;
582
+ }
583
+ // Backspace: shrink the pattern.
584
+ if (ev.key === 'backspace') {
585
+ s.pattern = s.pattern.slice(0, -1);
586
+ s.matchIndex = null;
587
+ this.searchStep(0);
588
+ return;
589
+ }
590
+ // Printable char: extend the pattern.
591
+ if (ev.key === 'char' && !ev.ctrl && !ev.meta && ev.char !== undefined) {
592
+ s.pattern += ev.char;
593
+ s.matchIndex = null;
594
+ this.searchStep(0);
595
+ return;
596
+ }
597
+ // Anything else (arrows, etc) accepts the match and processes the key.
598
+ this.active.search = null;
599
+ await this.handleEvent(ev);
600
+ }
601
+ /** Walk history backward looking for the current pattern. */
602
+ searchStep(delta) {
603
+ if (this.active?.search == null)
604
+ return;
605
+ const s = this.active.search;
606
+ const hist = this.state.history;
607
+ const startFrom = s.matchIndex === null ? hist.length - 1 : s.matchIndex + delta;
608
+ for (let i = startFrom; i >= 0 && i < hist.length; i--) {
609
+ if (s.pattern === '' || hist[i].includes(s.pattern)) {
610
+ s.matchIndex = i;
611
+ this.state.buffer.setText(hist[i]);
612
+ this.render();
613
+ return;
614
+ }
615
+ }
616
+ // No match: ring bell, keep current buffer.
617
+ this.stdout.write(BEL);
618
+ this.render();
619
+ }
620
+ // -------------------------------------------------------------------------
621
+ // Rendering
622
+ // -------------------------------------------------------------------------
623
+ /**
624
+ * Repaint the prompt + buffer. If `full` is true, skip the cursor-up
625
+ * optimisation (we don't know the previous geometry).
626
+ */
627
+ render(full = false) {
628
+ if (this.active === null)
629
+ return;
630
+ const a = this.active;
631
+ // Move the cursor back to row 0 of the previously drawn block.
632
+ if (!full && a.rowsDrawn > 0) {
633
+ // Move up to the anchor row.
634
+ const up = a.cursorRow;
635
+ this.stdout.write(CR + (up > 0 ? csiUp(up) : ''));
636
+ }
637
+ else {
638
+ this.stdout.write(CR);
639
+ }
640
+ // Compose the output: prompt + buffer text, with line wrapping computed
641
+ // virtually so we know where the cursor ends up.
642
+ const width = Math.max(1, this.termWidth());
643
+ // Pick the prompt + rendered text for the three render flavours:
644
+ // - vi ex-mode (`:`-prompt): `: <exBuffer>`, cursor at the end.
645
+ // - reverse-i-search: the `(reverse-i-search)\`pat':` preamble,
646
+ // with the matched pattern highlighted.
647
+ // - default editing: the caller-supplied prompt + buffer text.
648
+ const inExMode = this.state.mode === 'ex';
649
+ const promptStr = inExMode
650
+ ? ':'
651
+ : a.search === null
652
+ ? a.prompt
653
+ : `(reverse-i-search)\`${a.search.pattern}': `;
654
+ const rawText = inExMode
655
+ ? this.state.exBuffer
656
+ : this.state.buffer.text.replace(/\n/g, '⏎'); // newline glyph
657
+ // For search rendering we highlight the matched pattern (case-insensitive)
658
+ // inside the matched entry; otherwise the rendered text equals the raw text.
659
+ const renderText = !inExMode && a.search !== null && a.search.pattern.length > 0
660
+ ? highlightMatch(rawText, a.search.pattern)
661
+ : rawText;
662
+ const promptWidth = displayWidth(promptStr);
663
+ // Cursor positioning uses the raw text length, NOT the rendered text
664
+ // length, because ANSI escapes have zero display width but non-zero
665
+ // string length. In ex mode the cursor always sits at the end of the
666
+ // ex buffer.
667
+ const beforeText = inExMode
668
+ ? rawText
669
+ : rawText.slice(0, this.codePointsBeforeCursor().length);
670
+ const allText = renderText;
671
+ // Compute physical row/col for cursor.
672
+ const { row: cursorRow, col: cursorCol } = positionAfter(promptWidth, beforeText, width);
673
+ // Compute final row/col for the full block (so we know how many rows).
674
+ // Strip ANSI escape sequences from the geometry calculation since they
675
+ // have zero display width but non-zero string length.
676
+ const { row: lastRow } = positionAfter(promptWidth, stripAnsi(allText), width);
677
+ const rowsDrawn = lastRow + 1;
678
+ // Write the line. We erase each row first so leftover chars from a longer
679
+ // previous render are scrubbed.
680
+ this.stdout.write(csiEraseToEol());
681
+ this.stdout.write(promptStr);
682
+ this.stdout.write(allText);
683
+ // After writing, if the previous render had more rows than this one,
684
+ // erase the leftover rows.
685
+ if (a.rowsDrawn > rowsDrawn) {
686
+ const extra = a.rowsDrawn - rowsDrawn;
687
+ for (let i = 0; i < extra; i++) {
688
+ this.stdout.write(LF + csiEraseToEol());
689
+ }
690
+ // Move back up to where we are.
691
+ this.stdout.write(csiUp(extra));
692
+ }
693
+ // Reposition cursor.
694
+ // After writing `allText`, cursor is at (lastRow, lastCol). We want
695
+ // (cursorRow, cursorCol).
696
+ const rowDelta = cursorRow - lastRow;
697
+ if (rowDelta < 0)
698
+ this.stdout.write(csiUp(-rowDelta));
699
+ if (rowDelta > 0)
700
+ this.stdout.write(csiDown(rowDelta));
701
+ this.stdout.write(CR);
702
+ if (cursorCol > 0)
703
+ this.stdout.write(csiRight(cursorCol));
704
+ a.rowsDrawn = rowsDrawn;
705
+ a.cursorRow = cursorRow;
706
+ a.cursorCol = cursorCol;
707
+ // Silence unused.
708
+ void csiLeft;
709
+ }
710
+ moveCursorToEnd() {
711
+ if (this.active === null)
712
+ return;
713
+ const a = this.active;
714
+ // Step down to the final row of the current render.
715
+ const down = a.rowsDrawn - 1 - a.cursorRow;
716
+ if (down > 0)
717
+ this.stdout.write(csiDown(down));
718
+ this.stdout.write(CR);
719
+ a.cursorRow = a.rowsDrawn - 1;
720
+ a.cursorCol = 0;
721
+ }
722
+ codePointsBeforeCursor() {
723
+ return { length: this.state.buffer.cursor };
724
+ }
725
+ termWidth() {
726
+ const s = this.stdout;
727
+ if (typeof s.columns === 'number' && s.columns > 0)
728
+ return s.columns;
729
+ return 80;
730
+ }
731
+ }
732
+ const isTtyReadStream = (s) => typeof s.setRawMode === 'function';
733
+ // ---------------------------------------------------------------------------
734
+ // Search-line rendering / highlighting
735
+ // ---------------------------------------------------------------------------
736
+ const SGR_REVERSE = '\x1b[7m';
737
+ const SGR_NO_REVERSE = '\x1b[27m';
738
+ /**
739
+ * Wrap the first case-insensitive occurrence of `pattern` inside `text`
740
+ * with the reverse-video SGR pair. Returns `text` unchanged when the
741
+ * pattern is empty or not found.
742
+ *
743
+ * Kept as a pure helper for unit-testing (the renderer calls it during
744
+ * search mode, but `renderSearchLine` is the unit-testable surface).
745
+ */
746
+ export const highlightMatch = (text, pattern) => {
747
+ if (pattern.length === 0)
748
+ return text;
749
+ const lcText = text.toLowerCase();
750
+ const lcPat = pattern.toLowerCase();
751
+ const idx = lcText.indexOf(lcPat);
752
+ if (idx < 0)
753
+ return text;
754
+ return (text.slice(0, idx) +
755
+ SGR_REVERSE +
756
+ text.slice(idx, idx + pattern.length) +
757
+ SGR_NO_REVERSE +
758
+ text.slice(idx + pattern.length));
759
+ };
760
+ /**
761
+ * Render the search prompt + matched entry as a single string, with the
762
+ * matched pattern highlighted via reverse video. Exposed for unit tests.
763
+ * The real interactive renderer applies the same logic inline.
764
+ */
765
+ export const renderSearchLine = (pattern, entry) => {
766
+ const prefix = `(reverse-i-search)\`${pattern}': `;
767
+ return prefix + highlightMatch(entry, pattern);
768
+ };
769
+ /** Strip ANSI CSI escape sequences from `text` for display-width math. */
770
+ const stripAnsi = (text) =>
771
+ // Targets the SGR forms we emit (e.g. \x1b[7m, \x1b[27m). Kept narrow on
772
+ // purpose so it doesn't accidentally eat legitimate `[` characters.
773
+ // eslint-disable-next-line no-control-regex
774
+ text.replace(/\x1b\[[0-9;]*m/g, '');
775
+ // ---------------------------------------------------------------------------
776
+ // Display-width helpers (inlined copy of WP-09's tables; kept minimal).
777
+ // ---------------------------------------------------------------------------
778
+ const WIDE_RANGES = [
779
+ [0x1100, 0x115f],
780
+ [0x2329, 0x232a],
781
+ [0x2e80, 0x303e],
782
+ [0x3041, 0x33ff],
783
+ [0x3400, 0x4dbf],
784
+ [0x4e00, 0x9fff],
785
+ [0xa000, 0xa4cf],
786
+ [0xac00, 0xd7a3],
787
+ [0xf900, 0xfaff],
788
+ [0xfe10, 0xfe19],
789
+ [0xfe30, 0xfe6f],
790
+ [0xff00, 0xff60],
791
+ [0xffe0, 0xffe6],
792
+ [0x1f300, 0x1f64f],
793
+ [0x1f900, 0x1f9ff],
794
+ [0x20000, 0x2fffd],
795
+ [0x30000, 0x3fffd],
796
+ ];
797
+ const ZERO_RANGES = [
798
+ [0x0300, 0x036f],
799
+ [0x0483, 0x0489],
800
+ [0x0591, 0x05bd],
801
+ [0x05bf, 0x05bf],
802
+ [0x05c1, 0x05c2],
803
+ [0x05c4, 0x05c5],
804
+ [0x05c7, 0x05c7],
805
+ [0x0610, 0x061a],
806
+ [0x064b, 0x065f],
807
+ [0x0670, 0x0670],
808
+ [0x06d6, 0x06dc],
809
+ [0x06df, 0x06e4],
810
+ [0x06e7, 0x06e8],
811
+ [0x06ea, 0x06ed],
812
+ [0x0711, 0x0711],
813
+ [0x0730, 0x074a],
814
+ [0x200b, 0x200f],
815
+ [0x202a, 0x202e],
816
+ [0x2060, 0x206f],
817
+ [0x20d0, 0x20f0],
818
+ [0xfe00, 0xfe0f],
819
+ [0xfe20, 0xfe2f],
820
+ [0xfeff, 0xfeff],
821
+ [0xe0100, 0xe01ef],
822
+ ];
823
+ const inRange = (cp, ranges) => {
824
+ let lo = 0;
825
+ let hi = ranges.length - 1;
826
+ while (lo <= hi) {
827
+ const mid = (lo + hi) >> 1;
828
+ const entry = ranges[mid];
829
+ if (cp < entry[0])
830
+ hi = mid - 1;
831
+ else if (cp > entry[1])
832
+ lo = mid + 1;
833
+ else
834
+ return true;
835
+ }
836
+ return false;
837
+ };
838
+ const codePointWidth = (cp) => {
839
+ if (cp === 0)
840
+ return 0;
841
+ if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0))
842
+ return 0;
843
+ if (inRange(cp, ZERO_RANGES))
844
+ return 0;
845
+ if (inRange(cp, WIDE_RANGES))
846
+ return 2;
847
+ return 1;
848
+ };
849
+ const displayWidth = (text) => {
850
+ let w = 0;
851
+ for (const ch of text)
852
+ w += codePointWidth(ch.codePointAt(0) ?? 0);
853
+ return w;
854
+ };
855
+ /**
856
+ * Compute the final (row, col) position after writing `text` to a
857
+ * terminal whose cursor starts at column `startCol` and is `width`
858
+ * columns wide. Wrapping happens by display column count, not code points.
859
+ */
860
+ const positionAfter = (startCol, text, width) => {
861
+ let row = 0;
862
+ let col = startCol % width;
863
+ // Initial wrap if startCol exactly hit the width boundary.
864
+ if (col === 0 && startCol > 0) {
865
+ row += Math.floor(startCol / width);
866
+ }
867
+ else {
868
+ row += Math.floor(startCol / width);
869
+ }
870
+ for (const ch of text) {
871
+ if (ch === '\n') {
872
+ row += 1;
873
+ col = 0;
874
+ continue;
875
+ }
876
+ const w = codePointWidth(ch.codePointAt(0) ?? 0);
877
+ if (col + w > width) {
878
+ row += 1;
879
+ col = w;
880
+ }
881
+ else {
882
+ col += w;
883
+ if (col === width) {
884
+ // Stay on this row; next char triggers the wrap.
885
+ // (Real terminals differ here; the conservative choice that matches
886
+ // most xterm derivatives is to NOT advance row until the next glyph.)
887
+ }
888
+ }
889
+ }
890
+ return { row, col };
891
+ };