process.env.PATH = `${process.env.HOME}/.local/bin:/usr/local/bin/:${process.env.PATH}`;
function codeblock(text) {
const triple_backquote = "```";
return `${triple_backquote}shell\n${text}\n${triple_backquote}`;
}
const clipboardContent = await paste();
const tempfile = `${kit.tempdir()}/input.txt`;
await kit.writeFile(tempfile, clipboardContent, {
flag: "w",
encoding: "utf-8",
});
let command = "";
let [stdout, stderr, code] = [null, null, 0];
let panelContent = await kit.readFile(tempfile, { encoding: "utf-8" });
while (true) {
command = await arg(
{
placeholder: "Input Command:",
className: "p-2",
input: command,
hint: "Hit enter to run command and save output to clipboard, esc to dismiss window."
},
md(codeblock(panelContent))
);
if (command) {
({ stdout, stderr, code } = exec(`cat ${tempfile} | ${command}`));
panelContent = code == 0 ? stdout : stderr;
if (code == 0) {
panelContent = stdout;
} else {
panelContent = stderr;
}
} else {
panelContent = await kit.readFile(tempfile, { encoding: "utf-8" });
}
await copy(panelContent);
}