Input

There are two ways to collect text input from users. The quick way is the standalone prompt() function — it prints a question and waits for a line of text. For more control, reach for the Input class, which renders a bordered input box with label, password masking, custom styling, and events.

prompt()

A simple one-liner to ask the user something and get a string back. Supports plain text and password masking.

Example of ts-better-console prompt function in action
function prompt(
question?: string,
type?: InputType,
isArrowKeyNavigationEnabled?: boolean,
): Promise<string>

Parameters

  • question — the prompt text shown before the cursor. Defaults to "> ".
  • type"text" (default) or "password" (hides what the user types).
  • isArrowKeyNavigationEnabled — when true, enables command-history navigation with the ↑ / ↓ arrow keys (like a shell). Each submitted value is pushed into an internal history buffer. Defaults to false.

Cursor movement with ← / → is always active — you can navigate anywhere inside the current input to insert or delete characters. Backspace removes the character immediately to the left of the cursor, not just the last character in the buffer. Control sequences and other non-printable keys are silently ignored so they never corrupt the input.

import { prompt } from "ts-better-console";

const name = await prompt("What's your name? ");
console.log(`Hello, ${name}!`);

// Password input
const secret = await prompt("Password: ", "password");

// Shell-like prompt with ↑/↓ history navigation
const cmd = await prompt("$ ", "text", true);

Input class

For a richer experience, Input renders a bordered text field with a label, supports password mode, custom width and styling, and emits events as the user types.

Example of ts-better-console input class in action
new Input(options?: InputOptions)

Options

  • type"text" or "password". Defaults to "text".
  • label — text shown in the top border. Defaults to "Type something".
  • defaultValue — pre-filled value.
  • width — same as Card: number, ratio, or "auto". Defaults to 50.
  • align"left", "center", or "right".
  • position"inline", "top", or "bottom".
  • styles — object with input, label, and border style options.

Methods

  • prompt() — shows the input and returns a promise that resolves with the submitted value.

Events

  • "submit" — fired when the user presses Enter. Passes the value.
  • "change" — fired on every keystroke. Passes the current value.
  • "exit" — fired when the user presses Ctrl+C.
import { Input } from "ts-better-console";

const input = new Input({
label: "Enter your name",
width: 40,
styles: {
label: { color: "cyan", styles: ["bold"] },
},
});

const value = await input.prompt();
console.log("You entered:", value);

Want to support this project?

If you find ts-better-console useful and want to support its development, consider starring the GitHub repository or buying me a coffee! Your support helps me dedicate more time to improving the library and adding new features.

Want to contribute to this project?

Contributions are welcome! If you're interested in improving the library, fixing bugs, or adding new features, feel free to check out the GitHub repository and submit a pull request. Whether you're a seasoned developer or new to open source, your contributions can make a difference!