0.1.2 • Published 9 months ago

@rasch/prompt v0.1.2

Weekly downloads
-
License
0BSD
Repository
github
Last release
9 months ago

prompt

an interactive prompt for JavaScript CLI applications

Install

pnpm add @rasch/prompt
npm install @rasch/prompt
yarn add @rasch/prompt

Quick Start

import { prompt } from "@rasch/prompt"

const name = await prompt("What's your name? ")
const password = await prompt("Please enter your password: ", { silent: true })

console.log(`Welcome back, ${name}!`)

There are additional examples available!

API

Options :: { silent: Boolean, completions: [String], repl: Boolean, handleInput: HandleInput }
HandleInput :: (String, Interface, Options) => Undefined

prompt :: (String, Options) -> Promise(String)
prompt(query, options)

Import the Module

import { prompt } from "@rasch/prompt"

Simple Example

The original reason for this module is to hide the user input during password authentication. The input is not printed to the console (like unix system passwords) when the silent option is set to true and it is not saved to history.

const password = await prompt("Enter Password: ", { silent: true })

This prompt module can work for simple use cases...

const answer = await prompt("Which way do you want to go? ")

...but is not really needed here, since the built-in rl.question does nearly the same thing.

Completions

Tab autocomplete is available by providing an array of strings to the completions option. Completion is performed by simply filtering the array based on what the current input starts with. Sorry, there is no fuzzy filtering (currently).

const answer = await prompt(
  "Which way do you want to go? ",
  { completions: "north south east west up down left right".split(" ") }
)

REPL

A READ-EVAL-PRINT Loop is available by setting the repl option to true. (Please use your favorite robot voice when reading the italic text in the above sentence. Actually, that is the correct way to read ALL italicized text.)

const answer = await prompt(
  "Which way do you want to go? ", {
    repl: true,
    completions: "north south east west up down left right".split(" "),
  }
)

The REPL above is useless since it doesn't handle the user input. The option handleInput should be provided with a callback function. The callback function accepts up to three arguments:

  • input: the trimmed string provided by the user
  • rl: the Readline Interface which provides access to methods such as rl.history, rl.setPrompt, and rl.write.
  • opts: the object that was provided to the prompt module. This allows the options, including handleInput itself, to be dynamically modified.
const answer = await prompt(
  "Which way do you want to go? ", {
    repl: true,
    completions: "north south east west up down left right".split(" "),
    handleInput(input, rl, opts = {}) {
      switch (input) {
        case "north":
        case "up":
          opts.repl = false
          enterCastle()
          break
        case "east":
        case "right":
          console.log(
            "You entered the infinite Forest and somehow ended\n" +
            "up back where you started."
          )
          break
        case "south":
        case "down":
          opts.repl = false
          ohCoolASkatePark("Mission over! Let's skate instead.")
          break
        case "west":
        case "left":
          opts.repl = false
          enterPit("You fell in a deep pit just west of the castle!")
          break
        default:
          console.log(
            `You can't go "${input}". I don't even know what that is.`
          )
      }
    },
  }
)
0.1.2

9 months ago

0.1.1

11 months ago

0.1.0

11 months ago