6#include <prismspf/config.h>
20concept StringLike = std::convertible_to<T, std::string_view>;
29 static constexpr std::string_view
RESET =
"\033[0m";
33 static constexpr std::string_view
BOLD =
"\033[1m";
34 static constexpr std::string_view
DIM =
"\033[2m";
35 static constexpr std::string_view
ITALIC =
"\033[3m";
36 static constexpr std::string_view
UNDERLINE =
"\033[4m";
37 static constexpr std::string_view
BLINK =
"\033[5m";
38 static constexpr std::string_view
STRIKE =
"\033[9m";
40 static constexpr std::string_view
BLACK =
"\033[30m";
41 static constexpr std::string_view
RED =
"\033[31m";
42 static constexpr std::string_view
GREEN =
"\033[32m";
43 static constexpr std::string_view
YELLOW =
"\033[33m";
44 static constexpr std::string_view
BLUE =
"\033[34m";
45 static constexpr std::string_view
MAGENTA =
"\033[35m";
46 static constexpr std::string_view
CYAN =
"\033[36m";
47 static constexpr std::string_view
WHITE =
"\033[37m";
48 static constexpr std::string_view
DEFAULT =
"\033[39m";
51 static constexpr std::string_view
BRIGHT_RED =
"\033[91m";
59 static constexpr std::string_view
BG_BLACK =
"\033[40m";
60 static constexpr std::string_view
BG_RED =
"\033[41m";
61 static constexpr std::string_view
BG_GREEN =
"\033[42m";
62 static constexpr std::string_view
BG_YELLOW =
"\033[43m";
63 static constexpr std::string_view
BG_BLUE =
"\033[44m";
64 static constexpr std::string_view
BG_MAGENTA =
"\033[45m";
65 static constexpr std::string_view
BG_CYAN =
"\033[46m";
66 static constexpr std::string_view
BG_WHITE =
"\033[47m";
67 static constexpr std::string_view
BG_DEFAULT =
"\033[49m";
78 static constexpr std::string_view
MY_ORANGE =
"\033[38;5;166m";
79 static constexpr std::string_view
MY_PURPLE =
"\033[38;5;91m";
80 static constexpr std::string_view
MY_BLUE =
"\033[38;5;33m";
81 static constexpr std::string_view
MY_GRAY =
"\033[38;5;240m";
91 return std::string(code) + std::string(text) + std::string(
RESET);
101 for (
auto code : codes)
105 return prefix + std::string(text) + std::string(
RESET);
111 template <
typename... Codes>
112 requires(std::convertible_to<Codes, std::string_view> && ...)
117 ((prefix += std::string_view(codes)), ...);
118 return prefix + std::string(text) + std::string(
RESET);
126 std::initializer_list<std::string_view> codes,
127 std::size_t block_size = 1,
128 std::string_view modifier =
"")
131 std::span(codes.begin(), codes.size()),
139 template <
typename... Codes>
140 requires(std::convertible_to<Codes, std::string_view> && ...)
143 std::
size_t block_size,
144 std::string_view modifier,
145 [[maybe_unused]] Codes &&...codes)
147 auto code_arr = std::array {std::string_view(codes)...};
157 [[nodiscard]]
static bool
160 return std::getenv(
"TERM") !=
nullptr || std::getenv(
"COLORTERM") !=
nullptr;
166 std::span<const std::string_view> codes,
167 std::size_t block_size,
168 std::string_view modifier =
"")
170 if (codes.empty() || text.empty())
172 return std::string(text);
175 static std::mt19937 rng {std::random_device {}()};
176 std::uniform_int_distribution<std::size_t> dist(0, codes.size() - 1);
180 std::size_t index = 0;
181 while (index < text.size())
184 std::string_view code = codes[dist(rng)];
189 std::size_t chars_written = 0;
190 std::size_t next_index = index;
191 while (next_index < text.size() && chars_written < block_size)
193 if (text[next_index] ==
'\n')
198 constexpr uint8_t utf8_continuation_mask = 0xC0;
199 constexpr uint8_t utf8_continuation_pattern = 0x80;
201 while (next_index < text.size() &&
202 (
static_cast<uint8_t
>(text[next_index]) & utf8_continuation_mask) ==
203 utf8_continuation_pattern)
210 result.append(text, index, next_index - index);
213 if (next_index < text.size() && text[next_index] ==
'\n')
226PRISMS_PF_END_NAMESPACE
static constexpr std::string_view MY_ORANGE
Definition terminal.h:78
static constexpr std::string_view BG_DEFAULT
Definition terminal.h:67
static constexpr std::string_view MY_PURPLE
Definition terminal.h:79
static constexpr std::string_view MY_GRAY
Definition terminal.h:81
static constexpr std::string_view BG_RED
Definition terminal.h:60
static constexpr std::string_view BG_BRIGHT_YELLOW
Definition terminal.h:72
static constexpr std::string_view BG_BRIGHT_BLUE
Definition terminal.h:73
static constexpr std::string_view BRIGHT_YELLOW
Definition terminal.h:53
static constexpr std::string_view BRIGHT_MAGENTA
Definition terminal.h:55
static constexpr std::string_view DEFAULT
Definition terminal.h:48
static constexpr std::string_view BRIGHT_RED
Definition terminal.h:51
static constexpr std::string_view MY_BLUE
Definition terminal.h:80
static constexpr std::string_view BRIGHT_BLACK
Definition terminal.h:50
static constexpr std::string_view BG_BRIGHT_BLACK
Definition terminal.h:69
static constexpr std::string_view RED
Definition terminal.h:41
static std::string tessellate(const StringLike auto &text, std::initializer_list< std::string_view > codes, std::size_t block_size=1, std::string_view modifier="")
Tessellate text with given styles.
Definition terminal.h:125
static constexpr std::string_view BG_MAGENTA
Definition terminal.h:64
static constexpr std::string_view BLACK
Definition terminal.h:40
static constexpr std::string_view BLUE
Definition terminal.h:44
static constexpr std::string_view RESET
Definition terminal.h:29
static constexpr std::string_view ITALIC
Definition terminal.h:35
static constexpr std::string_view DIM
Definition terminal.h:34
static constexpr std::string_view BOLD
Definition terminal.h:33
static constexpr std::string_view BRIGHT_GREEN
Definition terminal.h:52
static std::string colorize(const StringLike auto &text, std::string_view code)
Wrap text in a single style.
Definition terminal.h:89
static constexpr std::string_view BRIGHT_WHITE
Definition terminal.h:57
static constexpr std::string_view ERASE_SCREEN
Definition terminal.h:31
static constexpr std::string_view WHITE
Definition terminal.h:47
static std::string tessellate_impl(std::string_view text, std::span< const std::string_view > codes, std::size_t block_size, std::string_view modifier="")
Definition terminal.h:165
static constexpr std::string_view BRIGHT_CYAN
Definition terminal.h:56
static constexpr std::string_view MAGENTA
Definition terminal.h:45
static constexpr std::string_view BG_BRIGHT_CYAN
Definition terminal.h:75
static constexpr std::string_view BG_CYAN
Definition terminal.h:65
static constexpr std::string_view BG_BLUE
Definition terminal.h:63
static constexpr std::string_view BG_BRIGHT_GREEN
Definition terminal.h:71
static constexpr std::string_view BG_WHITE
Definition terminal.h:66
static constexpr std::string_view BRIGHT_BLUE
Definition terminal.h:54
static constexpr std::string_view BG_BLACK
Definition terminal.h:59
static constexpr std::string_view UNDERLINE
Definition terminal.h:36
static constexpr std::string_view YELLOW
Definition terminal.h:43
static constexpr std::string_view BLINK
Definition terminal.h:37
static constexpr std::string_view BG_YELLOW
Definition terminal.h:62
static constexpr std::string_view BG_BRIGHT_RED
Definition terminal.h:70
static constexpr std::string_view GREEN
Definition terminal.h:42
static constexpr std::string_view BG_BRIGHT_MAGENTA
Definition terminal.h:74
static constexpr std::string_view BG_GREEN
Definition terminal.h:61
static bool is_supported() noexcept
Whether terminal supports ANSI codes.
Definition terminal.h:158
static constexpr std::string_view BG_BRIGHT_WHITE
Definition terminal.h:76
static constexpr std::string_view STRIKE
Definition terminal.h:38
static std::string colorize(const StringLike auto &text, std::initializer_list< std::string_view > codes)
Wrap text in multiple styles.
Definition terminal.h:98
static constexpr std::string_view CYAN
Definition terminal.h:46
Definition conditional_ostreams.cc:20
Definition vectorized_operations.h:17