Gathering detailed insights and metrics for prompt-sync-plus
Gathering detailed insights and metrics for prompt-sync-plus
Gathering detailed insights and metrics for prompt-sync-plus
Gathering detailed insights and metrics for prompt-sync-plus
A Node.js synchronous prompting library based on promp-sync
npm install prompt-sync-plus
Typescript
Module System
Node Version
NPM Version
TypeScript (51.69%)
JavaScript (47.6%)
Shell (0.71%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
6 Stars
77 Commits
2 Forks
1 Watchers
6 Branches
1 Contributors
Updated on Jun 10, 2025
Latest Version
1.0.2
Package Id
prompt-sync-plus@1.0.2
Unpacked Size
60.41 kB
Size
17.19 kB
File Count
5
NPM Version
8.19.2
Node Version
18.11.0
Published on
Jan 16, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
An easy-to-use, synchronous prompt for Node.js based on the widely-adopted prompt-sync. The intent behind this project is to expand upon the original work of heapwolf, et. al., clean up, modernize, and patch the library to fix several existing issues and add some additional features. This library should be a 1:1 drop-in for the original and should at minimum require only an install and replacement of existing imports.
Changes include:
Install via NPM:
npm install --save prompt-sync-plus
At minimum, you need to import the library and instantiate the prompt. The entered text is returned directly:
1import psp from "prompt-sync-plus"; 2 3const prompt = psp(); 4const result = prompt("How are you? "); 5 6console.log(`You responded with: '${result}'`);
Prompt settings can be supplied via JSON object globally and/or on a prompt-by-prompt basis. Global settings are supplied when the prompt is instantiated:
1import psp from "prompt-sync-plus"; 2 3const prompt = psp({ 4 /* your settings here */ 5});
Prompt-specific settings can be supplied either as the second or third argument to the prompt invocation.
1const result = prompt("How are you?", { 2 /* your settings here */ 3});
Or with a default response supplied:
1const result = prompt("How are you?", "Good", { 2 /* your settings here */ 3});
Prompt-specific settings override global settings wherever there is overlap:
1import psp from "prompt-sync-plus"; 2 3const prompt = psp({ sigint: true }); 4 5// overrides sigint behavior established by global setting above 6const result = prompt("How are you?", { sigint: false });
Both methods of configuration take the same JSON schema. See API for a full listing of available settings, or keep scrolling to see examples of various settings in action.
A global default value can be supplied via the defaultResponse
field:
1import psp from "prompt-sync-plus"; 2 3const prompt = psp({ defaultResponse: "No response" }); 4const result = prompt("Some question"); 5 6console.log(result); // No response
A prompt-specific default value can be supplied as a second argument to the prompt:
1const result = prompt("How are you? ", "Good"); 2 3console.log(`You responded with: '${result}'`); // You responded with Good
Prompt input that spans across multiple lines is supported in every interaction within prompt-sync-plus, including simple prompts, autocomplete suggestions, history scrolling, etc. Use arrow keys to position the cursor anywhere within the given multi-line input and edit it from the middle:
Note: prompt history supercedes up and down arrow key behavior. To use up and down arrow keys for positioning, the current prompt call must not have prompt history enabled.
The above support for multi-line inputs applies to the prompts themselves, for example you can use a multi-line JavaScript template literal:
1import psp from "prompt-sync-plus"; 2 3const prompt = psp(); 4 5const result = prompt(`Enter 6Something 7Here: `); 8 9console.log(`You entered: '${result}'`);
For password entry, etc. character input can be obscured via the echo
field:
1const result = prompt("Password: ", { echo: "*" });
To omit output entirely, supply the empty string to echo
:
1const result = prompt("Sensitive info: ", { echo: "" });
Prompt-sync-plus exposes a helpful shorthand for the syntax above:
1const result = prompt.hide("Sensitive info: ");
Handling of SIGINT (Ctrl+C) is configured via the sigint
boolean field. It determines whether to kill the process and return code 130 (true
) or gobble up the signal and immediately return null
from the prompt (false
). The latter is the default.
1const result = prompt("Enter something or CTRL+C to quit: ", { 2 sigint: true, 3});
Handling end-of-transmission (CTRL+D) is configured via the eot
boolean field. It determines whether to kill the process and return code 0 (true
) or gobble up the signal and continue prompting (false
). The latter is the default behavior.
1const result = prompt("Enter something CTRL+D: ", { 2 eot: true, 3});
Prompt-sync-plus supports a few different methods for providing users with autocomplete output. Note that this is an area where major changes were made to the original approach of prompt-sync, so your code will likely need some adjustments to use prompt-sync-plus.
At minimum, a search function needs to be passed in to the configuration to enable autocomplete - prompt-sync-plus handles the display and selection of results, but it is up to the caller to decide selection criteria with their own code:
1const listOfWords = [ 2 "interspecies", 3 "interstelar", 4 "interstate", 5 "interesting", 6 "interoperating", 7 "intolerant", 8 "introversion", 9 "introspection", 10 "interrogation", 11]; 12 13const findWordStart = (str) => 14 listOfWords.filter((word) => word.indexOf(str) === 0); 15 16// a simple autocomplete algorithm - find word starts 17const result = prompt("Enter a word: ", { 18 autocomplete: { 19 searchFn: findWordStart, 20 }, 21});
In the example above, autocomplete can be initiated from the prompt with a TAB key press. There are a few different prompting behaviors outlined below:
This is the default autocompletion behavior, but can also be configured explicitly via the behavior
field:
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.CYCLE, 5 }, 6});
This behavior cycles through each of the autocomplete results and replaces the input string at the cursor location. At the end of the autocomplete result list, cycle loops around to the start of the list.
This behavior leaves input intact but outputs columns of suggested words made from the list of autocomplete results. When these is only one matching autocomplete result, the rest of the word is filled in automatically.
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.SUGGEST, 5 }, 6});
Autocomplete SUGGEST supports some additional configuration:
Determine how many columns are displayed in the resulting output with the suggestColCount
field:
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.SUGGEST, 5 suggestColCount: 5, 6 }, 7});
The default value is 3
.
This setting has no impact on the CYCLE behavior.
Determine whether prompt-sync-plus fills user input up to the common starting substring of the results with the fill
field:
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.SUGGEST, 5 fill: true, 6 }, 7});
The default value is false
.
This setting has no impact on other autocomplete behaviors.
Determine whether, for the duration of the current prompt execution, autocomplete executes on every key stroke, or only on the configured key (TAB, by default) via the sticky
field:
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.SUGGEST, 5 sticky: true, 6 }, 7});
The default value is false
- i.e. autocomplete only triggers on TAB (or whichever key is configured to trigger autocomplete; see additional settings). Note that sticky is only configurable with AutocompleteBehavior.SUGGEST
.
This behavior is a hybrid of CYCLE and SUGGEST. Prompt-sync-plus will output columns of suggested words based on the autocomplete search results, in addition to filling the input line with each successive word in the list.
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 behavior: AutocompleteBehavior.HYBRID, 5 }, 6});
By default, autocomplete triggers on the TAB key, but this is configurable with the triggerKey
field:
1const result = prompt("Enter a word: ", { 2 autocomplete: { 3 searchFn: findWordStart, 4 triggerKey: 96, // back tick 5 }, 6});
Trigger keys are just defined by the ascii character you want. This library also provides a helpful utility for specifying keys by name to keep your code more self-documenting:
1import psp, { Key } from "prompt-sync-plus"; 2 3const findWordStart = /* etc */ 4 5const prompt = psp({ 6 autocomplete: { 7 searchFn: findWordStart, 8 triggerKey: Key.BACK_TICK 9 } 10});
The line history interface hasn't changed from prompt-sync and can be used in the same way:
1import psp from "prompt-sync-plus"; 2import promptSyncHistory from "prompt-sync-history"; 3 4const prompt = psp({ 5 history: promptSyncHistory(), 6}); 7 8let result = prompt("Prompt 1: "); 9result = prompt("Prompt 2: "); 10result = prompt("Prompt 3: "); 11result = prompt("Prompt 4: "); 12result = prompt("Prompt 5: "); 13 14/* user can choose to up or down arrow to scroll through past responses */ 15 16// or persist responses to disk 17prompt.history.save();
See prompt-sync-history to learn more about the expected interface and the resulting API.
Contributions are welcome and encouraged! Feel free to:
To work on prompt-sync-plus:
npm install
to pull down dependenciesnpm run build
to compile the TypeScript
npm run test
to run unit tests or npm run test-coverage
to run tests and output a test coverage report
In general: prompt-sync-plus development follows the Git Feature Branch workflow - new feature work or bug fixes should be done in a dedicated branch.
Attempt to add tests to the test suite whenever possible.
This project uses husky to run pre- and post-commit git hooks to run code formatting via prettier, build, run tests, and update status badges.
This process will fail if tests fail, code coverage dips below minimum, or the build fails. Github actions run in response to pull requests to build and run tests.
Like any open source project, this one's a work in progress. Additional work includes, but is not limited to:
npm audit
, and generate a badge based on the result. Add badge to readme.prompt.yesno()
prompt.yesnomaybe()
prompt.choose(list)
This project is licensed under the MIT license. In general, behave in the spirit of the DBaD license.
No vulnerabilities found.
No security vulnerabilities found.