Command framework
This commit is contained in:
85
lib/commands/definitions.tsx
Normal file
85
lib/commands/definitions.tsx
Normal file
@ -0,0 +1,85 @@
|
||||
import type { Command } from "./types";
|
||||
|
||||
function illegalUse(raw: string, cmd: Command): string[] {
|
||||
return [
|
||||
"Syntax error!",
|
||||
`Cannot parse "${raw}"`,
|
||||
""
|
||||
].concat(printSyntax(cmd));
|
||||
}
|
||||
|
||||
function checkFlags(flags: string[], cmd: Command): boolean {
|
||||
if (!flags || flags === []) return true;
|
||||
if (!cmd.flags) return false;
|
||||
|
||||
for (const flag of flags) {
|
||||
const isLong = flag.substring(0,2) === "--";
|
||||
const flagObj = cmd.flags.find(f => isLong ? f.long === flag.substring(2) : f.short === flag.substring(1));
|
||||
if (!flagObj) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function checkSubcmd(subcmds: string[], cmd: Command): boolean {
|
||||
if (!subcmds || subcmds === []) return true;
|
||||
if (!cmd.subcommands) return false;
|
||||
|
||||
for (const sc of subcmds) {
|
||||
const flagObj = cmd.subcommands.find(s => s.name === sc);
|
||||
if (!flagObj) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function printSyntax(cmd: Command): string[] {
|
||||
let flagsOption = "";
|
||||
let flagsDesc = [];
|
||||
if (cmd.flags && cmd.flags.length > 0) {
|
||||
flagsOption = " [";
|
||||
flagsDesc.push("Flags:");
|
||||
cmd.flags.forEach((flag => {
|
||||
flagsOption += `-${flag.short} `;
|
||||
flagsDesc.push(`\t-${flag.short}\t--${flag.long}\t${flag.desc}`);
|
||||
}));
|
||||
flagsOption = flagsOption.substring(0, flagsOption.length-1) + "]";
|
||||
flagsDesc.push("");
|
||||
}
|
||||
|
||||
let subcmdOption = "";
|
||||
let subcmdDesc = [];
|
||||
if (cmd.subcommands && cmd.subcommands.length > 0) {
|
||||
subcmdOption = " [";
|
||||
subcmdDesc.push("Subcommands:");
|
||||
cmd.subcommands.forEach((subcmd => {
|
||||
subcmdOption += `${subcmd.name}|`;
|
||||
subcmdDesc.push(`\t${subcmd.name}\t${subcmd.desc}`);
|
||||
}));
|
||||
subcmdOption = subcmdOption.substring(0, subcmdOption.length-1) + "]";
|
||||
subcmdDesc.push("");
|
||||
}
|
||||
|
||||
return [`Usage: ${cmd.name}${flagsOption}${subcmdOption}`, ""].concat(flagsDesc).concat(subcmdDesc);
|
||||
}
|
||||
|
||||
const about: Command = {
|
||||
name: "about",
|
||||
desc: "Show information about this page.",
|
||||
execute: (_flags, _args, _raw) => {
|
||||
return [
|
||||
"Hello there wanderer.",
|
||||
"So you want to know what this is about?",
|
||||
"",
|
||||
"Well, the answer is pretty unspectecular:",
|
||||
"This site presents some stuff that the user named C0ntroller created.",
|
||||
"If you wander arround you will find various projects.",
|
||||
"",
|
||||
"The navigation is done via this console interface.",
|
||||
"Even when you open a project page you don't need your mouse - just press Esc to close it.",
|
||||
"",
|
||||
"I hope you enjoy your stay here!",
|
||||
"If you wanted more information about the page itself, type 'project this'."
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
export const commandList = [about];
|
31
lib/commands/index.ts
Normal file
31
lib/commands/index.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { printSyntax, commandList } from "./definitions";
|
||||
|
||||
//const commandList = ["about", "navigate", "external", "help", "ed", "nano"];
|
||||
|
||||
export function commandCompletion(input: string): string[] {
|
||||
if (input === "") return [];
|
||||
const candidates = commandList.filter(cmd => cmd.name.substring(0, input.length) === input).map(cmd => cmd.name);
|
||||
return candidates;
|
||||
}
|
||||
|
||||
export function executeCommand(command: string): string[] {
|
||||
if (!command) return [`$ ${command}`].concat(illegalCommand(command));
|
||||
const args = command.split(" ");
|
||||
const cmd = commandList.find(cmd => cmd.name === args[0]);
|
||||
if (!cmd) return [`$ ${command}`].concat(illegalCommand(command));
|
||||
|
||||
const parsed = seperateFlags(args.splice(1));
|
||||
const result = parsed.flags.includes("--help") ? printSyntax(cmd) : cmd.execute(parsed.flags, parsed.subcmds, command);
|
||||
|
||||
return [`$ ${command}`].concat(result);
|
||||
}
|
||||
|
||||
function 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};
|
||||
}
|
||||
|
||||
function illegalCommand(command: string): string[] {
|
||||
return [`Command '${command}' not found.`, "Type 'help' for help."];
|
||||
}
|
19
lib/commands/types.tsx
Normal file
19
lib/commands/types.tsx
Normal file
@ -0,0 +1,19 @@
|
||||
interface Flag {
|
||||
short: string;
|
||||
long: string;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
interface SubCommand {
|
||||
name: string;
|
||||
desc: string;
|
||||
}
|
||||
|
||||
export interface Command {
|
||||
name: string;
|
||||
hidden?: boolean;
|
||||
desc: string;
|
||||
flags?: Flag[];
|
||||
subcommands?: SubCommand[];
|
||||
execute: (flags: string[], args: string[], raw: string) => string[];
|
||||
}
|
Reference in New Issue
Block a user