top of page

Console Menus Made Easier In C++

Hello, I am LeeTuah, a student who is inspired by the growing and fascinating community of Programming and Computer Science in general. I have mostly spent my time learning C++ and Python, and made some cool projects in them. In this blog, I will be talking about one of them.


Effortless-Menus is the name of my personal project. It contains a C++ class which allows to create menus in the console with ease. It allows keyboard event detection too. For example, if you were to press W and S keys in the menu, you would move one step above or below in the list of items in the menu, or if you were to press the Q, your currently selected menu item would be run. It also features colored text, that is your heading text and menu items will have different colors, and you can change those colors however you like. Each pair of item in is attached with its own function, so if you were to select any one of the items, the function associated to the item would run. On termination of the function will the menu reappear. It will run until the user chooses the Exit option at the bottom of the menu. All of this is done in C++17 purely in console. The GitHub link for the project is https://github.com/LeeTuah/Effortless-Menus


In the class, a pointer (STL Iterator) is utilized. This pointer points to the currently selected menu item, and updates based on the input of the user. It is referred to as the ‘item pointer’ in the blog. So, if you see that phrase in the blog, it probably means this.


The class offers a lot of features out of the bag. The menu in action would look something like this:




A file can have custom headings. The default one is “MENU”. However, it can be edited by the specific functions.


The menu class contains a value called the ‘max_string_len’ and it is a very important one. Basically, this variable controls the size of the menu. The default value for this variable is 32. This essentially controls the size of the menu row-wise. That is, the menu will be exactly equal to the variable size, not more, not less. This allows customization of the menu based on the length of the fields or set the menu to a perfect size which looks good and is visually attractive.


The heading of the menu can be changed at free will. The colors set in the menu heading and items are based on the ANSI color coding format. Default colors of the heading and item colors are both white. It can changed however, according to the needed, if it aligns with the ANSI color coding scheme. Also, the provided color code must be of two digits. Heading color is applied to the heading of the menu and the item pointer in the menu. The body color is applied to rest of the items.


For now, only three keybindings are supported by the menu. The up key (moves the item pointer above in the menu), the down key (moves the item pointer down the menu) and the select key (runs the function associated to the item pointer). These three keybinds are stored in a string in the above respective order. These keybinds can be changed according to the will of the programmer, and can also be retrieved from the class. However, please note that you should only use keyboard values (a - z), (A - Z (that is Shift + alphabet)), (0 - 9) or special symbols (- : ~ |, etc.) and not keys like Enter, Windows key, etc. This is because these types of keys are read differently in different operating system, and it becomes quiet hectic to manage and set different values for every possible OS just for one key. So, it is avoided entirely. The user doesn’t has to explicitly press the Enter key to register the input, just pressing the key will work.


The items in the menu are the interactable elements of the menu. They are referred to as ‘fields’ in the actual file. The fields are stored in a vector (dynamic arrays of C++) with a list of std::pairs containing a string and a function. A map or unordered map is not used because the menu will be iterated over several times, and that process is inefficient with maps and unordered maps. You can add a new field, edit an existing field, or remove an existing field. When you add a field, the function asks you for two parameters. The first one is the name, and the second one is function. On passing these, a new field will be created. To edit a field, three parameters are passed, the old name, the new name and the new function. The old name is to find the pair to be edited, and the new name and function are the new values for the old pair. The remove field function accepts a parameter of a string, checks if it exists in the given vector, then removes it, else throws an error if it is not found. There is a function to check if a given field exists or not, and is termed as has_field().


The default field in the vector is the Exit field. This field stays in the vector pre-defined and can’t be removed or edited by the programmer. This field essentially allows the user to exit the menu without heavy hassels. However, the text remains Exit and it cannot be changed.


One thing to note is that there is an external package used in the making of the menu. The package is called ‘conio.h’ and is required to run the menu program. To add conio.h in your project or includes, refer to the link https://github.com/zoelabbb/conio.h


In conclusion, this project took a lot of creative thinking and critical adaptation to the console. It was a unique idea, and was executed flawlessly. Although, I feel this project is still incomplete, and that more can be added to it or can be improved. This is to note that this is not the final version of this project, as if it is necessary, new features will be added or the old ones made better. As a beginner in C++ programming, this is my first big project and would like to accept other’s views and opinions on this topic. Thank you, for reading through this blog.

Commentaires


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2035 by Train of Thoughts. Powered and secured by Wix

bottom of page