CPGen
C++ project generator
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1#pragma once
2
23#include "Misc/Defs.hpp"
24#include <array>
25#include <cwchar>
26#include <string>
27#include <unordered_map>
28
29#ifndef _WIN32
30#include <poll.h>
31#include <unistd.h>
32#endif
33
38namespace Utils {
39
48namespace Colours {
49
52inline constexpr const char *FG_BLACK = "30";
53inline constexpr const char *FG_RED = "31";
54inline constexpr const char *FG_GREEN = "32";
55inline constexpr const char *FG_YELLOW = "33";
56inline constexpr const char *FG_BLUE = "34";
57inline constexpr const char *FG_MAGENTA = "35";
58inline constexpr const char *FG_CYAN = "36";
59inline constexpr const char *FG_WHITE = "37";
61
64inline constexpr const char *FG_BBLACK =
65 "90";
66inline constexpr const char *FG_BRED = "91";
67inline constexpr const char *FG_BGREEN = "92";
68inline constexpr const char *FG_BYELLOW = "93";
69inline constexpr const char *FG_BBLUE = "94";
70inline constexpr const char *FG_BMAGENTA = "95";
71inline constexpr const char *FG_BCYAN = "96";
72inline constexpr const char *FG_BWHITE = "97";
74
77inline constexpr const char *BG_BLACK = "40";
78inline constexpr const char *BG_RED = "41";
79inline constexpr const char *BG_GREEN = "42";
80inline constexpr const char *BG_YELLOW = "43";
81inline constexpr const char *BG_BLUE = "44";
82inline constexpr const char *BG_MAGENTA = "45";
83inline constexpr const char *BG_CYAN = "46";
84inline constexpr const char *BG_WHITE = "47";
86
89inline constexpr const char *BG_BBLACK = "100";
90inline constexpr const char *BG_BRED = "101";
91inline constexpr const char *BG_BGREEN = "102";
92inline constexpr const char *BG_BYELLOW = "103";
93inline constexpr const char *BG_BBLUE = "104";
94inline constexpr const char *BG_BMAGENTA =
95 "105";
96inline constexpr const char *BG_BCYAN = "106";
97inline constexpr const char *BG_BWHITE = "107";
99
102inline constexpr const char *RESET = "0";
103inline constexpr const char *BOLD = "1";
104inline constexpr const char *DIM = "2";
105inline constexpr const char *ITALIC = "3";
106inline constexpr const char *UNDERLINE = "4";
107inline constexpr const char *BLINK = "5";
108inline constexpr const char *INVERSE = "7";
109inline constexpr const char *HIDDEN = "8";
110inline constexpr const char *STRIKETHRU = "9";
112
122inline std::string esc(const char *code) {
123 return "\033[" + std::string(code) + "m";
124}
125
126} // namespace Colours
127
139inline bool stdinReady(int timeout_ms) {
140#ifndef _WIN32
141 struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
142 return poll(&pfd, 1, timeout_ms) > 0;
143#else
144 (void)timeout_ms;
145 return true;
146#endif
147}
148
158inline std::string stripAnsi(const std::string &s) {
159 std::string result;
160 result.reserve(s.size());
161 bool in_escape = false;
162 for (const char c : s) {
163 if (in_escape && std::isalpha(c) != 0) {
164 in_escape = false;
165 continue;
166 }
167 if (in_escape) {
168 continue;
169 }
170 if (c == '\033') {
171 in_escape = true;
172 continue;
173 }
174 result += c;
175 }
176 return result;
177}
178
195inline size_t visualWidth(const std::string &s) {
196 const std::string stripped = stripAnsi(s);
197 std::wstring wide(stripped.size(), L'\0');
198 (void)std::mbstowcs(wide.data(), stripped.c_str(), stripped.size());
199 wide.resize(std::wcslen(wide.c_str()));
200
201 // todo(Darleanow): Make this monstruosity multiplatform (aint have this on
202 // win) Considering using the utf8proc
203 const int w = ::wcswidth(wide.c_str(), wide.size()); // POSIX
204 return w < 0 ? wide.size() : static_cast<size_t>(w);
205}
206
221 if (!stdinReady(/*timeout_ms=*/50)) {
223 }
224
225 std::array<char, 2> seq = {};
226 [[maybe_unused]] const auto seq0 = read(STDIN_FILENO, seq.data(), 1);
227
228 if (seq[0] != '[') {
230 }
231
232 if (!stdinReady(/*timeout_ms=*/50)) {
234 }
235
236 [[maybe_unused]] const auto seq1 = read(STDIN_FILENO, seq.data() + 1, 1);
237
238 static const std::unordered_map<char, Defs::Special> ARROWS = {
239 {'A', Defs::Special::Up},
240 {'B', Defs::Special::Down},
242 {'D', Defs::Special::Left}};
243
244 if (auto it = ARROWS.find(seq[1]); it != ARROWS.end()) {
245 return it->second;
246 }
247
249}
250
269 std::array<char, 3> buf = {};
270 [[maybe_unused]] const auto buf0 = read(STDIN_FILENO, buf.data(), 1);
271
272 if (buf[0] == '\r' || buf[0] == '\n') {
274 }
275 if (buf[0] == 27) {
276 return handleEscapeKey();
277 }
278 if (buf[0] == 127 || buf[0] == 8) {
280 }
281 if (buf[0] == '\t') {
282 return Defs::Special::Tab;
283 }
284
285 return buf[0];
286}
287
288} // namespace Utils
Shared type definitions for the TUI input system.
@ Escape
Escape key or unrecognised escape sequence.
@ Down
Down arrow (ESC [ B).
@ Up
Up arrow (ESC [ A).
@ Tab
Horizontal tab (\t).
@ Right
Right arrow (ESC [ D).
@ Left
Left arrow (ESC [ C).
@ Backspace
Backspace (0x7F) or Delete (0x08).
@ Enter
Return / Enter key (\r or \n).
std::variant< char, Special > Key
A single keypress from stdin, represented as either a printable character or a Special control key.
Definition Defs.hpp:45
constexpr const char * FG_CYAN
Cyan foreground.
Definition Utils.hpp:58
constexpr const char * BG_MAGENTA
Magenta background.
Definition Utils.hpp:82
constexpr const char * FG_BYELLOW
Bright yellow foreground.
Definition Utils.hpp:68
constexpr const char * BG_BYELLOW
Bright yellow background.
Definition Utils.hpp:92
constexpr const char * FG_BMAGENTA
Bright magenta foreground.
Definition Utils.hpp:70
constexpr const char * FG_RED
Red foreground.
Definition Utils.hpp:53
constexpr const char * BG_BBLUE
Bright blue background.
Definition Utils.hpp:93
constexpr const char * FG_BLUE
Blue foreground.
Definition Utils.hpp:56
constexpr const char * FG_YELLOW
Yellow foreground.
Definition Utils.hpp:55
constexpr const char * BG_BRED
Bright red background.
Definition Utils.hpp:90
constexpr const char * BG_BLACK
Black background.
Definition Utils.hpp:77
constexpr const char * HIDDEN
Conceal / invisible.
Definition Utils.hpp:109
constexpr const char * BG_BWHITE
Bright white background.
Definition Utils.hpp:97
constexpr const char * UNDERLINE
Underline.
Definition Utils.hpp:106
constexpr const char * BG_CYAN
Cyan background.
Definition Utils.hpp:83
constexpr const char * BG_BBLACK
Bright black background.
Definition Utils.hpp:89
constexpr const char * FG_BGREEN
Bright green foreground.
Definition Utils.hpp:67
constexpr const char * FG_GREEN
Green foreground.
Definition Utils.hpp:54
constexpr const char * BG_RED
Red background.
Definition Utils.hpp:78
constexpr const char * INVERSE
Reverse video (swap FG/BG).
Definition Utils.hpp:108
constexpr const char * BG_WHITE
White background.
Definition Utils.hpp:84
constexpr const char * FG_BBLACK
Bright black (dark grey) foreground.
Definition Utils.hpp:64
constexpr const char * FG_BCYAN
Bright cyan foreground.
Definition Utils.hpp:71
constexpr const char * BG_BLUE
Blue background.
Definition Utils.hpp:81
constexpr const char * FG_BRED
Bright red foreground.
Definition Utils.hpp:66
constexpr const char * DIM
Dim / decreased intensity.
Definition Utils.hpp:104
std::string esc(const char *code)
Build a complete ANSI SGR escape sequence from a code string.
Definition Utils.hpp:122
constexpr const char * FG_BLACK
Black foreground.
Definition Utils.hpp:52
constexpr const char * ITALIC
Italic.
Definition Utils.hpp:105
constexpr const char * FG_BWHITE
Bright white foreground.
Definition Utils.hpp:72
constexpr const char * STRIKETHRU
Strikethrough.
Definition Utils.hpp:110
constexpr const char * BG_GREEN
Green background.
Definition Utils.hpp:79
constexpr const char * BG_BGREEN
Bright green background.
Definition Utils.hpp:91
constexpr const char * BG_BMAGENTA
Bright magenta background.
Definition Utils.hpp:94
constexpr const char * FG_WHITE
White foreground.
Definition Utils.hpp:59
constexpr const char * BG_BCYAN
Bright cyan background.
Definition Utils.hpp:96
constexpr const char * FG_BBLUE
Bright blue foreground.
Definition Utils.hpp:69
constexpr const char * RESET
Reset all attributes.
Definition Utils.hpp:102
constexpr const char * BOLD
Bold / increased intensity.
Definition Utils.hpp:103
constexpr const char * FG_MAGENTA
Magenta foreground.
Definition Utils.hpp:57
constexpr const char * BG_YELLOW
Yellow background.
Definition Utils.hpp:80
constexpr const char * BLINK
Slow blink.
Definition Utils.hpp:107
TUI utility functions and constants.
size_t visualWidth(const std::string &s)
Compute the visual (display) column width of a UTF-8 string.
Definition Utils.hpp:195
bool stdinReady(int timeout_ms)
Poll stdin for available data within a timeout.
Definition Utils.hpp:139
std::string stripAnsi(const std::string &s)
Remove all ANSI escape sequences from a string.
Definition Utils.hpp:158
Defs::Key readKey()
Read a single keypress from stdin (terminal must be in raw mode).
Definition Utils.hpp:268
Defs::Key handleEscapeKey()
Decode an escape sequence whose leading ESC byte has already been consumed.
Definition Utils.hpp:220