41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
|
import type { CommandIO } from "@core/command_io";
|
|
import type { ResultSuccess } from "@core/result";
|
|
import type { ShellState } from "@core/shell_state";
|
|
import { SIGNAL_MAP } from "@core/signal";
|
|
import type { ShellError } from "@core/util/errors";
|
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
|
|
|
const COMMAND_NAME = "async";
|
|
|
|
async function main(io: CommandIO, state: ShellState, __args: string[]): Promise<CommandOutput<ShellError>> {
|
|
while (true) {
|
|
const out = await io.getSignal();
|
|
|
|
if (out.type === SIGNAL_MAP.INPUT) {
|
|
const encoded = new TextEncoder().encode(("user pressed: " + out.payload + "\n") as string);
|
|
io.writeStdout(encoded);
|
|
|
|
if (out.payload === "x") {
|
|
const result: ResultSuccess = {
|
|
type: "success",
|
|
exitCode: 0,
|
|
stdout: createImmutableBuffer(new Uint8Array()),
|
|
stderr: createImmutableBuffer(new Uint8Array()),
|
|
commandName: COMMAND_NAME,
|
|
};
|
|
|
|
return {
|
|
state: state,
|
|
result,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export const descriptor = {
|
|
fn: main,
|
|
name: COMMAND_NAME,
|
|
} satisfies CommandDescriptor;
|