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

add default memory history #56

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
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ var prompt = require('prompt-sync')();
//
var n = prompt('How many more times? ');
```
# WITH HISTORY
# ADVANCED HISTORY

History is an optional extra, to use simply install the history plugin.
History is configurable, for example using this extra package:

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

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

See the [prompt-sync-history](http://npm.im/prompt-sync-history) module
Expand Down Expand Up @@ -80,10 +80,13 @@ History is not set when using hidden mode.
# EXAMPLES

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

var prompt = require('prompt-sync')({
// ANSI escape codes colored text test
require('./')()('\u001B[31mcolored text: \u001B[39m');

var prompt = require('./')({
history: require('prompt-sync-history')(),
autocomplete: complete(['hello1234', 'he', 'hello', 'hello12', 'hello123456']),
sigint: false
Expand Down Expand Up @@ -114,5 +117,5 @@ History is not set when using hidden mode.
}
return ret;
};
};
```
}
````
39 changes: 27 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ var fs = require('fs');
var stripAnsi = require('strip-ansi');
var term = 13; // carriage return

function createMemoryHistory() {
var HIST = [];
var ix = HIST.length;
return {
atStart() { return ix <= 0; },
atPenultimate() { return ix === HIST.length - 1; },
pastEnd() { return ix >= HIST.length; },
atEnd() { return ix === HIST.length; },
prev() { return HIST[--ix]; },
next() { return HIST[++ix]; },
reset() { ix = HIST.length; },
push(str) {
if (HIST.includes(str)) return;
HIST.push(str);
},
save() { },
};
}

/**
* create -- sync function for reading user input from stdin
* @param {Object} config {
Expand All @@ -16,15 +35,12 @@ var term = 13; // carriage return

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

function create(config) {

config = config || {};
function create(config = {}) {
var sigint = config.sigint;
var eot = config.eot;
var autocomplete = config.autocomplete =
config.autocomplete || function(){return []};
var history = config.history;
prompt.history = history || {save: function(){}};
var history = prompt.history = config.history || createMemoryHistory();
prompt.hide = function (ask) { return prompt(ask, {echo: ''}) };

return prompt;
Expand All @@ -46,20 +62,19 @@ function create(config) {
*/


function prompt(ask, value, opts) {
function prompt(ask = '', value, opts = {}) {
var insert = 0, savedinsert = 0, res, i, savedstr;
opts = opts || {};

if (Object(ask) === ask) {
opts = ask;
ask = opts.ask;
ask = opts.ask || '';
} else if (Object(value) === value) {
opts = value;
value = opts.value;
}
ask = ask || '';
}

var echo = opts.echo;
var masked = 'echo' in opts;
var masked = opts.echo !== undefined;
autocomplete = opts.autocomplete || autocomplete;

var fd = (process.platform === 'win32') ?
Expand Down Expand Up @@ -215,7 +230,7 @@ function create(config) {
process.stdin.setRawMode && process.stdin.setRawMode(wasRaw);

return str || value || '';
};
}


function promptPrint(masked, ask, echo, str, insert) {
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//basic:
// basic:
console.log(require('./')()('tell me something about yourself: '))

// ANSI escape codes colored text test
Expand Down Expand Up @@ -35,4 +35,4 @@ function complete(commands) {
}
return ret;
};
};
}