|
| 1 | +/* |
| 2 | +alib is a header only library which offers many usefull and frequently used C++ |
| 3 | +functions which are not present in standard c++ library. The library is under |
| 4 | +GPL3 license and is open for others to contribute to this project. This library |
| 5 | +supports multi platform. It works on both windows and Linux operating system. |
| 6 | +
|
| 7 | +The project is inspired from the rang project. |
| 8 | +
|
| 9 | +The project is maintained by Mushfiqur Rahman Abir aka Abir-Tx. |
| 10 | +
|
| 11 | +
|
| 12 | +Current Version: v1.0 |
| 13 | +Maintainer: Mushfiqur Rahman Abir |
| 14 | +Developer Profile https://www.github.com/Abir-Tx |
| 15 | +*/ |
| 16 | + |
| 17 | +#if !defined(ALIB_DOT_HPP) |
| 18 | +#define ALIB_DOT_HPP |
| 19 | + |
| 20 | +// Defining the different platforms |
| 21 | +#if defined(__unix__) || defined(__unix) || defined(__linux__) |
| 22 | +#define LINUX |
| 23 | +#elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) |
| 24 | +#define WINDOWS |
| 25 | +#elif defined(__APPLE__) || defined(__MACH__) |
| 26 | +#define MAC |
| 27 | +#else |
| 28 | +#error Unknown |
| 29 | +#endif |
| 30 | + |
| 31 | +// Including platform specific files |
| 32 | +#if defined(LINUX) || defined(MAC) |
| 33 | +#include <sys/ioctl.h> /* For getting terminal height and width. See consoleWidth() & consoleHeight() */ |
| 34 | +#include <unistd.h> |
| 35 | +#elif defined(WINDOWS) |
| 36 | +#include <windows.h> |
| 37 | +#endif |
| 38 | + |
| 39 | +// Including platform independent files |
| 40 | +#include <iomanip> |
| 41 | +#include <iostream> |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +#include "rang.hpp" /* A third party library for console text styling and coloring */ |
| 45 | + |
| 46 | +// Functions |
| 47 | +namespace alib { |
| 48 | +// Function for dynamically decorating a text |
| 49 | +void decorateMe(std::string textToDecor) { |
| 50 | + using namespace rang; |
| 51 | + |
| 52 | + int charSize = textToDecor.capacity(); |
| 53 | + std::cout << std::endl; |
| 54 | + std::cout << fg::red; |
| 55 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << "_"; } |
| 56 | + std::cout << fg::reset; |
| 57 | + std::cout << std::endl; |
| 58 | + |
| 59 | + std::cout << fg::blue << style::bold << textToDecor << style::reset |
| 60 | + << fg::reset; |
| 61 | + |
| 62 | + std::cout << std::endl; |
| 63 | + std::cout << fg::red; |
| 64 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << "_"; } |
| 65 | + std::cout << fg::reset; |
| 66 | + std::cout << std::endl; |
| 67 | + std::cout << std::endl; |
| 68 | +} |
| 69 | + |
| 70 | +void decorateMe(std::string textToDecor, unsigned short int lineBreaksNumber) { |
| 71 | + using namespace rang; |
| 72 | + |
| 73 | + int charSize = textToDecor.capacity(); |
| 74 | + std::cout << std::endl; |
| 75 | + std::cout << fg::red; |
| 76 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << "_"; } |
| 77 | + std::cout << fg::reset; |
| 78 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 79 | + std::cout << fg::blue << style::bold << textToDecor << style::reset |
| 80 | + << fg::reset; |
| 81 | + |
| 82 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 83 | + std::cout << fg::red; |
| 84 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << "_"; } |
| 85 | + std::cout << fg::reset; |
| 86 | + std::cout << std::endl; |
| 87 | + std::cout << std::endl; |
| 88 | +} |
| 89 | + |
| 90 | +void decorateMe(std::string textToDecor, unsigned short int lineBreaksNumber, |
| 91 | + std::string decorator) { |
| 92 | + using namespace rang; |
| 93 | + |
| 94 | + int charSize = textToDecor.capacity(); |
| 95 | + std::cout << std::endl; |
| 96 | + std::cout << fg::red; |
| 97 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << decorator; } |
| 98 | + std::cout << fg::reset; |
| 99 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 100 | + std::cout << fg::blue << style::bold << textToDecor << style::reset |
| 101 | + << fg::reset; |
| 102 | + |
| 103 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 104 | + std::cout << fg::red; |
| 105 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << decorator; } |
| 106 | + std::cout << fg::reset; |
| 107 | + std::cout << std::endl; |
| 108 | + std::cout << std::endl; |
| 109 | +} |
| 110 | + |
| 111 | +/* Clears the screen depending on OS */ |
| 112 | +void clrscr() { |
| 113 | +#if defined(LINUX) || defined(MAC) |
| 114 | + system("clear"); |
| 115 | +#elif defined(WINDOWS) |
| 116 | + system("cls"); |
| 117 | +#endif |
| 118 | +} |
| 119 | + |
| 120 | +// Return the console's width and height respectively |
| 121 | +unsigned int consoleWidth() { |
| 122 | +#if defined(LINUX) || defined(MAC) |
| 123 | + winsize conSize; |
| 124 | + ioctl(STDOUT_FILENO, TIOCGWINSZ, &conSize); |
| 125 | + return conSize.ws_col; |
| 126 | +#elif defined(WINDOWS) |
| 127 | + int width; |
| 128 | + CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 129 | + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 130 | + width = (int)(csbi.dwSize.X); |
| 131 | + return width; |
| 132 | +#endif |
| 133 | +} |
| 134 | + |
| 135 | +unsigned int consoleHeight() |
| 136 | + |
| 137 | +{ |
| 138 | +#if defined(LINUX) || defined(MAC) |
| 139 | + winsize conSize; |
| 140 | + ioctl(STDOUT_FILENO, TIOCGWINSZ, &conSize); |
| 141 | + return conSize.ws_row; |
| 142 | +#elif defined(WINDOWS) |
| 143 | + int height; |
| 144 | + CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 145 | + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 146 | + height = (int)(csbi.dwSize.Y); |
| 147 | + return height; |
| 148 | +#endif |
| 149 | +} |
| 150 | +// Alias for the consoleWidth & consoleHeight funcs |
| 151 | +#define terminalWidth consoleWidth |
| 152 | +#define terminalHeight consoleHeight |
| 153 | + |
| 154 | +// Another overload of decorateMe function with print to center ability |
| 155 | +void decorateMe(std::string textToDecor, unsigned short int lineBreaksNumber, |
| 156 | + std::string decorator, bool isCenter) { |
| 157 | + if (isCenter) { |
| 158 | + using namespace rang; |
| 159 | + |
| 160 | + int charSize = textToDecor.capacity(); |
| 161 | + |
| 162 | + // Getting the terminal center value & starting point of the decorations |
| 163 | + unsigned int termCenter = consoleWidth() / 2; |
| 164 | + int startingPoint = termCenter - (charSize / 2); |
| 165 | + int textToDecor_StartingPoint = startingPoint + ((charSize * 2) - charSize); |
| 166 | + |
| 167 | + std::cout << std::endl; |
| 168 | + std::cout << fg::red; |
| 169 | + std::cout << std::setw(startingPoint) << std::setfill(' '); |
| 170 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << decorator; } |
| 171 | + std::cout << fg::reset; |
| 172 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 173 | + std::cout << fg::blue << style::bold << std::setw(textToDecor_StartingPoint) |
| 174 | + << std::setfill(' ') << textToDecor << style::reset << fg::reset; |
| 175 | + |
| 176 | + for (int i = 1; i <= lineBreaksNumber; i++) { std::cout << std::endl; } |
| 177 | + std::cout << fg::red; |
| 178 | + std::cout << std::setw(startingPoint) << std::setfill(' '); |
| 179 | + for (int i = 0; i < (charSize * 2); i++) { std::cout << decorator; } |
| 180 | + std::cout << fg::reset; |
| 181 | + std::cout << std::endl; |
| 182 | + std::cout << std::endl; |
| 183 | + } else { |
| 184 | + decorateMe(textToDecor, lineBreaksNumber, decorator); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +} // end of namespace alib |
| 189 | + |
| 190 | +// Undefining different platforms |
| 191 | +#undef LINUX |
| 192 | +#undef WINDOWS |
| 193 | +#undef MAC |
| 194 | + |
| 195 | +#endif // ALIB_DOT_HPP |
0 commit comments