27#include <unordered_map>
53inline constexpr const char *
FG_RED =
"31";
56inline constexpr const char *
FG_BLUE =
"34";
58inline constexpr const char *
FG_CYAN =
"36";
66inline constexpr const char *
FG_BRED =
"91";
78inline constexpr const char *
BG_RED =
"41";
81inline constexpr const char *
BG_BLUE =
"44";
83inline constexpr const char *
BG_CYAN =
"46";
90inline constexpr const char *
BG_BRED =
"101";
102inline constexpr const char *
RESET =
"0";
103inline constexpr const char *
BOLD =
"1";
104inline constexpr const char *
DIM =
"2";
105inline constexpr const char *
ITALIC =
"3";
107inline constexpr const char *
BLINK =
"5";
109inline constexpr const char *
HIDDEN =
"8";
122inline std::string
esc(
const char *code) {
123 return "\033[" + std::string(code) +
"m";
141 struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
142 return poll(&pfd, 1, timeout_ms) > 0;
160 result.reserve(s.size());
161 bool in_escape =
false;
162 for (
const char c : s) {
163 if (in_escape && std::isalpha(c) != 0) {
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()));
203 const int w = ::wcswidth(wide.c_str(), wide.size());
204 return w < 0 ? wide.size() :
static_cast<size_t>(w);
225 std::array<char, 2> seq = {};
226 [[maybe_unused]]
const auto seq0 = read(STDIN_FILENO, seq.data(), 1);
236 [[maybe_unused]]
const auto seq1 = read(STDIN_FILENO, seq.data() + 1, 1);
238 static const std::unordered_map<char, Defs::Special> ARROWS = {
244 if (
auto it = ARROWS.find(seq[1]); it != ARROWS.end()) {
269 std::array<char, 3> buf = {};
270 [[maybe_unused]]
const auto buf0 = read(STDIN_FILENO, buf.data(), 1);
272 if (buf[0] ==
'\r' || buf[0] ==
'\n') {
278 if (buf[0] == 127 || buf[0] == 8) {
281 if (buf[0] ==
'\t') {
Shared type definitions for the TUI input system.
@ Escape
Escape key or unrecognised escape sequence.
@ Down
Down arrow (ESC [ B).
@ 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