From e2c9cee54fd9b9b2420265a45772a8216419cba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=B1=89=E6=88=90?= <308114274@qq.com> Date: Fri, 23 Oct 2015 09:10:34 +0800 Subject: [PATCH 1/2] fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove ‘.history’ instead of the earliest record from history array when trigger ‘line’ event. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index cea2c5b..d44a252 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ module.exports = function (repl, file) { fs.write(fd, code + '\n'); } else { repl.rli.historyIndex++; - repl.rli.history.pop(); + repl.rli.history.shift(); } }); From b65112f66bdef781f8080db146196884fd93dd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=B1=89=E6=88=90?= <308114274@qq.com> Date: Fri, 23 Oct 2015 09:26:41 +0800 Subject: [PATCH 2/2] Add 'clearhistory' command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit run ‘.clearhistory’ to clear repl's history. --- README.md | 6 +++++- index.js | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c018980..552bee5 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,8 @@ run `repl.history` on the command line A file `~/.node_history` will be created. -I like to alias it to `nr` for node repl \ No newline at end of file +I like to alias it to `nr` for node repl + +run `.history` to see repl's history. + +run `.clearhistory` to clear repl's history. \ No newline at end of file diff --git a/index.js b/index.js index d44a252..a6de6e7 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ module.exports = function (repl, file) { var fd = fs.openSync(file, 'a'), reval = repl.eval; repl.rli.addListener('line', function(code) { - if (code && code !== '.history') { + if (code && code !== '.history' && code !==".clearhistory") { fs.write(fd, code + '\n'); } else { repl.rli.historyIndex++; @@ -34,4 +34,18 @@ module.exports = function (repl, file) { repl.displayPrompt(); } }; + + repl.commands['clearhistory'] = { + help : 'Clear the history', + action : function() { + var fd = fs.openSync(file, 'w'); + fs.write(fd,"",function(){ + while(repl.rli.history.length > 0){ + repl.rli.history.pop(); + }; + + repl.displayPrompt(); + }); + } + }; };