Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Enter on Single Character #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm-debug.log
*sw.*
node_modules
.prompt_hist.txt
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var n = prompt('How many more times? ');
```
# WITH HISTORY

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

```sh
npm install --save prompt-sync-history
Expand All @@ -30,11 +30,11 @@ 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.
for options, or fork it for customized behaviour.

# API

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

Returns an instance of the `prompt` function.
Takes `config` option with the following possible properties
Expand All @@ -47,18 +47,20 @@ Takes `config` option with the following possible properties

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

`submitOnCharacter`: Entering any single character results in submission (auto enter). Supports a use case like 'press **w** to see more options'.

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

`ask` is the label of the prompt, `value` is the default value
in absence of a response.
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
`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`
Expand All @@ -68,7 +70,7 @@ is in the third position, this property will *not* overwrite the `value` paramet

## `prompt.hide(ask)`

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


Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var term = 13; // carriage return
* @returns {Function} prompt function
*/

// for ANSI escape codes reference see https://en.wikipedia.org/wiki/ANSI_escape_code
// for ANSI escape codes reference see https://en.wikipedia.org/wiki/ANSI_escape_code

function create(config) {

Expand All @@ -24,6 +24,7 @@ function create(config) {
var autocomplete = config.autocomplete =
config.autocomplete || function(){return []};
var history = config.history;
var submitOnCharacter = config.submitOnCharacter || false;
prompt.history = history || {save: function(){}};
prompt.hide = function (ask) { return prompt(ask, {echo: ''}) };

Expand Down Expand Up @@ -201,11 +202,20 @@ function create(config) {
process.stdout.write('\u001b[2D');
} else {
if ((character < 32 ) || (character > 126))
continue;
continue;
str = str.slice(0, insert) + String.fromCharCode(character) + str.slice(insert);
insert++;
};

// user wants to submit on single character entry
if (submitOnCharacter) {
fs.closeSync(fd);
if (!history) break;
if (!masked && str.length) history.push(str);
history.reset();
break;
}

promptPrint(masked, ask, echo, str, insert);

}
Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ var autocompleteTest = prompt('custom autocomplete: ', {

prompt.history.save();

console.log('\nName: %s\nPassword *: %s\nHidden password: %s\nAnother Hidden password: %s', name, pw, pwb, pwc);
var prompt2 = require('./')({
sigint: false,
submitOnCharacter: true
});

var submitOnCharacter = prompt2('enter single character: ', { submitOnCharacter: true });

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

function complete(commands) {
Expand Down