From 7a65e6f15c1ba433910efc5eb4454c004d6de157 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 21 Dec 2019 19:53:19 +0100 Subject: [PATCH] - Added different style options - Fixed a bug which caused the '> ' to not display properly - Now supporting BLANKS --- menu.c | 49 ++++++++++++++++++++++++++++++++++--------------- menu.h | 15 ++++++++++++++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/menu.c b/menu.c index f88fa2e..7f1e3e8 100644 --- a/menu.c +++ b/menu.c @@ -23,7 +23,7 @@ void get_console_dimensions(int* width, int* height) *height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; } -void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause) +void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause, enum MenuStyle style) { unsigned width, height, item_index; char key; @@ -34,22 +34,24 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo // Get the width and height of the console window get_console_dimensions(&width, &height); - + // Print frame with title for (unsigned i = 0; i < height - 1; ++i) { // Top line with title if (i == 0) { - for (unsigned j = 0; j < width - strlen(title) + 1; ++j) + for (unsigned j = 0; j < width - strlen(title) - 1; ++j) { - if (j == (width - strlen(title)) / 2) + if (j == ((width - strlen(title)) - 2)/ 2) { + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('[') : putchar(' '); fputs(title, stdout); + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('[') : putchar(' '); } - else + else { - putchar('*'); + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' '); } } } @@ -57,38 +59,55 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo else if (is_item_line(i, itemc, &item_index)) { // Print item text - printf("*\t%c) %s", itemv[item_index].key, itemv[item_index].text); + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' '); + + if (strcmp(itemv[item_index].text, "BLANK") == 0) { + putchar('\t'); + + // BLANK equals 5 characters + // + 3 since it regularly is "%c) " inbetween title and key + for (unsigned j = 0; j < 8; ++j) + { + putchar(' '); + } + } + else { + printf("\t%c) %s", itemv[item_index].key, itemv[item_index].text); + + } + // Print right side of frame for (unsigned j = 0; j < width - strlen(itemv[item_index].text) - 12; ++j) { putchar(' '); } - putchar('*'); + + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' '); } // Line above bottom line - else if (i == height - 2) + else if (i == height - 3) { for (unsigned j = 0; j < width; ++j) { - putchar('*'); + style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' '); } } // Blank line else { - putchar('*'); + i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' '); for (unsigned j = 0; j < width - 2; ++j) { putchar(' '); } - putchar('*'); + i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' '); } - putchar('\n'); } // Wait for user selection - putchar('> '); + putchar('>'); + putchar(' '); action_performed = false; do { key = _getch(); @@ -114,6 +133,6 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo // Show menu again if requested if (loopback) { - show_menu(itemc, itemv, title, loopback, pause); + show_menu(itemc, itemv, title, loopback, pause, style); } } \ No newline at end of file diff --git a/menu.h b/menu.h index bdb7fe1..b3e420f 100644 --- a/menu.h +++ b/menu.h @@ -9,10 +9,23 @@ struct MenuItem { void* (*action)(void); }; + +/// Is used for style-switching +/// Default style with asterisks around the terminal +/// Minimalistic design with bar instead of start>/item> +/// No border, just plain text +enum MenuStyle { + DEFAULT, + MODERN, + NO_BORDER +}; + + /// Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function. /// The length of the array itemv of menu items. /// An array of all menu items to display in the menu. /// The title of the menu. /// If this parameter is set to true, the menu will be displayed again after an action is executed. /// If this parameter is set to true, a pausecommand will be run after an action is executed. -void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause); \ No newline at end of file +/// Specifies the style in which the menu is displayed. +void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause, enum MenuStyle style); \ No newline at end of file