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

Implement insectrc (closes #107) #248

Merged
merged 1 commit into from
Feb 13, 2021
Merged
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
178 changes: 104 additions & 74 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

var os = require("os");
var path = require("path");
var Insect = require('./output/Insect/index.js');

var insectEnv = Insect.initialEnvironment;
Expand Down Expand Up @@ -43,90 +45,118 @@ if (process.argv.length >= 4) {
}
}

var interactive = process.stdin.isTTY;

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

// Set up REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
path: path.join(xdgBasedir.data, "insect-history"),
completer: function(line) {
var identifiers = Insect.identifiers(insectEnv);

var keywords =
identifiers.concat(Insect.functions(insectEnv))
.concat(Insect.supportedUnits)
.concat(Insect.commands);

var lastWord = line;
if (line.trim() !== "") {
var words = line.split(/\b/);
lastWord = words[words.length - 1];
keywords= keywords.filter(function(kw) {
return kw.indexOf(lastWord) === 0;
});
if (process.env.INSECT_NO_RC !== "true") {
var lineReader = require("line-reader");
var rcFile = path.join(os.homedir(), ".insectrc");
lineReader.eachLine(rcFile, function (line) {
var res = runInsect(Insect.fmtPlain, line);
if (res) {
// We really only care when it breaks
if (res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
}
}, function (err) {
// If the file doesn't exist, that's fine
if (err && err.code !== "ENOENT") {
throw err;
} else {
startInsect();
}
});
} else {
startInsect();
}

return [keywords, lastWord];
},
next: function(rl) {
var prompt = '\x1b[01m>>>\x1b[0m ';
function startInsect() {
var interactive = process.stdin.isTTY;

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

// Set up REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
path: path.join(xdgBasedir.data, "insect-history"),
completer: function(line) {
var identifiers = Insect.identifiers(insectEnv);

var keywords =
identifiers.concat(Insect.functions(insectEnv))
.concat(Insect.supportedUnits)
.concat(Insect.commands);

var lastWord = line;
if (line.trim() !== "") {
var words = line.split(/\b/);
lastWord = words[words.length - 1];
keywords= keywords.filter(function(kw) {
return kw.indexOf(lastWord) === 0;
});
}

// The visual length of the prompt (4) needs to be set explicitly for
// older versions of node:
rl.setPrompt(prompt, 4);
return [keywords, lastWord];
},
next: function(rl) {
var prompt = '\x1b[01m>>>\x1b[0m ';

rl.prompt();
// The visual length of the prompt (4) needs to be set explicitly for
// older versions of node:
rl.setPrompt(prompt, 4);

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

if (res) {
if (res.msgType == "quit") {
process.exit(0);
} else if (res.msgType == "clear") {
process.stdout.write('\x1Bc');
} else if (res.msgType == "copy") {
if (res.msg == "") {
console.log("\nNo result to copy.\n");
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('\x1Bc');
} else if (res.msgType == "copy") {
if (res.msg == "") {
console.log("\nNo result to copy.\n");
} else {
clipboardy.writeSync(res.msg);
console.log("\nCopied result '" + res.msg + "' to clipboard.\n");
}
} else {
clipboardy.writeSync(res.msg);
console.log("\nCopied result '" + res.msg + "' to clipboard.\n");
console.log(res.msg + "\n");
}
} else {
console.log(res.msg + "\n");
}
}

rl.prompt();
}).on('close', function() {
process.exit(0);
});
}
});
} else {
// Read from non-interactive stream (shell pipe)

var lineReader = require("line-reader");
lineReader.eachLine(process.stdin, function(line) {
var res = runInsect(Insect.fmtPlain, line);
if (res) {
// Only output values and halt on errors. Ignore 'info' and 'value-set'
// message types.
if (res.msgType === "value") {
console.log(res.msg);
} else if (res.msgType == "error") {
console.error(res.msg);
process.exit(1);
} else if (res.msgType == "quit") {
process.exit(0);
rl.prompt();
}).on('close', function() {
process.exit(0);
});
}
});
} else {
// Read from non-interactive stream (shell pipe)

if (typeof lineReader === "undefined") {
var lineReader = require("line-reader");
}
});
lineReader.eachLine(process.stdin, function(line) {
var res = runInsect(Insect.fmtPlain, line);
if (res) {
// Only output values and halt on errors. Ignore 'info' and 'value-set'
// message types.
if (res.msgType === "value") {
console.log(res.msg);
} else if (res.msgType == "error") {
console.error(res.msg);
process.exit(1);
} else if (res.msgType == "quit") {
process.exit(0);
}
}
});
}
}