diff options
Autocommit at 28.05.2022 @ 20:25
2 files changed, 16 insertions, 9 deletions
| -rw-r--r-- | src/Generic/ConsoleInterface/Command.h | 8 | ||||
| -rw-r--r-- | src/Generic/ConsoleInterface/Menu.cpp | 17 |
diff --git a/src/Generic/ConsoleInterface/Command.h b/src/Generic/ConsoleInterface/Command.h index f1eb55b..8e40955 100644 --- a/src/Generic/ConsoleInterface/Command.h +++ b/src/Generic/ConsoleInterface/Command.h @@ -10,18 +10,18 @@ * Menu uses this class for a more generic implementation of it's navigate function. */ class Command { - char nameInMenu[256]; - void (*exec)(); + char name[256]; + void (*exec)(const char* params); public: //! Leaves name empty and exec function pointer to nullptr Command(); //! Copies contents of nameInMenu and stores exec - Command(const char* nameInMenu, void (*exec)()); + Command(const char* name, void (*exec)(const char* params)); //! Executes the stored function pointer void run() const; - const char* get_nameInMenu() const; + const char* get_name() const; }; #endif diff --git a/src/Generic/ConsoleInterface/Menu.cpp b/src/Generic/ConsoleInterface/Menu.cpp index 16032eb..69bb7b9 100644 --- a/src/Generic/ConsoleInterface/Menu.cpp +++ b/src/Generic/ConsoleInterface/Menu.cpp @@ -4,6 +4,13 @@ Menu::Menu() : menuOptions() {} +unsigned indexOfCommandEnd(char*& str) { + while (*str == ' ') str++; + unsigned ind = 0; + while (str[ind] != ' ') ind++; + return ind; +} + /*! * Prints the title, then an Error/Warning/Sucess message (if set), * prints all command names as an ordered list (starting from 1), @@ -28,20 +35,20 @@ void Menu::navigate() const { clear(); inputLineBox("> ", buffer, MAX_LINE_WIDTH); - unsigned index = menuOptions.get_count(); + unsigned commandEnd = indexOfCommandEnd(buffer), + index = menuOptions.get_count(); for (unsigned i = 0; i < menuOptions.get_count(); i++) { - if (strncmp(buffer, menuOptions[i].get_nameInMenu(), MAX_LINE_WIDTH) == 0) { + if (strncmp(buffer, menuOptions[i].get_name(), commandEnd) == 0) { index = i; break; } } if (index > menuOptions.get_count()) { - printline("Invalid menu option!"); + printLine("Invalid menu option!"); continue; } - - menuOptions[index].run(); + menuOptions[index].run(buffer + commandEnd); } } |
