From e5b99160d759d0c94546296ed3f4e138e8c520f3 Mon Sep 17 00:00:00 2001 From: caub Date: Mon, 31 Jan 2022 22:48:55 +0100 Subject: [PATCH] add default memory history --- README.md | 23 +++++++++++++---------- index.js | 39 +++++++++++++++++++++++++++------------ test.js | 4 ++-- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2e72e71..9276abd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -114,5 +117,5 @@ History is not set when using hidden mode. } return ret; }; - }; -``` + } +```` diff --git a/index.js b/index.js index 076d9f8..a767582 100644 --- a/index.js +++ b/index.js @@ -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 { @@ -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; @@ -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') ? @@ -215,7 +230,7 @@ function create(config) { process.stdin.setRawMode && process.stdin.setRawMode(wasRaw); return str || value || ''; - }; + } function promptPrint(masked, ask, echo, str, insert) { diff --git a/test.js b/test.js index fc0df02..47a7430 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -//basic: +// basic: console.log(require('./')()('tell me something about yourself: ')) // ANSI escape codes colored text test @@ -35,4 +35,4 @@ function complete(commands) { } return ret; }; -}; +}