inquirerer 4.1.2 → 4.2.1
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 +206 -1
- package/esm/index.js +1 -0
- package/esm/ui/engine.js +232 -0
- package/esm/ui/index.js +16 -0
- package/esm/ui/progress.js +123 -0
- package/esm/ui/prompts.js +371 -0
- package/esm/ui/spinner.js +160 -0
- package/esm/ui/stream.js +172 -0
- package/esm/ui/types.js +22 -0
- package/esm/ui/upgrade.js +291 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +9 -5
- package/ui/engine.d.ts +79 -0
- package/ui/engine.js +236 -0
- package/ui/index.d.ts +12 -0
- package/ui/index.js +36 -0
- package/ui/progress.d.ts +46 -0
- package/ui/progress.js +128 -0
- package/ui/prompts.d.ts +49 -0
- package/ui/prompts.js +378 -0
- package/ui/spinner.d.ts +72 -0
- package/ui/spinner.js +165 -0
- package/ui/stream.d.ts +52 -0
- package/ui/stream.js +177 -0
- package/ui/types.d.ts +117 -0
- package/ui/types.js +25 -0
- package/ui/upgrade.d.ts +27 -0
- package/ui/upgrade.js +295 -0
package/ui/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* UI Module
|
|
4
|
+
*
|
|
5
|
+
* Event-driven UI components for building rich terminal interfaces
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.renderPromptHeader = exports.filterOptions = exports.checkboxPromptEngine = exports.autocompletePromptEngine = exports.listPromptEngine = exports.upgradePrompt = exports.interactiveUpgrade = exports.createStream = exports.StreamingText = exports.createProgress = exports.ProgressBar = exports.SPINNER_STYLES = exports.createSpinner = exports.Spinner = exports.Key = exports.ANSI = exports.UIEngine = void 0;
|
|
9
|
+
// Core engine
|
|
10
|
+
var engine_1 = require("./engine");
|
|
11
|
+
Object.defineProperty(exports, "UIEngine", { enumerable: true, get: function () { return engine_1.UIEngine; } });
|
|
12
|
+
Object.defineProperty(exports, "ANSI", { enumerable: true, get: function () { return engine_1.ANSI; } });
|
|
13
|
+
// Types
|
|
14
|
+
var types_1 = require("./types");
|
|
15
|
+
Object.defineProperty(exports, "Key", { enumerable: true, get: function () { return types_1.Key; } });
|
|
16
|
+
// Components
|
|
17
|
+
var spinner_1 = require("./spinner");
|
|
18
|
+
Object.defineProperty(exports, "Spinner", { enumerable: true, get: function () { return spinner_1.Spinner; } });
|
|
19
|
+
Object.defineProperty(exports, "createSpinner", { enumerable: true, get: function () { return spinner_1.createSpinner; } });
|
|
20
|
+
Object.defineProperty(exports, "SPINNER_STYLES", { enumerable: true, get: function () { return spinner_1.SPINNER_STYLES; } });
|
|
21
|
+
var progress_1 = require("./progress");
|
|
22
|
+
Object.defineProperty(exports, "ProgressBar", { enumerable: true, get: function () { return progress_1.ProgressBar; } });
|
|
23
|
+
Object.defineProperty(exports, "createProgress", { enumerable: true, get: function () { return progress_1.createProgress; } });
|
|
24
|
+
var stream_1 = require("./stream");
|
|
25
|
+
Object.defineProperty(exports, "StreamingText", { enumerable: true, get: function () { return stream_1.StreamingText; } });
|
|
26
|
+
Object.defineProperty(exports, "createStream", { enumerable: true, get: function () { return stream_1.createStream; } });
|
|
27
|
+
var upgrade_1 = require("./upgrade");
|
|
28
|
+
Object.defineProperty(exports, "interactiveUpgrade", { enumerable: true, get: function () { return upgrade_1.interactiveUpgrade; } });
|
|
29
|
+
Object.defineProperty(exports, "upgradePrompt", { enumerable: true, get: function () { return upgrade_1.upgradePrompt; } });
|
|
30
|
+
// Engine-based prompt implementations (internal use)
|
|
31
|
+
var prompts_1 = require("./prompts");
|
|
32
|
+
Object.defineProperty(exports, "listPromptEngine", { enumerable: true, get: function () { return prompts_1.listPromptEngine; } });
|
|
33
|
+
Object.defineProperty(exports, "autocompletePromptEngine", { enumerable: true, get: function () { return prompts_1.autocompletePromptEngine; } });
|
|
34
|
+
Object.defineProperty(exports, "checkboxPromptEngine", { enumerable: true, get: function () { return prompts_1.checkboxPromptEngine; } });
|
|
35
|
+
Object.defineProperty(exports, "filterOptions", { enumerable: true, get: function () { return prompts_1.filterOptions; } });
|
|
36
|
+
Object.defineProperty(exports, "renderPromptHeader", { enumerable: true, get: function () { return prompts_1.renderPromptHeader; } });
|
package/ui/progress.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progress Bar Component
|
|
3
|
+
*
|
|
4
|
+
* Visual progress indicator for operations with known completion
|
|
5
|
+
*/
|
|
6
|
+
import { Writable } from 'stream';
|
|
7
|
+
import { ProgressConfig } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Progress bar that can be controlled externally
|
|
10
|
+
*/
|
|
11
|
+
export declare class ProgressBar {
|
|
12
|
+
private width;
|
|
13
|
+
private showPercentage;
|
|
14
|
+
private state;
|
|
15
|
+
private output;
|
|
16
|
+
private isRunning;
|
|
17
|
+
constructor(config: ProgressConfig, output?: Writable);
|
|
18
|
+
/**
|
|
19
|
+
* Start the progress bar
|
|
20
|
+
*/
|
|
21
|
+
start(): this;
|
|
22
|
+
/**
|
|
23
|
+
* Update progress (0-1)
|
|
24
|
+
*/
|
|
25
|
+
update(value: number, text?: string): this;
|
|
26
|
+
/**
|
|
27
|
+
* Increment progress
|
|
28
|
+
*/
|
|
29
|
+
increment(amount?: number): this;
|
|
30
|
+
/**
|
|
31
|
+
* Complete the progress bar
|
|
32
|
+
*/
|
|
33
|
+
complete(text?: string): this;
|
|
34
|
+
/**
|
|
35
|
+
* Mark as error
|
|
36
|
+
*/
|
|
37
|
+
error(text?: string): this;
|
|
38
|
+
/**
|
|
39
|
+
* Render the current state
|
|
40
|
+
*/
|
|
41
|
+
private render;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create and start a progress bar
|
|
45
|
+
*/
|
|
46
|
+
export declare function createProgress(text: string, options?: Partial<ProgressConfig>): ProgressBar;
|
package/ui/progress.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Progress Bar Component
|
|
4
|
+
*
|
|
5
|
+
* Visual progress indicator for operations with known completion
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ProgressBar = void 0;
|
|
9
|
+
exports.createProgress = createProgress;
|
|
10
|
+
const yanse_1 = require("yanse");
|
|
11
|
+
/**
|
|
12
|
+
* Progress bar that can be controlled externally
|
|
13
|
+
*/
|
|
14
|
+
class ProgressBar {
|
|
15
|
+
width;
|
|
16
|
+
showPercentage;
|
|
17
|
+
state;
|
|
18
|
+
output;
|
|
19
|
+
isRunning = false;
|
|
20
|
+
constructor(config, output = process.stdout) {
|
|
21
|
+
this.width = config.width ?? 40;
|
|
22
|
+
this.showPercentage = config.showPercentage ?? true;
|
|
23
|
+
this.output = output;
|
|
24
|
+
this.state = {
|
|
25
|
+
value: 0,
|
|
26
|
+
text: config.text,
|
|
27
|
+
status: 'active',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Start the progress bar
|
|
32
|
+
*/
|
|
33
|
+
start() {
|
|
34
|
+
if (this.isRunning)
|
|
35
|
+
return this;
|
|
36
|
+
this.isRunning = true;
|
|
37
|
+
// Hide cursor
|
|
38
|
+
this.output.write('\x1B[?25l');
|
|
39
|
+
// Initial render
|
|
40
|
+
this.render();
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Update progress (0-1)
|
|
45
|
+
*/
|
|
46
|
+
update(value, text) {
|
|
47
|
+
this.state.value = Math.max(0, Math.min(1, value));
|
|
48
|
+
if (text) {
|
|
49
|
+
this.state.text = text;
|
|
50
|
+
}
|
|
51
|
+
if (this.isRunning) {
|
|
52
|
+
this.render();
|
|
53
|
+
}
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Increment progress
|
|
58
|
+
*/
|
|
59
|
+
increment(amount = 0.1) {
|
|
60
|
+
return this.update(this.state.value + amount);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Complete the progress bar
|
|
64
|
+
*/
|
|
65
|
+
complete(text) {
|
|
66
|
+
this.state.value = 1;
|
|
67
|
+
this.state.status = 'complete';
|
|
68
|
+
if (text) {
|
|
69
|
+
this.state.text = text;
|
|
70
|
+
}
|
|
71
|
+
this.render();
|
|
72
|
+
// Show cursor and newline
|
|
73
|
+
this.output.write('\x1B[?25h\n');
|
|
74
|
+
this.isRunning = false;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Mark as error
|
|
79
|
+
*/
|
|
80
|
+
error(text) {
|
|
81
|
+
this.state.status = 'error';
|
|
82
|
+
if (text) {
|
|
83
|
+
this.state.text = text;
|
|
84
|
+
}
|
|
85
|
+
this.render();
|
|
86
|
+
// Show cursor and newline
|
|
87
|
+
this.output.write('\x1B[?25h\n');
|
|
88
|
+
this.isRunning = false;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Render the current state
|
|
93
|
+
*/
|
|
94
|
+
render() {
|
|
95
|
+
const { value, text, status } = this.state;
|
|
96
|
+
const filled = Math.round(value * this.width);
|
|
97
|
+
const empty = this.width - filled;
|
|
98
|
+
let bar;
|
|
99
|
+
let icon;
|
|
100
|
+
switch (status) {
|
|
101
|
+
case 'complete':
|
|
102
|
+
bar = (0, yanse_1.green)('█'.repeat(this.width));
|
|
103
|
+
icon = (0, yanse_1.green)('✔');
|
|
104
|
+
break;
|
|
105
|
+
case 'error':
|
|
106
|
+
bar = (0, yanse_1.dim)('█'.repeat(filled) + '░'.repeat(empty));
|
|
107
|
+
icon = '\x1B[31m✖\x1B[0m'; // red
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
bar = (0, yanse_1.cyan)('█'.repeat(filled)) + (0, yanse_1.dim)('░'.repeat(empty));
|
|
111
|
+
icon = (0, yanse_1.cyan)('◐');
|
|
112
|
+
}
|
|
113
|
+
let line = `${icon} ${text} [${bar}]`;
|
|
114
|
+
if (this.showPercentage) {
|
|
115
|
+
const percent = Math.round(value * 100);
|
|
116
|
+
line += ` ${(0, yanse_1.white)(percent.toString().padStart(3))}%`;
|
|
117
|
+
}
|
|
118
|
+
// Clear line and write
|
|
119
|
+
this.output.write('\r\x1B[2K' + line);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.ProgressBar = ProgressBar;
|
|
123
|
+
/**
|
|
124
|
+
* Create and start a progress bar
|
|
125
|
+
*/
|
|
126
|
+
function createProgress(text, options) {
|
|
127
|
+
return new ProgressBar({ text, ...options });
|
|
128
|
+
}
|
package/ui/prompts.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine-based prompt implementations
|
|
3
|
+
*
|
|
4
|
+
* These are internal implementations of the standard prompts (list, autocomplete, checkbox)
|
|
5
|
+
* using the UIEngine for rendering and event handling.
|
|
6
|
+
*/
|
|
7
|
+
import { TerminalKeypress } from '../keypress';
|
|
8
|
+
import { OptionValue } from '../question';
|
|
9
|
+
import { Readable, Writable } from 'stream';
|
|
10
|
+
/**
|
|
11
|
+
* Render the prompt header lines
|
|
12
|
+
*/
|
|
13
|
+
export declare function renderPromptHeader(promptMessage: string, input: string): string[];
|
|
14
|
+
/**
|
|
15
|
+
* Filter options based on input (matches existing prompt.ts behavior)
|
|
16
|
+
*/
|
|
17
|
+
export declare function filterOptions(options: OptionValue[], input: string): OptionValue[];
|
|
18
|
+
export interface ListPromptConfig {
|
|
19
|
+
options: OptionValue[];
|
|
20
|
+
promptMessage: string;
|
|
21
|
+
maxLines: number;
|
|
22
|
+
keypress: TerminalKeypress;
|
|
23
|
+
input: Readable;
|
|
24
|
+
output: Writable;
|
|
25
|
+
noTty: boolean;
|
|
26
|
+
}
|
|
27
|
+
export declare function listPromptEngine(config: ListPromptConfig): Promise<any>;
|
|
28
|
+
export interface AutocompletePromptConfig {
|
|
29
|
+
options: OptionValue[];
|
|
30
|
+
promptMessage: string;
|
|
31
|
+
maxLines: number;
|
|
32
|
+
keypress: TerminalKeypress;
|
|
33
|
+
input: Readable;
|
|
34
|
+
output: Writable;
|
|
35
|
+
noTty: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare function autocompletePromptEngine(config: AutocompletePromptConfig): Promise<any>;
|
|
38
|
+
export interface CheckboxPromptConfig {
|
|
39
|
+
options: OptionValue[];
|
|
40
|
+
defaultSelections: boolean[];
|
|
41
|
+
promptMessage: string;
|
|
42
|
+
maxLines: number;
|
|
43
|
+
returnFullResults: boolean;
|
|
44
|
+
keypress: TerminalKeypress;
|
|
45
|
+
input: Readable;
|
|
46
|
+
output: Writable;
|
|
47
|
+
noTty: boolean;
|
|
48
|
+
}
|
|
49
|
+
export declare function checkboxPromptEngine(config: CheckboxPromptConfig): Promise<OptionValue[]>;
|
package/ui/prompts.js
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Engine-based prompt implementations
|
|
4
|
+
*
|
|
5
|
+
* These are internal implementations of the standard prompts (list, autocomplete, checkbox)
|
|
6
|
+
* using the UIEngine for rendering and event handling.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.renderPromptHeader = renderPromptHeader;
|
|
10
|
+
exports.filterOptions = filterOptions;
|
|
11
|
+
exports.listPromptEngine = listPromptEngine;
|
|
12
|
+
exports.autocompletePromptEngine = autocompletePromptEngine;
|
|
13
|
+
exports.checkboxPromptEngine = checkboxPromptEngine;
|
|
14
|
+
const yanse_1 = require("yanse");
|
|
15
|
+
const engine_1 = require("./engine");
|
|
16
|
+
const types_1 = require("./types");
|
|
17
|
+
/**
|
|
18
|
+
* Render the prompt header lines
|
|
19
|
+
*/
|
|
20
|
+
function renderPromptHeader(promptMessage, input) {
|
|
21
|
+
const inputLine = `> ${input}`;
|
|
22
|
+
return [promptMessage + inputLine];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Fuzzy match function (matches existing prompt.ts behavior)
|
|
26
|
+
*/
|
|
27
|
+
function fuzzyMatch(option, input) {
|
|
28
|
+
if (!input || !input.trim().length)
|
|
29
|
+
return true;
|
|
30
|
+
const length = input.length;
|
|
31
|
+
let position = 0;
|
|
32
|
+
for (let i = 0; i < option.length; i++) {
|
|
33
|
+
if (option[i] === input[position]) {
|
|
34
|
+
position++;
|
|
35
|
+
if (position === length) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Filter options based on input (matches existing prompt.ts behavior)
|
|
44
|
+
*/
|
|
45
|
+
function filterOptions(options, input) {
|
|
46
|
+
const normalizedInput = input.toLowerCase();
|
|
47
|
+
return options
|
|
48
|
+
.filter(option => fuzzyMatch(option.name.toLowerCase(), normalizedInput))
|
|
49
|
+
.sort((a, b) => {
|
|
50
|
+
if (a.name < b.name)
|
|
51
|
+
return -1;
|
|
52
|
+
if (a.name > b.name)
|
|
53
|
+
return 1;
|
|
54
|
+
return 0;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function listRender(state) {
|
|
58
|
+
const lines = [];
|
|
59
|
+
// Header
|
|
60
|
+
lines.push(state.promptMessage);
|
|
61
|
+
// Options
|
|
62
|
+
const endIndex = Math.min(state.startIndex + state.maxLines, state.options.length);
|
|
63
|
+
for (let i = state.startIndex; i < endIndex; i++) {
|
|
64
|
+
const option = state.options[i];
|
|
65
|
+
if (!option) {
|
|
66
|
+
lines.push('No options');
|
|
67
|
+
}
|
|
68
|
+
else if (i === state.selectedIndex) {
|
|
69
|
+
lines.push((0, yanse_1.blue)('> ' + option.name));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
lines.push(' ' + option.name);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return lines;
|
|
76
|
+
}
|
|
77
|
+
function listOnEvent(event, state) {
|
|
78
|
+
const { options, maxLines } = state;
|
|
79
|
+
let { selectedIndex, startIndex } = state;
|
|
80
|
+
if (event.type === 'key') {
|
|
81
|
+
switch (event.key) {
|
|
82
|
+
case types_1.Key.UP:
|
|
83
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : options.length - 1;
|
|
84
|
+
if (selectedIndex < startIndex) {
|
|
85
|
+
startIndex = selectedIndex;
|
|
86
|
+
}
|
|
87
|
+
else if (selectedIndex === options.length - 1) {
|
|
88
|
+
startIndex = Math.max(0, options.length - maxLines);
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case types_1.Key.DOWN:
|
|
92
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
93
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
94
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
95
|
+
}
|
|
96
|
+
else if (selectedIndex === 0) {
|
|
97
|
+
startIndex = 0;
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case types_1.Key.ENTER:
|
|
101
|
+
return {
|
|
102
|
+
state: { ...state, selectedIndex, startIndex },
|
|
103
|
+
done: true,
|
|
104
|
+
value: options[selectedIndex]?.value
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { state: { ...state, selectedIndex, startIndex } };
|
|
109
|
+
}
|
|
110
|
+
async function listPromptEngine(config) {
|
|
111
|
+
const engine = new engine_1.UIEngine({
|
|
112
|
+
keypress: config.keypress,
|
|
113
|
+
ownsKeypress: false,
|
|
114
|
+
input: config.input,
|
|
115
|
+
output: config.output,
|
|
116
|
+
noTty: config.noTty,
|
|
117
|
+
clearScreenOnStart: true,
|
|
118
|
+
});
|
|
119
|
+
const initialState = {
|
|
120
|
+
options: config.options,
|
|
121
|
+
selectedIndex: 0,
|
|
122
|
+
startIndex: 0,
|
|
123
|
+
maxLines: config.maxLines,
|
|
124
|
+
promptMessage: config.promptMessage,
|
|
125
|
+
};
|
|
126
|
+
return engine.run({
|
|
127
|
+
initialState,
|
|
128
|
+
render: listRender,
|
|
129
|
+
onEvent: listOnEvent,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function autocompleteRender(state) {
|
|
133
|
+
const lines = [];
|
|
134
|
+
// Header with input
|
|
135
|
+
lines.push(state.promptMessage + `> ${state.input}`);
|
|
136
|
+
// Filtered options
|
|
137
|
+
const endIndex = Math.min(state.startIndex + state.maxLines, state.filteredOptions.length);
|
|
138
|
+
for (let i = state.startIndex; i < endIndex; i++) {
|
|
139
|
+
const option = state.filteredOptions[i];
|
|
140
|
+
if (!option) {
|
|
141
|
+
lines.push('No options');
|
|
142
|
+
}
|
|
143
|
+
else if (i === state.selectedIndex) {
|
|
144
|
+
lines.push((0, yanse_1.blue)('> ' + option.name));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
lines.push(' ' + option.name);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return lines;
|
|
151
|
+
}
|
|
152
|
+
function autocompleteOnEvent(event, state) {
|
|
153
|
+
const { options, maxLines } = state;
|
|
154
|
+
let { input, filteredOptions, selectedIndex, startIndex } = state;
|
|
155
|
+
const updateFiltered = () => {
|
|
156
|
+
filteredOptions = filterOptions(options, input);
|
|
157
|
+
if (selectedIndex < startIndex) {
|
|
158
|
+
startIndex = selectedIndex;
|
|
159
|
+
}
|
|
160
|
+
else if (selectedIndex >= startIndex + maxLines) {
|
|
161
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
162
|
+
}
|
|
163
|
+
if (selectedIndex >= filteredOptions.length) {
|
|
164
|
+
selectedIndex = Math.max(filteredOptions.length - 1, 0);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
if (event.type === 'key') {
|
|
168
|
+
switch (event.key) {
|
|
169
|
+
case types_1.Key.UP:
|
|
170
|
+
if (filteredOptions.length === 0)
|
|
171
|
+
break;
|
|
172
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
173
|
+
if (selectedIndex < startIndex) {
|
|
174
|
+
startIndex = selectedIndex;
|
|
175
|
+
}
|
|
176
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
177
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines);
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case types_1.Key.DOWN:
|
|
181
|
+
if (filteredOptions.length === 0)
|
|
182
|
+
break;
|
|
183
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
184
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
185
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
186
|
+
}
|
|
187
|
+
else if (selectedIndex === 0) {
|
|
188
|
+
startIndex = 0;
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case types_1.Key.BACKSPACE:
|
|
192
|
+
input = input.slice(0, -1);
|
|
193
|
+
updateFiltered();
|
|
194
|
+
break;
|
|
195
|
+
case types_1.Key.SPACE:
|
|
196
|
+
// In autocomplete, space is part of input (unlike checkbox)
|
|
197
|
+
input += ' ';
|
|
198
|
+
updateFiltered();
|
|
199
|
+
break;
|
|
200
|
+
case types_1.Key.ENTER:
|
|
201
|
+
return {
|
|
202
|
+
state: { ...state, input, filteredOptions, selectedIndex, startIndex },
|
|
203
|
+
done: true,
|
|
204
|
+
value: filteredOptions[selectedIndex]?.value || input
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (event.type === 'char') {
|
|
209
|
+
input += event.char;
|
|
210
|
+
updateFiltered();
|
|
211
|
+
}
|
|
212
|
+
return { state: { ...state, input, filteredOptions, selectedIndex, startIndex } };
|
|
213
|
+
}
|
|
214
|
+
async function autocompletePromptEngine(config) {
|
|
215
|
+
const engine = new engine_1.UIEngine({
|
|
216
|
+
keypress: config.keypress,
|
|
217
|
+
ownsKeypress: false,
|
|
218
|
+
input: config.input,
|
|
219
|
+
output: config.output,
|
|
220
|
+
noTty: config.noTty,
|
|
221
|
+
clearScreenOnStart: true,
|
|
222
|
+
});
|
|
223
|
+
const initialState = {
|
|
224
|
+
options: config.options,
|
|
225
|
+
filteredOptions: config.options,
|
|
226
|
+
input: '',
|
|
227
|
+
selectedIndex: 0,
|
|
228
|
+
startIndex: 0,
|
|
229
|
+
maxLines: config.maxLines,
|
|
230
|
+
promptMessage: config.promptMessage,
|
|
231
|
+
};
|
|
232
|
+
return engine.run({
|
|
233
|
+
initialState,
|
|
234
|
+
render: autocompleteRender,
|
|
235
|
+
onEvent: autocompleteOnEvent,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
function checkboxRender(state) {
|
|
239
|
+
const lines = [];
|
|
240
|
+
// Header with input
|
|
241
|
+
lines.push(state.promptMessage + `> ${state.input}`);
|
|
242
|
+
// Filtered options with checkboxes
|
|
243
|
+
const endIndex = Math.min(state.startIndex + state.maxLines, state.filteredOptions.length);
|
|
244
|
+
for (let i = state.startIndex; i < endIndex; i++) {
|
|
245
|
+
const option = state.filteredOptions[i];
|
|
246
|
+
const isSelected = state.selectedIndex === i;
|
|
247
|
+
const marker = isSelected ? '>' : ' ';
|
|
248
|
+
// Find original index to get selection state
|
|
249
|
+
const originalIndex = state.options.findIndex(o => o.name === option.name);
|
|
250
|
+
if (originalIndex >= 0) {
|
|
251
|
+
const isChecked = state.selections[originalIndex] ? '◉' : '○';
|
|
252
|
+
const line = `${marker} ${isChecked} ${option.name}`;
|
|
253
|
+
lines.push(isSelected ? (0, yanse_1.blue)(line) : line);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
lines.push('No options');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return lines;
|
|
260
|
+
}
|
|
261
|
+
function checkboxOnEvent(event, state) {
|
|
262
|
+
const { options, maxLines, returnFullResults } = state;
|
|
263
|
+
let { input, filteredOptions, selections, selectedIndex, startIndex } = state;
|
|
264
|
+
const updateFiltered = () => {
|
|
265
|
+
filteredOptions = filterOptions(options, input);
|
|
266
|
+
// Clamp selectedIndex and startIndex when filtered results change
|
|
267
|
+
if (filteredOptions.length === 0) {
|
|
268
|
+
selectedIndex = 0;
|
|
269
|
+
startIndex = 0;
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
if (selectedIndex >= filteredOptions.length) {
|
|
273
|
+
selectedIndex = filteredOptions.length - 1;
|
|
274
|
+
}
|
|
275
|
+
if (startIndex > Math.max(0, filteredOptions.length - maxLines)) {
|
|
276
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
if (event.type === 'key') {
|
|
281
|
+
switch (event.key) {
|
|
282
|
+
case types_1.Key.UP:
|
|
283
|
+
if (filteredOptions.length === 0)
|
|
284
|
+
break;
|
|
285
|
+
selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
286
|
+
if (selectedIndex < startIndex) {
|
|
287
|
+
startIndex = selectedIndex;
|
|
288
|
+
}
|
|
289
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
290
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines);
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
case types_1.Key.DOWN:
|
|
294
|
+
if (filteredOptions.length === 0)
|
|
295
|
+
break;
|
|
296
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
297
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
298
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
299
|
+
}
|
|
300
|
+
else if (selectedIndex === 0) {
|
|
301
|
+
startIndex = 0;
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
case types_1.Key.BACKSPACE:
|
|
305
|
+
input = input.slice(0, -1);
|
|
306
|
+
updateFiltered();
|
|
307
|
+
break;
|
|
308
|
+
case types_1.Key.SPACE:
|
|
309
|
+
// Toggle selection
|
|
310
|
+
if (filteredOptions.length > 0 && filteredOptions[selectedIndex]) {
|
|
311
|
+
const originalIndex = options.indexOf(filteredOptions[selectedIndex]);
|
|
312
|
+
if (originalIndex >= 0) {
|
|
313
|
+
selections = [...selections];
|
|
314
|
+
selections[originalIndex] = !selections[originalIndex];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
case types_1.Key.ENTER:
|
|
319
|
+
const result = [];
|
|
320
|
+
if (returnFullResults) {
|
|
321
|
+
options.forEach((option, index) => {
|
|
322
|
+
result.push({
|
|
323
|
+
name: option.name,
|
|
324
|
+
value: option.value,
|
|
325
|
+
selected: selections[index]
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
options.forEach((option, index) => {
|
|
331
|
+
if (selections[index]) {
|
|
332
|
+
result.push({
|
|
333
|
+
name: option.name,
|
|
334
|
+
value: option.value,
|
|
335
|
+
selected: true
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
state: { ...state, input, filteredOptions, selections, selectedIndex, startIndex },
|
|
342
|
+
done: true,
|
|
343
|
+
value: result
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else if (event.type === 'char') {
|
|
348
|
+
input += event.char;
|
|
349
|
+
updateFiltered();
|
|
350
|
+
}
|
|
351
|
+
return { state: { ...state, input, filteredOptions, selections, selectedIndex, startIndex } };
|
|
352
|
+
}
|
|
353
|
+
async function checkboxPromptEngine(config) {
|
|
354
|
+
const engine = new engine_1.UIEngine({
|
|
355
|
+
keypress: config.keypress,
|
|
356
|
+
ownsKeypress: false,
|
|
357
|
+
input: config.input,
|
|
358
|
+
output: config.output,
|
|
359
|
+
noTty: config.noTty,
|
|
360
|
+
clearScreenOnStart: true,
|
|
361
|
+
});
|
|
362
|
+
const initialState = {
|
|
363
|
+
options: config.options,
|
|
364
|
+
filteredOptions: config.options,
|
|
365
|
+
selections: config.defaultSelections,
|
|
366
|
+
input: '',
|
|
367
|
+
selectedIndex: 0,
|
|
368
|
+
startIndex: 0,
|
|
369
|
+
maxLines: config.maxLines,
|
|
370
|
+
promptMessage: config.promptMessage,
|
|
371
|
+
returnFullResults: config.returnFullResults,
|
|
372
|
+
};
|
|
373
|
+
return engine.run({
|
|
374
|
+
initialState,
|
|
375
|
+
render: checkboxRender,
|
|
376
|
+
onEvent: checkboxOnEvent,
|
|
377
|
+
});
|
|
378
|
+
}
|