diff --git a/frontend/src/ts/commandline/index.ts b/frontend/src/ts/commandline/index.ts index 0bc831393..963422ef2 100644 --- a/frontend/src/ts/commandline/index.ts +++ b/frontend/src/ts/commandline/index.ts @@ -327,85 +327,66 @@ function trigger(command: string): void { } } -//todo rewrite this mess -function addChildCommands( - unifiedCommands: MonkeyTypes.Command[], - commandItem: MonkeyTypes.Command | MonkeyTypes.CommandsSubgroup, - parentCommandDisplay = "", - parentCommand?: MonkeyTypes.CommandsSubgroup -): void { - let commandItemDisplay = (commandItem as MonkeyTypes.Command).display; - - if ((commandItem as MonkeyTypes.Command)?.input !== true) { - commandItemDisplay = (commandItem as MonkeyTypes.Command).display.replace( - /\s?\.\.\.$/g, - "" - ); - } - - let icon = ``; - if ( - (commandItem as MonkeyTypes.Command).configValue !== undefined && - Config[parentCommand?.configKey as keyof MonkeyTypes.Config] === - (commandItem as MonkeyTypes.Command).configValue - ) { - icon = ``; - } - if ( - (commandItem as MonkeyTypes.Command).noIcon || - (commandItem as MonkeyTypes.Command).input === true - ) { - icon = ""; - } - - if (parentCommandDisplay) { - commandItemDisplay = - parentCommandDisplay + " > " + icon + commandItemDisplay; - } - if ((commandItem as MonkeyTypes.Command).subgroup) { - const command = commandItem as MonkeyTypes.Command; - if (command.subgroup?.beforeList) command.subgroup.beforeList(); - try { - ( - (commandItem as MonkeyTypes.Command) - ?.subgroup as MonkeyTypes.CommandsSubgroup - ).list?.forEach((cmd) => { - (commandItem as MonkeyTypes.CommandsSubgroup).configKey = ( - (commandItem as MonkeyTypes.Command) - .subgroup as MonkeyTypes.CommandsSubgroup - ).configKey; - addChildCommands( - unifiedCommands, - cmd, - commandItemDisplay, - commandItem as MonkeyTypes.CommandsSubgroup - ); - }); - // commandItem.exec(); - // const currentCommandsIndex = CommandlineLists.current.length - 1; - // CommandlineLists.current[currentCommandsIndex].list.forEach((cmd) => { - // if (cmd.alias === undefined) cmd.alias = commandItem.alias; - // addChildCommands(unifiedCommands, cmd, commandItemDisplay); - // }); - // CommandlineLists.current.pop(); - } catch (e) {} - } else { - const tempCommandItem: MonkeyTypes.Command = { - ...(commandItem as MonkeyTypes.Command), +function getCommands( + command: MonkeyTypes.Command, + parentCommand?: MonkeyTypes.Command +): MonkeyTypes.Command[] { + const ret: MonkeyTypes.Command[] = []; + if (command.subgroup) { + const currentCommand = { + ...command, + subgroup: { + ...command.subgroup, + list: [], + }, }; + for (const cmd of command.subgroup.list) { + ret.push(...getCommands(cmd, currentCommand)); + } + } else { if (parentCommand) { - (tempCommandItem as MonkeyTypes.Command).icon = ( - parentCommand as unknown as MonkeyTypes.Command - ).icon; + const parentCommandDisplay = parentCommand.display.replace( + /\s?\.\.\.$/g, + "" + ); + let configIcon = ""; + const parentKey = parentCommand.subgroup?.configKey; + const currentValue = command.configValue; + if (parentKey !== undefined && currentValue !== undefined) { + if ( + (command.configValueMode === "include" && + (Config[parentKey] as unknown[]).includes(currentValue)) || + Config[parentKey] === currentValue + ) { + configIcon = ``; + } else { + configIcon = ``; + } + } + const displayString = + parentCommandDisplay + + " > " + + (command.noIcon ? "" : configIcon) + + command.display; + const newCommand = { + ...command, + display: displayString, + icon: parentCommand.icon, + alias: (parentCommand.alias ?? "") + " " + (command.alias ?? ""), + visible: (parentCommand.visible ?? true) && (command.visible ?? true), + available: (): boolean => { + return ( + (parentCommand?.available?.() ?? true) && + (command?.available?.() ?? true) + ); + }, + }; + ret.push(newCommand); + } else { + ret.push(command); } - if (parentCommandDisplay) tempCommandItem.display = commandItemDisplay; - //@ts-ignore - if (parentCommand?.available) { - //@ts-ignore - tempCommandItem.available = parentCommand.available; - } - unifiedCommands.push(tempCommandItem); } + return ret; } function generateSingleListOfCommands(): { @@ -413,9 +394,10 @@ function generateSingleListOfCommands(): { list: MonkeyTypes.Command[]; } { const allCommands: MonkeyTypes.Command[] = []; - CommandlineLists.commands.list.forEach((c) => - addChildCommands(allCommands, c) - ); + for (const command of CommandlineLists.commands.list) { + allCommands.push(...getCommands(command)); + } + return { title: "All Commands", list: allCommands,