Merge pull request #3 from CyB3RC0nN0R/dev_Simon

Added different style options
This commit is contained in:
Kai S. K. Engelbart 2019-12-21 20:15:59 +01:00 committed by GitHub
commit 0bb541db4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 16 deletions

45
menu.c
View File

@ -24,7 +24,7 @@ void get_console_dimensions(int* width, int* height)
*height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; *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; unsigned width, height, item_index;
char key; char key;
@ -42,15 +42,17 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
// Top line with title // Top line with title
if (i == 0) 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); fputs(title, stdout);
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('[') : putchar(' ');
} }
else else
{ {
putchar('*'); style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' ');
} }
} }
} }
@ -58,38 +60,55 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
else if (is_item_line(i, itemc, &item_index)) else if (is_item_line(i, itemc, &item_index))
{ {
// Print item text // 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 // Print right side of frame
for (unsigned j = 0; j < width - strlen(itemv[item_index].text) - 12; ++j) for (unsigned j = 0; j < width - strlen(itemv[item_index].text) - 12; ++j)
{ {
putchar(' '); putchar(' ');
} }
putchar('*');
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ');
} }
// Line above bottom line // Line above bottom line
else if (i == height - 2) else if (i == height - 3)
{ {
for (unsigned j = 0; j < width; ++j) for (unsigned j = 0; j < width; ++j)
{ {
putchar('*'); style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' ');
} }
} }
// Blank line // Blank line
else else
{ {
putchar('*'); i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' ');
for (unsigned j = 0; j < width - 2; ++j) for (unsigned j = 0; j < width - 2; ++j)
{ {
putchar(' '); putchar(' ');
} }
putchar('*'); i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' ');
} }
putchar('\n');
} }
// Wait for user selection // Wait for user selection
putchar('> '); putchar('>');
putchar(' ');
action_performed = false; action_performed = false;
do { do {
key = _getch(); key = _getch();
@ -115,6 +134,6 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
// Show menu again if requested // Show menu again if requested
if (loopback) 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); 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> /// <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="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="itemv">An array of all menu items to display in the menu.</param>
/// <param name="title">The title of 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="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> /// <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);