feat: add support for a session execution duration

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-10-26 22:47:42 -07:00
parent b8dd7edfdd
commit 07ffb6793a
5 changed files with 25 additions and 9 deletions
+18 -4
View File
@@ -2,7 +2,7 @@
// Licensed under the MIT License.
import { render } from "../ui/ui-root.js";
import { executeShellCommandTTY } from "../runtime/utils.js";
import { executeShellCommandTTY, ExecuteShellCommandTTYResult } from "../runtime/utils.js";
import { saveCommand, loadCommand } from "../utils/cache.js";
import { supportedShells as shells } from "../utils/bindings.js";
@@ -12,6 +12,7 @@ type RootCommandOptions = {
shell: string | undefined;
command: string | undefined;
history: boolean | undefined;
duration: string | undefined;
};
export const action = async (options: RootCommandOptions) => {
@@ -26,10 +27,23 @@ export const action = async (options: RootCommandOptions) => {
process.exit(1);
}
const commandToExecute = await render(options.command);
await saveCommand(commandToExecute);
let executed = false;
const commands = [];
let result: ExecuteShellCommandTTYResult = { code: 0 };
while (options.duration === "session" || !executed) {
const commandToExecute = await render(options.command);
if (commandToExecute == null) {
result = { code: 0 };
break;
}
commands.push(commandToExecute);
result = await executeShellCommandTTY(shell, commandToExecute);
executed = true;
}
await saveCommand(commands);
const result = await executeShellCommandTTY(shell, commandToExecute);
if (result.code) {
process.exit(result.code);
} else {
+1
View File
@@ -20,6 +20,7 @@ program
.option("-s, --shell <shell>", `shell to use for command execution, supported shells: ${supportedShells}`)
.option("-c, --command <commmand>", "command to use as initial input")
.option("--history", "get the last command execute")
.option("-d, --duration <duration>", "duration of the autocomplete session, supported durations: single, session", "session")
.action(action);
program.addCommand(bind);
+1 -1
View File
@@ -3,7 +3,7 @@
import { exec, spawn } from "node:child_process";
type ExecuteShellCommandTTYResult = {
export type ExecuteShellCommandTTYResult = {
code: number | null;
};
+3 -2
View File
@@ -11,7 +11,7 @@ import Suggestions from "./suggestions.js";
import Input from "./input.js";
const Prompt = "> ";
let uiResult = "";
let uiResult = undefined;
function UI({ startingCommand }: { startingCommand: string }) {
const { exit } = useApp();
@@ -71,7 +71,8 @@ function UI({ startingCommand }: { startingCommand: string }) {
);
}
export const render = async (command: string | undefined) => {
export const render = async (command: string | undefined): Promise<string | undefined> => {
uiResult = undefined;
const { waitUntilExit } = inkRender(<UI startingCommand={command ?? ""} />);
await waitUntilExit();
+2 -2
View File
@@ -10,11 +10,11 @@ const cacheFolder = ".inshellisense";
const folderPath = path.join(os.homedir(), cacheFolder);
const cachePath = path.join(os.homedir(), cacheFolder, "inshellisense.cache");
export const saveCommand = async (command: string) => {
export const saveCommand = async (command: string[]) => {
if (!fs.existsSync(folderPath)) {
await fsAsync.mkdir(folderPath);
}
await fsAsync.writeFile(cachePath, command);
await fsAsync.writeFile(cachePath, command.map((c, idx) => `${idx.toString().padStart(5)} ${c}`).join("\n"));
};
export const loadCommand = async (): Promise<string> => {