3#include <prismspf/config.h>
17concept StringLike = std::convertible_to<T, std::string_view>;
26 static constexpr std::string_view
RESET =
"\033[0m";
30 static constexpr std::string_view
BOLD =
"\033[1m";
31 static constexpr std::string_view
DIM =
"\033[2m";
32 static constexpr std::string_view
ITALIC =
"\033[3m";
33 static constexpr std::string_view
UNDERLINE =
"\033[4m";
34 static constexpr std::string_view
BLINK =
"\033[5m";
35 static constexpr std::string_view
STRIKE =
"\033[9m";
37 static constexpr std::string_view
BLACK =
"\033[30m";
38 static constexpr std::string_view
RED =
"\033[31m";
39 static constexpr std::string_view
GREEN =
"\033[32m";
40 static constexpr std::string_view
YELLOW =
"\033[33m";
41 static constexpr std::string_view
BLUE =
"\033[34m";
42 static constexpr std::string_view
MAGENTA =
"\033[35m";
43 static constexpr std::string_view
CYAN =
"\033[36m";
44 static constexpr std::string_view
WHITE =
"\033[37m";
45 static constexpr std::string_view
DEFAULT =
"\033[39m";
48 static constexpr std::string_view
BRIGHT_RED =
"\033[91m";
56 static constexpr std::string_view
BG_BLACK =
"\033[40m";
57 static constexpr std::string_view
BG_RED =
"\033[41m";
58 static constexpr std::string_view
BG_GREEN =
"\033[42m";
59 static constexpr std::string_view
BG_YELLOW =
"\033[43m";
60 static constexpr std::string_view
BG_BLUE =
"\033[44m";
61 static constexpr std::string_view
BG_MAGENTA =
"\033[45m";
62 static constexpr std::string_view
BG_CYAN =
"\033[46m";
63 static constexpr std::string_view
BG_WHITE =
"\033[47m";
64 static constexpr std::string_view
BG_DEFAULT =
"\033[49m";
75 static constexpr std::string_view
MY_ORANGE =
"\033[38;5;166m";
76 static constexpr std::string_view
MY_PURPLE =
"\033[38;5;91m";
77 static constexpr std::string_view
MY_BLUE =
"\033[38;5;33m";
78 static constexpr std::string_view
MY_GRAY =
"\033[38;5;240m";
88 return std::string(code) + std::string(text) + std::string(
RESET);
98 for (
auto code : codes)
102 return prefix + std::string(text) + std::string(
RESET);
108 template <
typename... Codes>
109 requires(std::convertible_to<Codes, std::string_view> && ...)
114 ((prefix += std::string_view(codes)), ...);
115 return prefix + std::string(text) + std::string(
RESET);
123 std::initializer_list<std::string_view> codes,
124 std::size_t block_size = 1,
125 std::string_view modifier =
"")
128 std::span(codes.begin(), codes.size()),
136 template <
typename... Codes>
137 requires(std::convertible_to<Codes, std::string_view> && ...)
140 std::
size_t block_size,
141 std::string_view modifier,
142 [[maybe_unused]] Codes &&...codes)
144 auto code_arr = std::array {std::string_view(codes)...};
154 [[nodiscard]]
static bool
157 return std::getenv(
"TERM") !=
nullptr || std::getenv(
"COLORTERM") !=
nullptr;
163 std::span<const std::string_view> codes,
164 std::size_t block_size,
165 std::string_view modifier =
"")
167 if (codes.empty() || text.empty())
169 return std::string(text);
172 static std::mt19937 rng {std::random_device {}()};
173 std::uniform_int_distribution<std::size_t> dist(0, codes.size() - 1);
177 std::size_t index = 0;
178 while (index < text.size())
181 std::string_view code = codes[dist(rng)];
186 std::size_t chars_written = 0;
187 std::size_t next_index = index;
188 while (next_index < text.size() && chars_written < block_size)
190 if (text[next_index] ==
'\n')
195 constexpr uint8_t utf8_continuation_mask = 0xC0;
196 constexpr uint8_t utf8_continuation_pattern = 0x80;
198 while (next_index < text.size() &&
199 (
static_cast<uint8_t
>(text[next_index]) & utf8_continuation_mask) ==
200 utf8_continuation_pattern)
207 result.append(text, index, next_index - index);
210 if (next_index < text.size() && text[next_index] ==
'\n')
223PRISMS_PF_END_NAMESPACE
static constexpr std::string_view MY_ORANGE
Definition terminal.h:75
static constexpr std::string_view BG_DEFAULT
Definition terminal.h:64
static constexpr std::string_view MY_PURPLE
Definition terminal.h:76
static constexpr std::string_view MY_GRAY
Definition terminal.h:78
static constexpr std::string_view BG_RED
Definition terminal.h:57
static constexpr std::string_view BG_BRIGHT_YELLOW
Definition terminal.h:69
static constexpr std::string_view BG_BRIGHT_BLUE
Definition terminal.h:70
static constexpr std::string_view BRIGHT_YELLOW
Definition terminal.h:50
static constexpr std::string_view BRIGHT_MAGENTA
Definition terminal.h:52
static constexpr std::string_view DEFAULT
Definition terminal.h:45
static constexpr std::string_view BRIGHT_RED
Definition terminal.h:48
static constexpr std::string_view MY_BLUE
Definition terminal.h:77
static constexpr std::string_view BRIGHT_BLACK
Definition terminal.h:47
static constexpr std::string_view BG_BRIGHT_BLACK
Definition terminal.h:66
static constexpr std::string_view RED
Definition terminal.h:38
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:122
static constexpr std::string_view BG_MAGENTA
Definition terminal.h:61
static constexpr std::string_view BLACK
Definition terminal.h:37
static constexpr std::string_view BLUE
Definition terminal.h:41
static constexpr std::string_view RESET
Definition terminal.h:26
static constexpr std::string_view ITALIC
Definition terminal.h:32
static constexpr std::string_view DIM
Definition terminal.h:31
static constexpr std::string_view BOLD
Definition terminal.h:30
static constexpr std::string_view BRIGHT_GREEN
Definition terminal.h:49
static std::string colorize(const StringLike auto &text, std::string_view code)
Wrap text in a single style.
Definition terminal.h:86
static constexpr std::string_view BRIGHT_WHITE
Definition terminal.h:54
static constexpr std::string_view ERASE_SCREEN
Definition terminal.h:28
static constexpr std::string_view WHITE
Definition terminal.h:44
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:162
static constexpr std::string_view BRIGHT_CYAN
Definition terminal.h:53
static constexpr std::string_view MAGENTA
Definition terminal.h:42
static constexpr std::string_view BG_BRIGHT_CYAN
Definition terminal.h:72
static constexpr std::string_view BG_CYAN
Definition terminal.h:62
static constexpr std::string_view BG_BLUE
Definition terminal.h:60
static constexpr std::string_view BG_BRIGHT_GREEN
Definition terminal.h:68
static constexpr std::string_view BG_WHITE
Definition terminal.h:63
static constexpr std::string_view BRIGHT_BLUE
Definition terminal.h:51
static constexpr std::string_view BG_BLACK
Definition terminal.h:56
static constexpr std::string_view UNDERLINE
Definition terminal.h:33
static constexpr std::string_view YELLOW
Definition terminal.h:40
static constexpr std::string_view BLINK
Definition terminal.h:34
static constexpr std::string_view BG_YELLOW
Definition terminal.h:59
static constexpr std::string_view BG_BRIGHT_RED
Definition terminal.h:67
static constexpr std::string_view GREEN
Definition terminal.h:39
static constexpr std::string_view BG_BRIGHT_MAGENTA
Definition terminal.h:71
static constexpr std::string_view BG_GREEN
Definition terminal.h:58
static bool is_supported() noexcept
Whether terminal supports ANSI codes.
Definition terminal.h:155
static constexpr std::string_view BG_BRIGHT_WHITE
Definition terminal.h:73
static constexpr std::string_view STRIKE
Definition terminal.h:35
static std::string colorize(const StringLike auto &text, std::initializer_list< std::string_view > codes)
Wrap text in multiple styles.
Definition terminal.h:95
static constexpr std::string_view CYAN
Definition terminal.h:43
Definition conditional_ostreams.cc:20
Definition vectorized_operations.h:17