Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [perfectConMenu.cpp]
- A pretty decent, unflinching (in terms of imperfections), console menu.
- (c) Damion 'Phr0z3n.dev' Tapper, 2014.
- Email: Phr0z3n.Dev@Gmail.com
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- /**
- * These definitions will be used to select code based on OS environment. Standard library definitions
- * could be utilised but, being that some compilers and architectures are still undergoing the 32 -> 64 bit
- * transition, I guess its best to stay on the safe side for the time being.
- **/
- #define _WINDOWS 0x001
- #define _NIX 0x002
- /**
- * Comment/uncomment as required.
- **/
- #define _MY_ENV WINDOWS
- //#define _MY_ENV _NIX
- #include <iostream>
- #include <string>
- #if _MY_ENV == WINDOWS
- #include <windows.h>
- #define CSBI CONSOLE_SCREEN_BUFFER_INFO
- /**
- * This directive is almost never required for the 'system' function is almost always accessible anyway.
- **/
- #elif _MY_ENV == _NIX
- #include <cstdlib>
- #include <unistd.h>
- #include <term.h>
- #endif
- /*===============================================--=================================================
- FUNCTION PROTOTYPES
- ==================================================================================================*/
- int mainMenu(void);
- void sysAdmin(void);
- inline void clearScreen(void);
- int main(void)
- {
- if(mainMenu() == 0)
- {
- exit(EXIT_SUCCESS);
- }
- return 0;
- }
- /*===============================================--=================================================
- THE MAIN MENU FUNCTION
- ==================================================================================================*/
- int mainMenu(void)
- {
- std::string *mainMenuOptBuffer = new std::string; /* Why 'string' and not 'char'? */
- /**
- * std::cin.get() (not pertaining to certain overloaded versions), accepts (and store) only one
- * character at a time (ONLY when ENTER is pressed). This bit of logic
- * does not hinder the fact that more than one characters can still be entered at an std::cin.get()
- * prompt.
- *
- * In its true sense, expecting a '1' menu option and even accepting a '1abc' option instead,
- * is in no way, logical. std::cin.getline() will store all options entered, and the relative logic
- * implemented, will evaluate and differentiate between '1' and '1abc' (&| an option of length 1 || > 1).
- *
- * It is the latter-mentioned logic, which is actually implemented in this code, that makes this
- * program foolproof.
- **/
- while(mainMenuOptBuffer->length() != 1)
- {
- /**
- * Console menu recursion without logic to clear the screen is redundantly messy. No pun intended.
- **/
- clearScreen();
- std::cout << "SELECT OPTION (1-8)" << std::endl << std::endl;
- std::cout << "[COLOR]" << std::endl;
- std::cout << "1. " << "Red" << std::endl;
- std::cout << "2. " << "Green" << std::endl;
- std::cout << "3. " << "Blue" << std::endl;
- std::cout << "4. " << "Cyan" << std::endl;
- std::cout << "5. " << "Magenta" << std::endl;
- std::cout << "6. " << "Yellow" << std::endl << std::endl;
- std::cout << "[SYSTEM]" << std::endl;
- std::cout << "7. " << "Admin" << std::endl;
- std::cout << "8. " << "Exit" << std::endl << std::endl;
- std::cout << ">: ";
- std::getline(std::cin, *mainMenuOptBuffer, '\n');
- }
- std::cout << std::endl;
- switch(mainMenuOptBuffer->data()[0])
- {
- case '1':
- std::cout << "You selected 'Red'";
- break;
- case '2':
- std::cout << "You selected 'Green'";
- break;
- case '3':
- std::cout << "You selected 'Blue'";
- break;
- case '4':
- std::cout << "You selected 'Cyan'";
- break;
- case '5':
- std::cout << "You selected 'Magenta'";
- break;
- case '6':
- std::cout << "You selected 'Yellow'";
- break;
- case '7':
- sysAdmin();
- break;
- case '8':
- return 0; /* The program returns to 'main' to exit. More than just cool stuff. */
- default:
- /**
- * If no option matches the main menu is redisplayed.
- * Notice how the function did not recurse. That technique can prove a bit elusive under
- * certain coding circumstances. This method is so superior, it should be considered
- * de jure or boilerplate.
- **/
- mainMenu();
- }
- /**
- * NOTE:
- * You may or may not want to implement code here to store the main menu option globally, in case
- * the operations of other sections of your program depend on it.
- *
- * 'CMainMenu::setMainMenuOption(mainMenuOptBuffer->data()[0])', for instance.
- **/
- /**
- * A coding guru once told me that clean code is superior coding practice, not only in its logic but
- * also in its manifestation. Hence, the disposal of unneeded variables.
- **/
- delete mainMenuOptBuffer;
- return 0;
- }
- /*===============================================--=================================================
- THE 'Admin' OPTION FUNCTION EXAMPLE
- ==================================================================================================*/
- void sysAdmin(void)
- {
- std::cout << "Congrats! You made it in the system...";
- }
- /*===============================================--=================================================
- THE CLEAR SCREEN FUNCTION
- ==================================================================================================*/
- inline void clearScreen(void)
- {
- #if _MY_ENV == WINDOWS
- /**
- * NOTE:
- * This particular function is a nifty little snippet anyone can use, with minor alterations
- * (function name), to clear the console screen on Windows, without having to depend on the
- * predefined non-standard ones in particular IDEs. All that is required is the inclusion of the
- * 'windows.h' header file.
- **/
- COORD coordScreen = {0, 0}; /* Home for the cursor. */
- DWORD cCharsWritten;
- /**
- * NOTE:
- * CSBI is originally defined as CONSOLE_SCREEN_BUFFER_INFO in (windows.h) 'wincon.h' but I suspected
- * that the abbreviated redefinition (in 'simpleCrypto.hpp') would make the code neater.
- **/
- CSBI csbi;
- DWORD dwConSize;
- HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
- /* Get the number of character cells in the current buffer. */
- if(!GetConsoleScreenBufferInfo(hConsoleOut, &csbi))
- return;
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- /* Fill the entire screen with blanks. */
- if(!FillConsoleOutputCharacter(hConsoleOut, ' ', dwConSize, coordScreen, &cCharsWritten))
- return;
- /* Get the current text attribute. */
- if(!GetConsoleScreenBufferInfo(hConsoleOut, &csbi))
- return;
- /* Set the buffer's attributes accordingly. */
- if(!FillConsoleOutputAttribute(hConsoleOut, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
- return;
- /* Put the cursor at its home coordinates. */
- SetConsoleCursorPosition(hConsoleOut, coordScreen);
- #elif _MY_ENV == _NIX
- if(!cur_term)
- {
- int result;
- setupterm(NULL, STDOUT_FILENO, &result);
- if(result <= 0) return;
- }
- putp(tigetstr("clear"));
- #endif // _MY_ENV
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement