- Added different style options

- Fixed a bug which caused the '> ' to not display properly
- Now supporting BLANKS
This commit is contained in:
Simon 2019-12-21 19:53:19 +01:00
parent f2d0f6f743
commit 7a65e6f15c
2 changed files with 48 additions and 16 deletions

45
menu.c
View File

@ -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;
@ -41,15 +41,17 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
// 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
{
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);
}
}

15
menu.h
View File

@ -9,10 +9,23 @@ struct MenuItem {
void* (*action)(void);
};
/// <summary>Is used for style-switching</summary>
/// <item name="DEFAULT">Default style with asterisks around the terminal</item>
/// <item name="MODERN">Minimalistic design with bar instead of start>/item>
/// <item name="NO_BORDER">No border, just plain text</item>
enum MenuStyle {
DEFAULT,
MODERN,
NO_BORDER
};
/// <summary>Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function.</summary>
/// <param name="itemc">The length of the array <c>itemv</c> of menu items.</param>
/// <param name="itemv">An array of all menu items to display in the menu.</param>
/// <param name="title">The title of the menu.</param>
/// <param name="loopback">If this parameter is set to <c>true</c>, the menu will be displayed again after an action is executed.</param>
/// <param name="pause">If this parameter is set to <c>true</c>, a <c>pause</c>command will be run after an action is executed.</param>
void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause);
/// <param name="style">Specifies the style in which the menu is displayed.</param>
void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause, enum MenuStyle style);