2022-06-12 14:22:15 +02:00
|
|
|
import type { ContentList } from "../content/types";
|
2021-12-16 00:21:14 +01:00
|
|
|
import { printSyntax, commandList } from "./definitions";
|
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
interface CommandInterfaceCallbacks {
|
2022-06-12 14:22:15 +02:00
|
|
|
setModalVisible?: CallableFunction;
|
|
|
|
setModalContent?: CallableFunction;
|
2021-12-16 00:21:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
export class CommandInterface {
|
2022-06-12 14:22:15 +02:00
|
|
|
callbacks?: CommandInterfaceCallbacks;
|
|
|
|
content: ContentList = [];
|
2021-12-16 00:21:14 +01:00
|
|
|
|
2022-06-12 14:22:15 +02:00
|
|
|
constructor(callbacks?: CommandInterfaceCallbacks, content?: ContentList) {
|
2022-01-14 14:27:19 +01:00
|
|
|
this.callbacks = callbacks;
|
2022-06-12 14:22:15 +02:00
|
|
|
this.content = content || [];
|
2022-01-14 14:27:19 +01:00
|
|
|
}
|
2021-12-16 00:21:14 +01:00
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
static commandCompletion(input: string): string[] {
|
|
|
|
if (input === "") return [];
|
2022-06-12 15:19:24 +02:00
|
|
|
const candidates = commandList.filter(cmd => cmd.name.startsWith(input)).map(cmd => cmd.name);
|
2022-01-14 14:27:19 +01:00
|
|
|
return candidates;
|
|
|
|
}
|
2021-12-16 00:21:14 +01:00
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
executeCommand(command: string): string[] {
|
|
|
|
if (!command) return [`$ ${command}`].concat(this.illegalCommand(command));
|
|
|
|
const args = command.split(" ");
|
|
|
|
const cmd = commandList.find(cmd => cmd.name === args[0]);
|
|
|
|
if (!cmd) return [`$ ${command}`].concat(this.illegalCommand(command));
|
|
|
|
|
|
|
|
const parsed = this.seperateFlags(args.splice(1));
|
|
|
|
const result = parsed.flags.includes("--help") ? printSyntax(cmd) : cmd.execute(parsed.flags, parsed.subcmds, command, this);
|
|
|
|
|
|
|
|
return [`$ ${command}`].concat(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private seperateFlags(args: string[]): {flags: string[], subcmds: string[]} {
|
|
|
|
const flags = args.filter(arg => arg.substring(0,1) === "-");
|
|
|
|
const subcmds = args.filter(arg => arg.substring(0,1) !== "-");
|
|
|
|
return {flags, subcmds};
|
|
|
|
}
|
2021-12-16 00:21:14 +01:00
|
|
|
|
2022-01-14 14:27:19 +01:00
|
|
|
private illegalCommand(command: string): string[] {
|
|
|
|
return [`Command '${command}' not found.`, "Type 'help' for help."];
|
|
|
|
}
|
2021-12-16 00:21:14 +01:00
|
|
|
}
|