npm.io
2.0.0 • Published 1 year ago

@topcli/stdin

Licence
MIT
Version
2.0.0
Deps
1
Size
38 kB
Vulns
0
Weekly
0
Stars
11

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}`);

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[];
}

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

Keywords