# prompt-sync

> a synchronous prompt for node.js

Latest version **4.2.0** (published 2019-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install prompt-sync
pnpm add prompt-sync
yarn add prompt-sync
bun add prompt-sync
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.0 |
| Published | 2019-12-20 |
| First published | 2014-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/prompt-sync) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 227 |
| Maintainers | davidmarkclements, hij1nx |
| Keywords | prompt, sync, blocking, readline, input, getline, repl, history |

## Links

- npm: https://www.npmjs.com/package/prompt-sync
- Repository: https://github.com/heapwolf/prompt-sync
- Homepage: https://github.com/heapwolf/prompt-sync#readme
- Issues: https://github.com/heapwolf/prompt-sync/issues
- npm.io page: https://npm.io/package/prompt-sync

## Dependencies (1)

- [strip-ansi](https://npm.io/package/strip-ansi.md) ^5.0.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 4.2.0 (latest) — 2019-12-20
- 4.1.7 — 2019-05-30
- 4.1.6 — 2018-03-19
- 4.1.5 — 2017-05-04
- 4.1.4 — 2016-05-22
- 4.0.4 — 2016-03-03
- 4.0.3 — 2016-03-03
- 4.0.2 — 2016-03-03
- 4.0.1 — 2016-03-03
- 3.0.3 — 2016-03-03
- 4.0.0 — 2016-03-03
- 3.0.2 — 2016-02-25
- 3.0.1 — 2016-02-24
- 3.0.0 — 2015-09-01
- 2.1.0 — 2015-08-17
- … 2 more at https://npm.io/package/prompt-sync/versions

## README

# SYNOPSIS
A sync prompt for node. very simple. no C++ bindings and no bash scripts.

Works on Linux, OS X and Windows.

# BASIC MODE
```js

var prompt = require('prompt-sync')();
//
// get input from the user.
//
var n = prompt('How many more times? ');
```
# WITH HISTORY

History is an optional extra, to use simply install the history plugin. 

```sh
npm install --save prompt-sync-history
```

```js
var prompt = require('prompt-sync')({
  history: require('prompt-sync-history')() //open history file
});
//get some user input
var input = prompt()
prompt.history.save() //save history back to file
```

See the [prompt-sync-history](http://npm.im/prompt-sync-history) module
for options, or fork it for customized behaviour. 

# API

## `require('prompt-sync')(config) => prompt` 

Returns an instance of the `prompt` function.
Takes `config` option with the following possible properties

`sigint`: Default is `false`. A ^C may be pressed during the input process to abort the text entry. If sigint it `false`, prompt returns `null`. If sigint is `true` the ^C will be handled in the traditional way: as a SIGINT signal causing process to exit with code 130.

`eot`: Default is `false`. A ^D pressed as the first character of an input line causes prompt-sync to echo `exit` and exit the process with code 0.

`autocomplete`: A completer function that will be called when user enters TAB to allow for autocomplete. It takes a string as an argument an returns an array of strings that are possible matches for completion. An empty array is returned if there are no matches.

`history`: Takes an object that supplies a "history interface", see [prompt-sync-history](http://npm.im/prompt-sync-history) for an example.

## `prompt(ask, value, opts)`

`ask` is the label of the prompt, `value` is the default value
in absence of a response. 

The `opts` argument can also be in the first or second parameter position.

Opts can have the following properties

`echo`: Default is `'*'`. If set the password will be masked with the specified character. For hidden input, set echo to `''` (or use `prompt.hide`).

`autocomplete`: Overrides the instance `autocomplete` function to allow for custom 
autocompletion of a particular prompt.

`value`: Same as the `value` parameter, the default value for the prompt. If `opts`
is in the third position, this property will *not* overwrite the `value` parameter.

`ask`: Sames as the `value` parameter. The prompt label. If `opts` is not in the first position, the `ask` parameter will *not* be overridden by this property.

## `prompt.hide(ask)`

Convenience method for creating a standard hidden password prompt, 
this is the same as `prompt(ask, {echo: ''})`


# LINE EDITING
Line editing is enabled in the non-hidden mode. (use up/down arrows for history and backspace and left/right arrows for editing)

History is not set when using hidden mode.

# EXAMPLES

```js
  //basic:
  console.log(require('prompt-sync')()('tell me something about yourself: '))

  var prompt = require('prompt-sync')({
    history: require('prompt-sync-history')(),
    autocomplete: complete(['hello1234', 'he', 'hello', 'hello12', 'hello123456']),
    sigint: false
  });

  var value = 'frank';
  var name = prompt('enter name: ', value);
  console.log('enter echo * password');
  var pw = prompt({echo: '*'});
  var pwb = prompt('enter hidden password (or don\'t): ', {echo: '', value: '*pwb default*'})
  var pwc = prompt.hide('enter another hidden password: ')
  var autocompleteTest = prompt('custom autocomplete: ', {
    autocomplete: complete(['bye1234', 'by', 'bye12', 'bye123456'])
  });

  prompt.history.save();

  console.log('\nName: %s\nPassword *: %s\nHidden password: %s\nAnother Hidden password: %s', name, pw, pwb, pwc);
  console.log('autocomplete2: ', autocompleteTest);

  function complete(commands) {
    return function (str) {
      var i;
      var ret = [];
      for (i=0; i< commands.length; i++) {
        if (commands[i].indexOf(str) == 0)
          ret.push(commands[i]);
      }
      return ret;
    };
  };
```

---
_Source: https://npm.io/package/prompt-sync · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
