Merge pull request #4 from CyB3RC0nN0R/paging

Added paging
This commit is contained in:
Kai S. K. Engelbart 2020-01-14 08:13:00 +01:00 committed by GitHub
commit 99154516c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 48 deletions

101
menu.c
View File

@ -1,5 +1,6 @@
#include "pch.h" #include "pch.h"
#include "framework.h" #include "framework.h"
#include "menu.h" #include "menu.h"
const struct MenuBorder DEFAULT = { const struct MenuBorder DEFAULT = {
@ -18,6 +19,66 @@ const struct MenuBorder SOLID = {
186, 205, 201, 187, 200, 188, '[', ']' 186, 205, 201, 187, 200, 188, '[', ']'
}; };
void show_menu(const int itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border);
void page(const struct MenuPage* pages, const size_t page_count, const bool infinite_loop)
{
unsigned int page_index = 0;
bool action_performed, loop, page_changed = true;
char page_key, itemKey;
int i;
do {
do {
show_menu(pages[page_index].item_count, pages[page_index].items, pages[page_index].title, pages[page_index].border);
// Wait for user selection
putchar('>');
putchar(' ');
loop = pages[page_index].loopback;
page_key = _getch();
// Forward paging
if (page_key == 'm' && page_index < page_count - 1)
++page_index;
// Backward paging
else if (page_key == 'n' && page_index > 0)
--page_index;
else
{
page_changed = false;
itemKey = page_key;
do {
action_performed = false;
itemKey != page_key ? itemKey = _getch() : page_key;
page_key = 0;
for (i = 0; i < pages[page_index].item_count; ++i)
{
if (pages[page_index].items[i].key == itemKey)
{
// Perform action
system("cls");
pages[page_index].items[i].action();
action_performed = true;
// Pause if requested
if (pages[page_index].pause)
{
putchar('\n');
system("pause");
}
}
}
} while (!action_performed);
}
} while (loop || page_changed);
page_index = 0;
} while (infinite_loop);
}
// Checks if a line index should display a menu item // Checks if a line index should display a menu item
bool is_item_line(const int line, const int itemc, int* item_index) bool is_item_line(const int line, const int itemc, int* item_index)
{ {
@ -40,11 +101,10 @@ 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, const struct MenuItem itemv[], const char title[], const bool loopback, const bool pause, const struct MenuBorder *border) // Draws a menu to the console
void show_menu(const int itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border)
{ {
unsigned width, height, item_index; unsigned width, height, item_index;
char key;
bool action_performed;
// Clear the console window // Clear the console window
system("cls"); system("cls");
@ -63,7 +123,7 @@ void show_menu(const int itemc, const struct MenuItem itemv[], const char title[
for (unsigned j = 1; j < width - strlen(title) - 2; ++j) for (unsigned j = 1; j < width - strlen(title) - 2; ++j)
{ {
if (j == ((width - strlen(title)) - 2)/ 2) if (j == ((width - strlen(title)) - 2) / 2)
{ {
putchar(border->title_left); putchar(border->title_left);
fputs(title, stdout); fputs(title, stdout);
@ -84,7 +144,7 @@ void show_menu(const int itemc, const struct MenuItem itemv[], const char title[
// Print item text // Print item text
putchar(border->line_vertical); putchar(border->line_vertical);
if (strcmp(itemv[item_index].text, "BLANK") == 0) { if (strcmp(itemv[item_index].text, "BLANK") == 0) {
putchar('\t'); putchar('\t');
// BLANK equals 5 characters // BLANK equals 5 characters
@ -132,35 +192,4 @@ void show_menu(const int itemc, const struct MenuItem itemv[], const char title[
i != height - 2 ? putchar(border->line_vertical) : putchar(' '); i != height - 2 ? putchar(border->line_vertical) : putchar(' ');
} }
} }
// Wait for user selection
putchar('>');
putchar(' ');
action_performed = false;
do {
key = _getch();
for (int i = 0; i < itemc; ++i)
{
if (itemv[i].key == key)
{
// Perform action
system("cls");
itemv[i].action();
action_performed = true;
// Pause if requested
if (pause)
{
putchar('\n');
system("pause");
}
}
}
} while (!action_performed);
// Show menu again if requested
if (loopback)
{
show_menu(itemc, itemv, title, loopback, pause, border);
}
} }

23
menu.h
View File

@ -21,13 +21,20 @@ struct MenuBorder {
char title_right; char title_right;
}; };
// Default borders initialized in menu.c
const extern struct MenuBorder DEFAULT, MODERN, NO_BORDER, SOLID; const extern struct MenuBorder DEFAULT, MODERN, NO_BORDER, SOLID;
/// <summary>Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function.</summary> /// <summary>Represents a page containing menu items inside a menu.</summary>
/// <param name="itemc">The length of the array <c>itemv</c> of menu items.</param> struct MenuPage {
/// <param name="itemv">An array of all menu items to display in the menu.</param> const struct MenuItem* items;
/// <param name="title">The title of the menu.</param> const size_t item_count;
/// <param name="loopback">If this parameter is set to <c>true</c>, the menu will be displayed again after an action is executed.</param> char* title;
/// <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> bool loopback, pause;
/// <param name="border">Specifies the border in which the menu is displayed.</param> const struct MenuBorder* border;
void show_menu(const int itemc, const struct MenuItem itemv[], const char title[], const bool loopback, const bool pause, const struct MenuBorder *border); };
/// <summary>Constructs a CUI menu with a specific amount of pages.</summary>
/// <param name="pages">An array of all pages to display.</param>
/// <param name="page_count">The length of the array <c>pages</c>.</param>
/// <param name="infinite_loop">Always display the first page after a loopback=false item finished executing.</param>
void page(const struct MenuPage* pages, const size_t page_count, const bool infinite_loop);