Skip to content

Instantly share code, notes, and snippets.

@pomdtr
Created July 31, 2021 10:41
Show Gist options
  • Save pomdtr/6d389a4490fd53b6eeaf4ac1e74e820e to your computer and use it in GitHub Desktop.
Save pomdtr/6d389a4490fd53b6eeaf4ac1e74e820e to your computer and use it in GitHub Desktop.
// Menu: Pipe Clipboard to Command
// Decription: Manipulate clipboard content using the command line
// Author: pomdtr
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}`;
}
// Persist clipoard inside a file
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."
},
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment