Skip to content
/ stdin Public

Node.js light ⚡, interactive and elegant input crafted for REPL experience.

License

Notifications You must be signed in to change notification settings

TopCli/stdin

Repository files navigation

Stdin

version Maintenance mit OpenSSF Scorecard build

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

Requirements

Getting Started

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

Usage example

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.

API

stdin(title: null | string, options?: StdinOptions): Promise< string >

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.

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Gentilhomme
Gentilhomme

💻 📖 👀 🛡️ 🐛
Alexandre Malaj
Alexandre Malaj

💻 📖
PierreDemailly
PierreDemailly

💻 ⚠️

License

MIT