Skip to content

Commit

Permalink
Added printGrid and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mimhle committed Nov 30, 2023
1 parent 565f831 commit de9bd36
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
39 changes: 38 additions & 1 deletion UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ void UserInterface::print(const std::vector<std::string>& items, int color, bool
}
}

void UserInterface::printTable(const std::vector<std::vector<std::string>>& items, int color, bool newLine,
char separator, bool center
) const {
std::vector<int> vtMaxWidth;
for (const std::vector<std::string>& vtItem: items) {
for (int i = 0; i < vtItem.size(); i++) {
if (vtMaxWidth.size() <= i) {
vtMaxWidth.push_back(0);
}
if (vtItem[i].length() > vtMaxWidth[i]) {
vtMaxWidth[i] = (int) vtItem[i].length();
}
}
}

std::vector<std::string> vtStrItems;
for (const std::vector<std::string>& vtItem: items) {
std::string strItem;
for (const auto & s : vtItem) {
strItem += s + std::string(vtMaxWidth[&s - &vtItem[0]] - s.length(), ' ') + separator;
}
vtStrItems.push_back(strItem);
}

for (const std::string& strItem: vtStrItems) {
if (center) printCentered(strItem, color, false);
else print(strItem, color, false);

if (strItem != vtStrItems.back() && vtStrItems.size() > 1) {
printLineBreak();
}
}
if (newLine) {
printLineBreak();
}
}

void UserInterface::printCentered(const std::string& text, int color, bool newLine, char padding, char cap,
int capColor
) const {
Expand Down Expand Up @@ -170,7 +207,7 @@ std::string UserInterface::input(const std::string& message, int color, bool hid

std::string strInput;
char c;
while ((int)(c = _getch()) != 13) { // enter
while ((int) (c = _getch()) != 13) { // enter
if (c == 8) { // backspace
if (!strInput.empty()) {
printf("\b \b");
Expand Down
18 changes: 17 additions & 1 deletion UserInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ class UserInterface {
print(const std::vector<std::string>& items, int color = DEFAULT_COLOR, bool newLine = true, char separator = '\n'
) const;

/**
* @brief Print text to console
*
* @param items Items to print (vector of strings)
* @param color Color of text
* @param newLine Print new line after text
* @param separator Separator character
* @param center Center text
* @return void
*/
void printTable(const std::vector<std::vector<std::string>>& items, int color = DEFAULT_COLOR, bool newLine = true,
char separator = ' ', bool center = false
) const;

/**
* @brief Print text to console, centered
*
Expand Down Expand Up @@ -156,7 +170,9 @@ class UserInterface {
* @param hideChar Character to replace input with (if hideInput is true)
* @return User input
*/
std::string input(const std::string& message = "", int color = DEFAULT_COLOR, bool hideInput = false, char hideChar = '*') const;
std::string input(const std::string& message = "", int color = DEFAULT_COLOR, bool hideInput = false,
char hideChar = '*'
) const;
};

#endif //CTDL_GK_USERINTERFACE_CPP
Expand Down
4 changes: 0 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void adminMenu() {
}

void employeeMenu() {
g_ui.cBorder = '*';
g_ui.printCentered("EMPLOYEE MENU", LIGHT_YELLOW, true, '-');
g_ui.print(
{
Expand All @@ -82,7 +81,6 @@ void employeeMenu() {
}

void loginMenu() {
g_ui.cBorder = '*';
g_ui.printTitle("LOGIN", LIGHT_GREEN, BRIGHT_WHITE, true);
g_ui.printLineBreak();
g_ui.print(
Expand Down Expand Up @@ -116,7 +114,6 @@ void loginMenu() {

void loginAdmin() {
g_ui.clearScreen();
g_ui.cBorder = '*';
g_ui.printTitle("ADMIN LOGIN", LIGHT_YELLOW, LIGHT_CYAN, true);
g_ui.printLineBreak();
g_userAccount = g_ui.input("Username: ", LIGHT_PURPLE, false);
Expand All @@ -143,7 +140,6 @@ void loginAdmin() {

void loginEmployee() {
g_ui.clearScreen();
g_ui.cBorder = '*';
g_ui.printTitle("EMPLOYEE LOGIN", LIGHT_YELLOW, LIGHT_CYAN, true);
g_ui.printLineBreak();
g_userAccount = g_ui.input("Username: ", LIGHT_PURPLE, false);
Expand Down

0 comments on commit de9bd36

Please sign in to comment.