A lightweight and interactive standard input (stdin) utility for Node.js, designed for REPL-like experiences with support for auto-completion and history.
Internally, it leverages process.stdin
in raw mode to capture key-by-key input directly from the TTY stream.
Demo with the SlimIO CLI
- Node.js v22 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @topcli/stdin
# or
$ yarn add @topcli/stdin
import stdin from "@topcli/stdin";
const data = await stdin("Question title > ", {
history: ["command in history 1", "command in history 2"],
autocomplete: [
"events",
"events.get_info"
]
});
console.log(`input data: ${data}`);
Tip
The history array is passed by reference. If you plan to run multiple prompts in a loop, define a shared history array outside the loop to persist input history across calls.
Prompts the user for input with an optional title and options. If title is null
, the prompt will be displayed without a prefix.
StdinOptions
interface
interface StdinOptions {
/**
* An array of previous commands or inputs to enable navigation with arrow keys.
*/
history?: string[];
/**
* A list of strings used to suggest autocompletion while typing.
*/
autocomplete?: string[];
}
Caution
This module does not implement history persistence or caching. If you want to persist history across sessions, consider using a package like cacache.
Thanks goes to these wonderful people (emoji key):
Gentilhomme 💻 📖 👀 🛡️ 🐛 |
Alexandre Malaj 💻 📖 |
PierreDemailly 💻 |
MIT