Skip to content

Commit

Permalink
Various improvements to terminal CLI
Browse files Browse the repository at this point in the history
- Allow passing expression as multiple arguments
  This allows e.g. `insect 1 + 1`, whereas previously this would've been
  considered an error.
- Use insectrc in non-interactive mode too.
  Fixes sharkdp#377.
- Add alternative location for insectrc ($XDG_CONFIG_HOME/insect/
  insectrc). Fixes sharkdp#307.
- Exit with non-zero exit code if evaluating argument fails.
  • Loading branch information
triallax committed Jul 7, 2023
1 parent a935f00 commit 51d7050
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions index.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as Insect from "./output/Insect/index.js";
import * as path from "path";
import * as xdgBasedir from "xdg-basedir";

var insectEnv = Insect.initialEnvironment;

Expand All @@ -25,56 +26,59 @@ function runInsect(fmt, line) {
return res;
}

// Handle command line arguments
if (process.argv.length >= 4) {
usage();
} else if (process.argv.length == 3) {
var arg = process.argv[2];
if (arg === "-h" || arg === "--help") {
// Top-level await is not supported in Node 12 and earlier.
(async function() {
if (process.env.INSECT_NO_RC !== "true") {
function processRcLine(line) {
var res = runInsect(Insect.fmtPlain, line);
// We really only care when it breaks
if (res && res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
}

var [util, lineReader] = await Promise.all([import("util"), import("line-reader")]);
var eachLine = util.promisify(lineReader.eachLine);

try {
await eachLine(path.join(xdgBasedir.config, "insect/insectrc"), processRcLine);
} catch (err) {
if (err.code === "ENOENT") {
try {
var os = await import("os");
await eachLine(path.join(os.homedir(), ".insectrc"), processRcLine);
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
} else {
throw err;
}
}
}

// Handle command line arguments
var args = process.argv.slice(2);
if (args[0] === "-h" || args[0] === "--help") {
usage();
} else {
} else if (args.length !== 0) {
// Execute a single command
var res = runInsect(Insect.fmtPlain, arg);
var res = runInsect(Insect.fmtPlain, args.join(" "));
if (res.msgType === "value" || res.msgType === "info") {
console.log(res.msg);
} else if (res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
process.exit(0);
}
}

if (process.env.INSECT_NO_RC !== "true") {
// Top-level await is not supported in Node 12 and earlier.
(async function() {
var [os, lineReader] = await Promise.all([import("os"), import("line-reader")]);

var rcFile = path.join(os.homedir(), ".insectrc");
lineReader.eachLine(rcFile, function (line) {
var res = runInsect(Insect.fmtPlain, line);
// We really only care when it breaks
if (res && 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();
}

async function startInsect() {
var interactive = process.stdin.isTTY;

if (interactive) {
var [fs, clipboardy, readline, xdgBasedir] = await Promise.all([import("fs"), import("clipboardy"), import("readline"), import("xdg-basedir")]);
var [fs, clipboardy, readline] = await Promise.all([import("fs"), import("clipboardy"), import("readline")]);

// Create `xdgBasedir.data` if it doesn't already exist.
// See https://github.com/sharkdp/insect/issues/364.
Expand Down Expand Up @@ -192,4 +196,4 @@ async function startInsect() {
}
});
}
}
}());

0 comments on commit 51d7050

Please sign in to comment.