From f2a837db735a57b38b223afb93002b819a414ac6 Mon Sep 17 00:00:00 2001 From: Mary Strodl Date: Tue, 9 Feb 2021 09:35:17 -0500 Subject: [PATCH] Implement insectrc (closes #107) --- index.js | 178 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 74 deletions(-) diff --git a/index.js b/index.js index be5ca28..d25272b 100755 --- a/index.js +++ b/index.js @@ -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; @@ -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); + } + } + }); + } }