Skip to content

Commit

Permalink
Add history for CLI version, closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Mar 26, 2017
1 parent db69009 commit dbabacf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
44 changes: 24 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ if (process.argv.length >= 4) {
var interactive = process.stdin.isTTY;

if (interactive) {
var readline = require('readline');
var readline = require('historic-readline');
var xdgBasedir = require('xdg-basedir');
var path = require('path');

// Set up REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
path: path.join(xdgBasedir.config, "insect-history"),
prompt: '\x1b[01m>>>\x1b[0m ',
completer: function(line) {
var variables = Object.keys(insectEnv);
Expand All @@ -71,27 +74,28 @@ if (interactive) {
}

return [keywords, lastWord];
}
});

rl.prompt();

rl.on('line', function(line) {
var res = runInsect(Insect.fmtConsole, line);

if (res) {
if (res.msgType == "quit") {
},
next: function(rl) {
rl.prompt();

rl.on('line', function(line) {
var res = runInsect(Insect.fmtConsole, line);

if (res) {
if (res.msgType == "quit") {
process.exit(0);
} else if (res.msgType == "clear") {
process.stdout.write('\033[2J\033[0f');
} else {
console.log(res.msg + "\n");
}
}

rl.prompt();
}).on('close', function() {
process.exit(0);
} else if (res.msgType == "clear") {
process.stdout.write('\033[2J\033[0f');
} else {
console.log(res.msg + "\n");
}
});
}

rl.prompt();
}).on('close', function() {
process.exit(0);
});
} else {
// Read from non-interactive stream (shell pipe)
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "insect",
"version": "2.4.1",
"version": "2.5.0",
"description": "REPL-style scientific calculator",
"author": "David Peter <[email protected]>",
"license": "MIT",
Expand All @@ -24,6 +24,8 @@
},
"dependencies": {
"decimal.js": "^7.1.1",
"line-reader": "^0.4.0"
"historic-readline": "^1.0.8",
"line-reader": "^0.4.0",
"xdg-basedir": "^3.0.0"
}
}

0 comments on commit dbabacf

Please sign in to comment.