Website Summary Files Commits Branches @commit @diff @stats
path: root / src / ConsoleInterface / Menu.cpp
blob: e216d28c4a64159a290bd2cedd4cd630692899b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Menu.h"
#include "Toolbox.hpp"
#include <cstring>

Menu::Menu() : Menu("Menu") {}

Menu::Menu(const char* title) : menuOptions() {
	strcpy(this->title, title);
}

Menu::Menu(const char* title, const Command* commands, unsigned commandCount) {
	menuOptions = List<Command>(commands, commandCount);
	strcpy(this->title, title);
}

void Menu::addCommand(const Command& command) {
	menuOptions.add(command);
}

enum MessageType {
	NoPrint,
	Success,
	Warning,
	Error,
};
String msg;
MessageType msgType;

void Menu::registerError(const char* message) const {
	msg = message;
	msgType = Error;
}

void Menu::registerWarning(const char* message) const {
	msg = message;
	msgType = Warning;
}

void Menu::registerSuccess(const char* message) const {
	msg = message;
	msgType = Success;
}

void Menu::navigate() const {
	if (menuOptions.get_count() == 0) {
		printLine("Menu is empty!");
		return;
	}

	unsigned buffer = 1; // We immedeately read the value of buffer, so this is fine

	while (buffer != 0) {
		printTitle(title);
		switch(msgType) {
			case Error: printError(msg.get_cstr()); msgType = NoPrint; break;
			case Warning: printWarning(msg.get_cstr()); msgType = NoPrint; break;
			case Success: printSuccess(msg.get_cstr()); msgType = NoPrint; break;
			case NoPrint: printLine(""); break;
		}
		printLine("");

		resetOrderedList(0);
		printOrderedListElem("Go Back");
		for (unsigned i = 0; i < menuOptions.get_count(); i++)
			printOrderedListElem(menuOptions[i].get_nameInMenu());

		_printAltInputBoxLabel("Execute No [0-");
		print(menuOptions.get_count());
		print("]: ");
		read(buffer);
		printLine("");

		if (buffer > menuOptions.get_count())
			registerError("Invalid menu option!");
		else if (buffer > 0)
			menuOptions[buffer - 1].run();
	}
}