Widgets for using in C++ terminal
CliWidgets are a series of widgets to use in the terminal, with the idea to help programmers to build easy menus or inputs/outputs that users will use to interact with the application.
CliWidgets has been tested on Windows 10 (x64) and Linux (Ubuntu x64).
For Linux, the requisites are:
- A C++11 compiler.
- The stty linux utility which I use to hide the terminal input for InputPassword and for hide the arrow keys when selecting an option in menus.
- The tput to hide / restore the terminal cursor.
On Linux, create a build folder and cd
inside it. Then eexecute the next commands: cmake ..
and after cmake --build .
On Windows you will have to do the steps manually:
- Create a build folder in the main directory of the project.
- Execute this command:
cmake -G "Visual Studio 16 2019" ..
thencmake --build .
It will generate a solution in the build folder with two projects:
- A library (CliWidget).
- An executable project that uses the main.cpp, used for the development of the library.
A simple output text to display a text. A format can be applied to it, enabling the next formats:
- Bold.
- Underline.
- Italic.
- Blink.
- Foreground color: sets the terminal foreground color, using a color defined in the ForegroundColor Enum.
- Background color: sets the terminal background color, using a color defined in the BackgroundColor Enum.
Example:
#include "CliWidget/Text.hpp"
cliw::Text text("Hello World");
text.setBackgroundColor(cliw::BackgroundColor::GREEN);
text.setForegroundColor(cliw::ForegroundColor::BLUE);
text.setUnderline(true);
text.setBold(true);
text.setItalic(true);
text.setBlink(true);
text.display();
A password input that hides the input when users are writting hteir passwords like in Linux. The password can be checked with a regex.
Example of input password and a regex to validate that it's length is 6 characters or more:
#include "CliWidget/Text.hpp"
#include "CliWidget/InputPassword.hpp"
cliw::Text textPwd("Insert a Password:");
cliw::InputPassword inputPsw;
inputPsw.setRegex("^.{6,}");
textPwd.display();
inputPsw.display();
cliw::Text textResult("The entered password is " + inputPsw.getValue());
textResult.setBold(true);
textResult.display();
if (!inputPsw.check()) {
cliw::Text textResult("The entered password is short");
textResult.display();
}
This widget display a set of options. The user can navigate through them with up and down keys and select one option with the enter key.
Example:
#include "CliWidget/Select.hpp"
#include "CliWidget/Text.hpp"
cliw::Select select(std::vector<std::string> {"One", "Two", "Three"});
select.display();
cliw::Text text1("Selected index is " + std::to_string(select.getSelectedIndex()) + "");
cliw::Text text2("Selected value is " + select.getSelectedValue());
text1.display();
text2.display();
This widget is like the select, with the difference that the user can select more than one option. For selecting a option the user has to press the space bar.
Example:
#include "CliWidget/MultiSelect.hpp"
#include "CliWidget/Text.hpp"
cliw::MultiSelect multiSelect(std::vector<std::string>{"Cat", "Dog", "Hamster", "Bird", "Fish"});
multiSelect.display();
std::string strText3 = "Selected indexes are ";
std::string strText4 = "Selected values are ";
for (int index : multiSelect.getSelectedIndexes())
strText3 += std::to_string(index) + " ";
for (std::string value : multiSelect.getSelectedValues())
strText4 += value + " ";