chore: Fix formatting.

This commit is contained in:
greysoh 2024-05-07 11:44:18 -04:00
parent 964938e407
commit 3a69222ee5
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
2 changed files with 64 additions and 33 deletions

View file

@ -1,4 +1,4 @@
import { Command } from "commander";
import { Command, type ParseOptions } from "commander";
import { PrintLine } from "../commands";
export class SSHCommand extends Command {
@ -10,56 +10,73 @@ export class SSHCommand extends Command {
* @param println PrintLine function to use
* @param name Optional field for the name of the command
*/
constructor(println: PrintLine, name?: string, doNotUseThisParamater: boolean = false) {
constructor(
println: PrintLine,
name?: string,
disableSSHHelpPatching: boolean = false,
) {
super(name);
this.configureOutput({
writeOut: (str) => println(str),
writeErr: (str) => {
writeOut: str => println(str),
writeErr: str => {
if (str.includes("--help") || str.includes("-h")) return;
println(str);
}
},
});
if (!doNotUseThisParamater) {
if (!disableSSHHelpPatching) {
const sshCommand = new SSHCommand(println, "help", true);
sshCommand.description("Display help for command");
sshCommand.argument("[command]", "Command to show help for");
sshCommand.description("display help for command");
sshCommand.argument("[command]", "command to show help for");
sshCommand.action(() => {
this.hasRecievedExitSignal = true;
println("Aborted crash\n");
if (process.env.NODE_ENV != "production") {
println(
"Caught irrecoverable action (command help call) in patchCommander\n",
);
} else {
println("Aborted\n");
}
});
this.addCommand(sshCommand);
}
};
}
_exit() {
this.hasRecievedExitSignal = true;
};
}
_exitCallback() {
this.hasRecievedExitSignal = true;
};
}
action(fn: (...args: any[]) => void | Promise<void>): this {
super.action(fn);
// @ts-ignore
// prettier-ignore
const oldActionHandler: (...args: any[]) => void | Promise<void> = this._actionHandler;
// @ts-ignore
this._actionHandler = async(...args: any[]): Promise<void> => {
this._actionHandler = async (...args: any[]): Promise<void> => {
if (args[0][0] == "--help" || args[0][0] == "-h") return;
await oldActionHandler(...args);
};
return this;
}
parse(argv?: readonly string[], options?: ParseOptions): this {
super.parse(["nextruntime", ...(argv ?? [])], options);
return this;
}
createCommand(name: string) {
const command = new SSHCommand(this.println, name);
return command;
};
};
}
}

View file

@ -1,13 +1,16 @@
import type { ServerChannel } from "ssh2";
export async function readFromKeyboard(stream: ServerChannel, disableEcho: boolean = false): Promise<string> {
export async function readFromKeyboard(
stream: ServerChannel,
disableEcho: boolean = false,
): Promise<string> {
const leftEscape = "\x1B[D";
const rightEscape = "\x1B[C";
const ourBackspace = "\u0008";
// \x7F = Ascii escape code for backspace (client side)
let line = "";
let lineIndex = 0;
let isReady = false;
@ -27,7 +30,7 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole
if (!disableEcho) {
const deltaCursor = line.length - lineIndex;
if (deltaCursor == line.length) return setTimeout(eventLoop, 5);
if (deltaCursor < 0) {
@ -38,12 +41,16 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole
stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace);
// Go backwards & rerender text & go backwards again (wtf?)
stream.write(leftEscape.repeat(deltaCursor + 1) + line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor + 1));
stream.write(
leftEscape.repeat(deltaCursor + 1) +
line.substring(lineIndex - 1) +
leftEscape.repeat(deltaCursor + 1),
);
}
lineIndex -= 1;
}
} else if (readStreamData.includes("\x1B")) {
} else if (readStreamData.includes("\x1B")) {
if (readStreamData.includes(rightEscape)) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5);
lineIndex += 1;
@ -59,32 +66,39 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole
lineIndex += readStreamData.length;
// There isn't a splice method for String prototypes. So, ugh:
line = line.substring(0, lineIndex - 1) + readStreamData + line.substring(lineIndex - 1);
line =
line.substring(0, lineIndex - 1) +
readStreamData +
line.substring(lineIndex - 1);
if (!disableEcho) {
let deltaCursor = line.length - lineIndex;
// wtf?
if (deltaCursor < 0) {
console.log("FIXME: somehow, our deltaCursor value is negative! please investigate me");
console.log(
"FIXME: somehow, our deltaCursor value is negative! please investigate me",
);
deltaCursor = 0;
}
stream.write(line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor));
stream.write(
line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor),
);
}
}
setTimeout(eventLoop, 5);
}
// Yes, this is bad practice. Currently, I don't care.
return new Promise(async(resolve) => {
return new Promise(async resolve => {
eventLoop();
while (!isReady) {
await new Promise((i) => setTimeout(i, 5));
await new Promise(i => setTimeout(i, 5));
}
resolve(line);
});
};
}