inquirerer 0.1.3 → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +165 -0
- package/esm/index.js +1 -0
- package/esm/keypress.js +71 -0
- package/esm/package.js +26 -0
- package/esm/prompt.js +338 -0
- package/esm/question/classes.js +2 -0
- package/esm/question/index.js +1 -0
- package/esm/utils.js +8 -0
- package/index.d.ts +1 -0
- package/index.js +17 -0
- package/keypress.d.ts +33 -0
- package/keypress.js +75 -0
- package/package.d.ts +1 -0
- package/package.js +30 -0
- package/package.json +30 -35
- package/prompt.d.ts +24 -0
- package/prompt.js +345 -0
- package/question/classes.d.ts +0 -0
- package/question/classes.js +2 -0
- package/question/index.d.ts +1 -0
- package/question/index.js +17 -0
- package/question/types.d.ts +34 -0
- package/question/types.js +2 -0
- package/utils.d.ts +1 -0
- package/utils.js +15 -0
- package/build/index.js +0 -117
- package/build/index.js.map +0 -1
- package/build/test/prompt.test.js +0 -331
- package/build/test/prompt.test.js.map +0 -1
- package/build/types/index.d.ts +0 -21
- package/index.ts +0 -86
- package/readme.md +0 -105
- package/test/prompt.test.ts +0 -236
- package/tsconfig.json +0 -18
- package/yarn.lock +0 -3332
- /package/{build/types/test/prompt.test.d.ts → esm/question/types.js} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# inquirerer
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="90" src="https://user-images.githubusercontent.com/545047/190171475-b416f99e-2831-4786-9ba3-a7ff4d95b0d3.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
|
|
9
|
+
<a href="https://github.com/pyramation/inquirerer2/actions/workflows/run-tests.yml">
|
|
10
|
+
<img height="20" src="https://github.com/pyramation/inquirerer2/actions/workflows/run-tests.yml/badge.svg" />
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/pyramation/inquirerer2/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/npm/dt/inquirerer"></a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/inquirerer"><img height="20" src="https://img.shields.io/github/package-json/v/pyramation/inquirerer2?filename=packages%2Finquirerer%2Fpackage.json"></a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
This library is designed to facilitate the creation of command-line utilities by providing a robust framework for capturing user input through interactive prompts. It supports a variety of question types, making it highly flexible and suitable for a wide range of applications.
|
|
18
|
+
|
|
19
|
+
_This is the new and improved version of [pyramation/inquirer](https://github.com/pyramation/inquirerer). Soon this will completely replace it!_
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install inquirerer
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- 🖊 **Multiple Question Types:** Support for text, autocomplete, checkbox, and confirm questions.
|
|
30
|
+
- 🛠 **Customizable Prompts:** Easily configure prompts to include default values, requirement flags, and custom validation rules.
|
|
31
|
+
- 🔎 **Interactive Autocomplete:** Dynamically filter options based on user input.
|
|
32
|
+
- ✔️ **Flexible Checkbox Selections:** Allow users to select multiple options from a list.
|
|
33
|
+
|
|
34
|
+
## Table of Contents
|
|
35
|
+
|
|
36
|
+
- [Introduction](#inquirerer)
|
|
37
|
+
- [Install](#install)
|
|
38
|
+
- [Features](#features)
|
|
39
|
+
- [Installation](#installation)
|
|
40
|
+
- [Usage](#usage)
|
|
41
|
+
- [Example Questions](#example-questions)
|
|
42
|
+
- [Text Question](#text-question)
|
|
43
|
+
- [Confirm Question](#confirm-question)
|
|
44
|
+
- [Autocomplete Question](#autocomplete-question)
|
|
45
|
+
- [Checkbox Question](#checkbox-question)
|
|
46
|
+
- [Executing a Prompt](#executing-a-prompt)
|
|
47
|
+
- [Developing](#developing)
|
|
48
|
+
- [Credits](#credits)
|
|
49
|
+
- [Disclaimer](#disclaimer)
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
To install the library, use `npm` or `yarn`:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm install inquirerer
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
Import the library and use it to create prompts for your command-line application:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { Inquirerer } from 'inquirerer';
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Example Questions
|
|
68
|
+
|
|
69
|
+
Here are several examples of different question types you can use:
|
|
70
|
+
|
|
71
|
+
#### Text Question
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
{
|
|
75
|
+
type: 'text',
|
|
76
|
+
name: 'firstName',
|
|
77
|
+
message: 'Enter your first name',
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Confirm Question
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
{
|
|
85
|
+
type: 'confirm',
|
|
86
|
+
name: 'continue',
|
|
87
|
+
message: 'Do you wish to continue?',
|
|
88
|
+
default: true,
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Autocomplete Question
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
{
|
|
96
|
+
type: 'autocomplete',
|
|
97
|
+
name: 'fruitChoice',
|
|
98
|
+
message: 'Select your favorite fruit',
|
|
99
|
+
options: [
|
|
100
|
+
'Apple', 'Banana', 'Cherry'
|
|
101
|
+
],
|
|
102
|
+
maxDisplayLines: 5
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Checkbox Question
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
{
|
|
110
|
+
type: 'checkbox',
|
|
111
|
+
name: 'techStack',
|
|
112
|
+
message: 'Choose your tech stack',
|
|
113
|
+
options: [
|
|
114
|
+
'Node.js', 'React', 'Angular', 'Vue.js'
|
|
115
|
+
],
|
|
116
|
+
returnFullResults: false,
|
|
117
|
+
required: true
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Executing a Prompt
|
|
122
|
+
|
|
123
|
+
To run a prompt and handle the response:
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
const prompter = new Inquirerer();
|
|
127
|
+
|
|
128
|
+
async function runPrompt() {
|
|
129
|
+
const responses = await prompter.prompt({}, [
|
|
130
|
+
{
|
|
131
|
+
type: 'text',
|
|
132
|
+
name: 'lastName',
|
|
133
|
+
message: 'Enter your last name',
|
|
134
|
+
required: true
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
type: 'checkbox',
|
|
138
|
+
name: 'devTools',
|
|
139
|
+
message: 'Select development tools:',
|
|
140
|
+
options: ['VS Code', 'Sublime', 'Atom'],
|
|
141
|
+
required: true
|
|
142
|
+
}
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
console.log(responses);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
runPrompt();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
## Developing
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
When first cloning the repo:
|
|
156
|
+
```
|
|
157
|
+
yarn
|
|
158
|
+
yarn build
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Disclaimer
|
|
162
|
+
|
|
163
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
164
|
+
|
|
165
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './prompt';
|
package/esm/keypress.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const defaultProcessWrapper = {
|
|
2
|
+
exit: (code) => process.exit(code)
|
|
3
|
+
};
|
|
4
|
+
export const KEY_CODES = {
|
|
5
|
+
UP_ARROW: '\u001b[A',
|
|
6
|
+
DOWN_ARROW: '\u001b[B',
|
|
7
|
+
RIGHT_ARROW: '\u001b[C',
|
|
8
|
+
LEFT_ARROW: '\u001b[D',
|
|
9
|
+
ENTER: '\r',
|
|
10
|
+
SPACE: ' ',
|
|
11
|
+
CTRL_C: '\u0003',
|
|
12
|
+
BACKSPACE: '\x7f',
|
|
13
|
+
BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
|
|
14
|
+
};
|
|
15
|
+
export class TerminalKeypress {
|
|
16
|
+
listeners = {};
|
|
17
|
+
active = true;
|
|
18
|
+
noTty;
|
|
19
|
+
input;
|
|
20
|
+
proc;
|
|
21
|
+
constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
|
|
22
|
+
this.noTty = noTty;
|
|
23
|
+
this.input = input;
|
|
24
|
+
this.proc = proc;
|
|
25
|
+
if (this.isTTY()) {
|
|
26
|
+
this.input.setRawMode(true);
|
|
27
|
+
this.input.resume();
|
|
28
|
+
this.input.setEncoding('utf8');
|
|
29
|
+
}
|
|
30
|
+
this.setupListeners();
|
|
31
|
+
}
|
|
32
|
+
isTTY() {
|
|
33
|
+
return !this.noTty;
|
|
34
|
+
}
|
|
35
|
+
setupListeners() {
|
|
36
|
+
this.input.on('data', (key) => {
|
|
37
|
+
if (!this.active)
|
|
38
|
+
return;
|
|
39
|
+
const handlers = this.listeners[key];
|
|
40
|
+
handlers?.forEach(handler => handler());
|
|
41
|
+
if (key === KEY_CODES.CTRL_C) { // Ctrl+C
|
|
42
|
+
this.proc.exit(0);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
on(key, callback) {
|
|
47
|
+
if (!this.listeners[key]) {
|
|
48
|
+
this.listeners[key] = [];
|
|
49
|
+
}
|
|
50
|
+
this.listeners[key].push(callback);
|
|
51
|
+
}
|
|
52
|
+
off(key, callback) {
|
|
53
|
+
if (this.listeners[key]) {
|
|
54
|
+
const index = this.listeners[key].indexOf(callback);
|
|
55
|
+
if (index !== -1) {
|
|
56
|
+
this.listeners[key].splice(index, 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
pause() {
|
|
61
|
+
this.active = false;
|
|
62
|
+
}
|
|
63
|
+
resume() {
|
|
64
|
+
this.active = true;
|
|
65
|
+
}
|
|
66
|
+
destroy() {
|
|
67
|
+
this.input.setRawMode(false);
|
|
68
|
+
this.input.pause();
|
|
69
|
+
this.input.removeAllListeners('data');
|
|
70
|
+
}
|
|
71
|
+
}
|
package/esm/package.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "fs";
|
|
2
|
+
import { dirname, join } from "path";
|
|
3
|
+
// need to search due to the dist/ folder and src/, etc.
|
|
4
|
+
function findPackageJson(currentDir) {
|
|
5
|
+
const filePath = join(currentDir, 'package.json');
|
|
6
|
+
// Check if package.json exists in the current directory
|
|
7
|
+
if (existsSync(filePath)) {
|
|
8
|
+
return filePath;
|
|
9
|
+
}
|
|
10
|
+
// Get the parent directory
|
|
11
|
+
const parentDir = dirname(currentDir);
|
|
12
|
+
// If reached the root directory, package.json is not found
|
|
13
|
+
if (parentDir === currentDir) {
|
|
14
|
+
throw new Error('package.json not found in any parent directory');
|
|
15
|
+
}
|
|
16
|
+
// Recursively look in the parent directory
|
|
17
|
+
return findPackageJson(parentDir);
|
|
18
|
+
}
|
|
19
|
+
export function readAndParsePackageJson() {
|
|
20
|
+
// Start searching from the current directory
|
|
21
|
+
const pkgPath = findPackageJson(__dirname);
|
|
22
|
+
// Read and parse the package.json
|
|
23
|
+
const str = readFileSync(pkgPath, 'utf8');
|
|
24
|
+
const pkg = JSON.parse(str);
|
|
25
|
+
return pkg;
|
|
26
|
+
}
|
package/esm/prompt.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import readline from 'readline';
|
|
3
|
+
import { KEY_CODES, TerminalKeypress } from './keypress';
|
|
4
|
+
const requiredMessage = (question) => chalk.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
5
|
+
function generatePromptMessage(question, ctx) {
|
|
6
|
+
let promptMessage = question.message ? `${question.message}\n> ` : `${question.name}:\n> `;
|
|
7
|
+
if (ctx.numTries > 0 && question.required) {
|
|
8
|
+
promptMessage = requiredMessage(question) + promptMessage;
|
|
9
|
+
}
|
|
10
|
+
switch (question.type) {
|
|
11
|
+
case 'confirm':
|
|
12
|
+
promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
|
|
13
|
+
break;
|
|
14
|
+
case 'text':
|
|
15
|
+
if (question.default) {
|
|
16
|
+
promptMessage += ` [${question.default}]`;
|
|
17
|
+
}
|
|
18
|
+
break;
|
|
19
|
+
case 'autocomplete':
|
|
20
|
+
case 'checkbox':
|
|
21
|
+
// For these types, you might want to show the default selected options if any
|
|
22
|
+
if (question.options && question.default) {
|
|
23
|
+
const defaultOptions = Array.isArray(question.default) ? question.default : [question.default];
|
|
24
|
+
const defaultText = defaultOptions.join(', ');
|
|
25
|
+
promptMessage += ` [${defaultText}]`;
|
|
26
|
+
}
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
return promptMessage;
|
|
30
|
+
}
|
|
31
|
+
export class Inquirerer {
|
|
32
|
+
rl;
|
|
33
|
+
keypress;
|
|
34
|
+
noTty;
|
|
35
|
+
output;
|
|
36
|
+
constructor(noTty = false, input = process.stdin, output = process.stdout) {
|
|
37
|
+
this.noTty = noTty;
|
|
38
|
+
this.output = output;
|
|
39
|
+
if (!noTty) {
|
|
40
|
+
this.rl = readline.createInterface({
|
|
41
|
+
input,
|
|
42
|
+
output
|
|
43
|
+
});
|
|
44
|
+
this.keypress = new TerminalKeypress(noTty, input);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.rl = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
clearScreen() {
|
|
51
|
+
// same as console.clear()
|
|
52
|
+
this.output.write('\x1Bc'); // This is the escape sequence to clear the terminal screen.
|
|
53
|
+
}
|
|
54
|
+
write(message) {
|
|
55
|
+
this.output.write(message);
|
|
56
|
+
}
|
|
57
|
+
log(message) {
|
|
58
|
+
this.output.write(message + '\n');
|
|
59
|
+
}
|
|
60
|
+
async prompt(params, questions, usageText) {
|
|
61
|
+
const obj = { ...params };
|
|
62
|
+
// when interactive and missing a bunch of stuff, we should display to the user
|
|
63
|
+
if (usageText && Object.values(params).some(value => value === undefined) && !this.noTty) {
|
|
64
|
+
this.log(usageText);
|
|
65
|
+
}
|
|
66
|
+
const needsContinue = (question) => {
|
|
67
|
+
const value = obj[question.name];
|
|
68
|
+
return (value === undefined ||
|
|
69
|
+
value === null ||
|
|
70
|
+
(typeof value === 'string' && value.trim().length === 0) // Check for empty string, safely trimming it
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
let index = 0;
|
|
74
|
+
let numTries = 0;
|
|
75
|
+
while (index < questions.length) {
|
|
76
|
+
const question = questions[index];
|
|
77
|
+
const ctx = {
|
|
78
|
+
numTries
|
|
79
|
+
};
|
|
80
|
+
if (needsContinue(question)) {
|
|
81
|
+
switch (question.type) {
|
|
82
|
+
case 'confirm':
|
|
83
|
+
obj[question.name] = await this.confirm(question, ctx);
|
|
84
|
+
break;
|
|
85
|
+
case 'checkbox':
|
|
86
|
+
obj[question.name] = await this.checkbox(question, ctx);
|
|
87
|
+
break;
|
|
88
|
+
case 'autocomplete':
|
|
89
|
+
obj[question.name] = await this.autocomplete(question, ctx);
|
|
90
|
+
break;
|
|
91
|
+
case 'text':
|
|
92
|
+
default:
|
|
93
|
+
obj[question.name] = await this.text(question, ctx); // Use promptText instead of text
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
// Check if the question is required and the response is not adequate
|
|
97
|
+
if (question.required && needsContinue(question)) {
|
|
98
|
+
// Reset the property to undefined to re-trigger the prompt
|
|
99
|
+
numTries++;
|
|
100
|
+
obj[question.name] = undefined;
|
|
101
|
+
continue; // Stay on the same question
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
index++; // Move to the next question
|
|
105
|
+
numTries = 0;
|
|
106
|
+
}
|
|
107
|
+
return obj;
|
|
108
|
+
}
|
|
109
|
+
async confirm(question, ctx) {
|
|
110
|
+
if (this.noTty || !this.rl)
|
|
111
|
+
return question.default ?? false; // Return default if non-interactive
|
|
112
|
+
return new Promise((resolve) => {
|
|
113
|
+
// Construct the prompt with the default option indicated
|
|
114
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
115
|
+
this.rl.question(promptMessage, (answer) => {
|
|
116
|
+
const userInput = answer.trim().toLowerCase();
|
|
117
|
+
if (userInput === '') {
|
|
118
|
+
resolve(question.default ?? false); // Use default value if input is empty
|
|
119
|
+
}
|
|
120
|
+
else if (['yes', 'y'].includes(userInput)) {
|
|
121
|
+
resolve(true);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
resolve(false);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
async text(question, ctx) {
|
|
130
|
+
if (this.noTty || !this.rl)
|
|
131
|
+
return question.default ?? null; // Return default if non-interactive
|
|
132
|
+
let userInput = '';
|
|
133
|
+
return new Promise((resolve) => {
|
|
134
|
+
this.clearScreen(); // Clear the console at the beginning of each input session
|
|
135
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
136
|
+
this.rl.question(promptMessage, (answer) => {
|
|
137
|
+
userInput = answer;
|
|
138
|
+
if (userInput.trim() !== '') {
|
|
139
|
+
resolve(userInput); // Return input if not empty
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
resolve(null); // Return null if empty and not required
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async checkbox(question, ctx) {
|
|
148
|
+
if (this.noTty || !this.rl)
|
|
149
|
+
return question.default ?? []; // Return default if non-interactive
|
|
150
|
+
this.keypress.resume();
|
|
151
|
+
const options = question.options || [];
|
|
152
|
+
let input = ''; // Search input
|
|
153
|
+
let filteredOptions = options;
|
|
154
|
+
let selectedIndex = 0;
|
|
155
|
+
let startIndex = 0; // Start index for visible options
|
|
156
|
+
const maxLines = question.maxDisplayLines || options.length; // Use provided max or total options
|
|
157
|
+
const selections = new Array(options.length).fill(false);
|
|
158
|
+
const updateFilteredOptions = () => {
|
|
159
|
+
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
160
|
+
};
|
|
161
|
+
const display = () => {
|
|
162
|
+
this.clearScreen();
|
|
163
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
164
|
+
this.write(promptMessage);
|
|
165
|
+
this.log(`${input}`);
|
|
166
|
+
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
167
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
168
|
+
const option = filteredOptions[i];
|
|
169
|
+
const isSelected = selectedIndex === i;
|
|
170
|
+
const marker = isSelected ? '>' : ' ';
|
|
171
|
+
const isChecked = selections[options.indexOf(option)] ? '◉' : '○'; // Use the original index in options
|
|
172
|
+
const line = `${marker} ${isChecked} ${option}`;
|
|
173
|
+
this.log(isSelected ? chalk.blue(line) : line);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
display();
|
|
177
|
+
// Handling BACKSPACE key
|
|
178
|
+
this.keypress.on(KEY_CODES.BACKSPACE, () => {
|
|
179
|
+
input = input.slice(0, -1);
|
|
180
|
+
updateFilteredOptions();
|
|
181
|
+
display();
|
|
182
|
+
});
|
|
183
|
+
// Register alphanumeric keypresses to accumulate input, excluding space
|
|
184
|
+
'abcdefghijklmnopqrstuvwxyz0123456789'.split('').forEach(char => {
|
|
185
|
+
this.keypress.on(char, () => {
|
|
186
|
+
input += char;
|
|
187
|
+
updateFilteredOptions();
|
|
188
|
+
display();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
this.keypress.on(KEY_CODES.UP_ARROW, () => {
|
|
192
|
+
selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
193
|
+
if (selectedIndex < startIndex) {
|
|
194
|
+
startIndex = selectedIndex; // Scroll up
|
|
195
|
+
}
|
|
196
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
197
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines); // Jump to the bottom of the list
|
|
198
|
+
}
|
|
199
|
+
display();
|
|
200
|
+
});
|
|
201
|
+
this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
|
|
202
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
203
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
204
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
205
|
+
}
|
|
206
|
+
else if (selectedIndex === 0) {
|
|
207
|
+
startIndex = 0; // Jump to the top of the list
|
|
208
|
+
}
|
|
209
|
+
display();
|
|
210
|
+
});
|
|
211
|
+
this.keypress.on(KEY_CODES.SPACE, () => {
|
|
212
|
+
// Map filtered index back to the original index in options
|
|
213
|
+
selections[options.indexOf(filteredOptions[selectedIndex])] = !selections[options.indexOf(filteredOptions[selectedIndex])];
|
|
214
|
+
display();
|
|
215
|
+
});
|
|
216
|
+
return new Promise(resolve => {
|
|
217
|
+
this.keypress.on(KEY_CODES.ENTER, () => {
|
|
218
|
+
this.keypress.pause();
|
|
219
|
+
const result = [];
|
|
220
|
+
if (question.returnFullResults) {
|
|
221
|
+
// Return all options with their selected status
|
|
222
|
+
options.forEach((option, index) => {
|
|
223
|
+
result.push({
|
|
224
|
+
name: option,
|
|
225
|
+
value: selections[index]
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
// Return only options that are selected
|
|
231
|
+
options.forEach((option, index) => {
|
|
232
|
+
if (selections[index]) {
|
|
233
|
+
result.push({
|
|
234
|
+
name: option,
|
|
235
|
+
value: selections[index]
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
resolve(result);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
async autocomplete(question, ctx) {
|
|
245
|
+
if (this.noTty || !this.rl)
|
|
246
|
+
return question.default ?? false; // Return default if non-interactive
|
|
247
|
+
this.keypress.resume();
|
|
248
|
+
const options = question.options || [];
|
|
249
|
+
let input = '';
|
|
250
|
+
let filteredOptions = options;
|
|
251
|
+
let selectedIndex = 0;
|
|
252
|
+
let startIndex = 0; // Start index for visible options
|
|
253
|
+
const maxLines = question.maxDisplayLines || options.length; // Use provided max or total options
|
|
254
|
+
const display = () => {
|
|
255
|
+
this.clearScreen();
|
|
256
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
257
|
+
this.log(promptMessage);
|
|
258
|
+
// Determine the range of options to display
|
|
259
|
+
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
260
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
261
|
+
const option = filteredOptions[i];
|
|
262
|
+
if (i === selectedIndex) {
|
|
263
|
+
this.log(chalk.blue('> ' + option)); // Highlight the selected option with chalk
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
this.log(' ' + option);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const updateFilteredOptions = () => {
|
|
271
|
+
filteredOptions = this.filterOptions(options, input);
|
|
272
|
+
// Adjust startIndex to keep the selectedIndex in the visible range
|
|
273
|
+
if (selectedIndex < startIndex) {
|
|
274
|
+
startIndex = selectedIndex;
|
|
275
|
+
}
|
|
276
|
+
else if (selectedIndex >= startIndex + maxLines) {
|
|
277
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
278
|
+
}
|
|
279
|
+
if (selectedIndex >= filteredOptions.length) {
|
|
280
|
+
selectedIndex = Math.max(filteredOptions.length - 1, 0);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
display();
|
|
284
|
+
// Handling BACKSPACE key
|
|
285
|
+
this.keypress.on(KEY_CODES.BACKSPACE, () => {
|
|
286
|
+
input = input.slice(0, -1);
|
|
287
|
+
updateFilteredOptions();
|
|
288
|
+
display();
|
|
289
|
+
});
|
|
290
|
+
// Register alphanumeric and space keypresses to accumulate input
|
|
291
|
+
'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
|
|
292
|
+
this.keypress.on(char, () => {
|
|
293
|
+
input += char;
|
|
294
|
+
updateFilteredOptions();
|
|
295
|
+
display();
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
// Navigation
|
|
299
|
+
this.keypress.on(KEY_CODES.UP_ARROW, () => {
|
|
300
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
301
|
+
if (selectedIndex < startIndex) {
|
|
302
|
+
startIndex = selectedIndex; // Scroll up
|
|
303
|
+
}
|
|
304
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
305
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines); // Jump to the bottom of the list
|
|
306
|
+
}
|
|
307
|
+
display();
|
|
308
|
+
});
|
|
309
|
+
this.keypress.on(KEY_CODES.DOWN_ARROW, () => {
|
|
310
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
311
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
312
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
313
|
+
}
|
|
314
|
+
else if (selectedIndex === 0) {
|
|
315
|
+
startIndex = 0; // Jump to the top of the list
|
|
316
|
+
}
|
|
317
|
+
display();
|
|
318
|
+
});
|
|
319
|
+
return new Promise(resolve => {
|
|
320
|
+
this.keypress.on(KEY_CODES.ENTER, () => {
|
|
321
|
+
this.keypress.pause();
|
|
322
|
+
resolve(filteredOptions[selectedIndex] || input);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
filterOptions(options, input) {
|
|
327
|
+
return options
|
|
328
|
+
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
329
|
+
.sort();
|
|
330
|
+
}
|
|
331
|
+
// Method to cleanly close the readline interface
|
|
332
|
+
close() {
|
|
333
|
+
if (this.rl) {
|
|
334
|
+
this.rl.close();
|
|
335
|
+
this.keypress.destroy();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types';
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { readAndParsePackageJson } from "./package";
|
|
3
|
+
// Function to display the version information
|
|
4
|
+
export function displayVersion() {
|
|
5
|
+
const pkg = readAndParsePackageJson();
|
|
6
|
+
console.log(chalk.green(`Name: ${pkg.name}`));
|
|
7
|
+
console.log(chalk.blue(`Version: ${pkg.version}`));
|
|
8
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './prompt';
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./prompt"), exports);
|