1.0.4 • Published 3 years ago

better_read v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

better_read

A better readline for Node.js with features like autofill and custom visuals.

Installation

npm install better_read

Usage

better_read works with promises, enabling both synchronous and asynchronous input taking.

Synchronous use:

const read = require("better_read")

async fuction main() {
  console.log("\n" + await read.prompt("> ")
}

main()

Asynchronous use:

const read = require("better_read")

read.prompt("> ").then(input => console.log("\n" + input)

API:

prompt

read.prompt([query[, hide]])
Typeasync
ReturnsPromise<>

Dispalys a query to the user and prompts them for input.

Example:

async function question(query) {
  console.log(await read.prompt(query))
}

question("Synchronous with async-await: ")
read.prompt("Asynchronous with then: ").then(input => console.log(input))

query:

Typestring
Optionalfalse

Query to show to the user.

hide

Typeboolean
Optionaltrue
Defaultfalse

If true, the input is masked with the * character.

setVisual

read.setVisual([function])
Typesync
Returnsvoid

The visual effects (hints, previews, usage) for the next prompt is set with this function. It is reset automatically after calling prompt.

Example:

This example automatically shows the length of the input string:

async function main() {
  read.setVisual(input => console.log(input.length))
  let input = read.prompt("Length of: ")
  console.log("Length of " + input + " is " + input.length + ".")
}
main()

This function is automatically called when a change occurs to the input with a single argument, the input at the time. The function can freely print everything it wants according to the input argument. The printing automatically begins from right after the input:

> input_text<printing starts here>

The application automatically handles all the erasing and re-rendering, so you do not need to include any erasing functions.

function

Typestring
Optionalfalse

The function to be called for visualization.

setAutofill

read.setAutofill([function])
Typesync
Returnsvoid

Autofilling the input on the tab press is set using this command. It is reset automatically after calling prompt.

Example:

This example auto-fills the input with "hello" when tab is pressed and the input is "h":

async function main() {
  read.setAutofill(input => (input === "h")? "ello" : ""))
  let input = read.prompt("Length of: ")
  console.log("Length of " + input + " is " + input.length + ".")
}
main()

The application automatically calls the function when the input changes with one argument, the input at the time. The return value of the function is then appended to the input. As in the above example, the input is "h" and the return-value of the autofill function is "ello", which is appended to the input, "h" + "ello" = "hello".

function

Typestring
Optionalfalse
1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago