1 #ifndef OOKII_VT_HELPER_H_
4 #define OOKII_VT_HELPER_H_
10 #include <string_view>
18 template<
typename CharType,
typename Traits>
21 static constexpr CharType c_escape =
'\x1b';
23 static CharType *find_csi_end(CharType *begin, CharType *end,
const std::locale &loc)
25 for (
auto current = begin; current < end; ++current)
28 if (!std::isdigit(ch, loc) && !Traits::eq(ch,
';') && !Traits::eq(ch,
' '))
37 static CharType *find_osc_end(CharType *begin, CharType *end)
39 bool has_escape =
false;
40 for (
auto current = begin; current < end; ++current)
43 if (Traits::eq(ch, 0x7))
50 if (Traits::eq(ch,
'\\'))
59 if (Traits::eq(ch, c_escape))
68 static CharType *find_sequence_end(CharType *begin, CharType *end,
const std::locale &loc)
78 return find_csi_end(begin + 1, end, loc);
81 return find_osc_end(begin + 1, end);
102 namespace text_format
238 template<
typename CharType = ookii::details::default_
char_type>
239 inline std::basic_string<CharType>
get_extended_color(
int red,
int green,
int blue,
bool foreground =
true)
241 static constexpr
auto format_string = literal_cast<CharType>(
"\x1b[{};2;{};{};{}m");
242 return OOKII_FMT_NS format(format_string.data(), (foreground ? 38 : 48), red, green, blue);
291 !ookii::details::is_console(stream))
296 bool has_term =
false;
301 getenv_s(&required, term,
"TERM");
302 has_term = required > 0;
304 auto term = getenv(
"TERM");
305 has_term = term !=
nullptr;
309 if (has_term && strcmp(term,
"dumb") == 0)
342 getenv_s(&required,
nullptr, 0,
"NO_COLOR");
343 has_env = required > 0;
345 has_env = getenv(
"NO_COLOR") !=
nullptr;
352 return enable(stream);
364 operator bool() const noexcept
366 return is_supported();
Handles the lifetime of virtual terminal support.
Definition: vt_helper.h:254
static virtual_terminal_support enable_color(standard_stream stream)
Enables color support using virtual terminal sequences for the console attached to the specified stre...
Definition: vt_helper.h:337
static virtual_terminal_support enable(standard_stream stream)
Enables virtual terminal sequences for the console attached to the specified stream.
Definition: vt_helper.h:288
virtual_terminal_support(virtual_terminal_support &&)=default
Move constructor for virtual_terminal_support.
bool is_supported() const noexcept
Gets a value that indicates whether the stream supports virtual terminal sequences.
Definition: vt_helper.h:357
virtual_terminal_support & operator=(virtual_terminal_support &&)=default
Move assignment operator for virtual_terminal_support.
void reset() noexcept
Restores the console mode to its previous value, if it had been changed.
Definition: vt_helper.h:372
virtual_terminal_support()=default
Initializes a new instance of the virtual_terminal_support class.
Provides helpers for using the console.
constexpr const char * bold_bright
Applies the brightness/intensity flag to the foreground color.
Definition: vt_helper.h:109
constexpr const char * negative
Swaps foreground and background colors.
Definition: vt_helper.h:121
constexpr const char * foreground_white
Sets the foreground color to white.
Definition: vt_helper.h:148
constexpr const char * bright_foreground_cyan
Sets the foreground color to bright cyan.
Definition: vt_helper.h:199
constexpr const char * background_blue
Sets the background color to blue.
Definition: vt_helper.h:166
constexpr const char * no_underline
Removes underline.
Definition: vt_helper.h:118
constexpr const char * bright_foreground_magenta
Sets the foreground color to bright magenta.
Definition: vt_helper.h:196
constexpr const char * bright_foreground_blue
Sets the foreground color to bright blue.
Definition: vt_helper.h:193
constexpr const char * background_white
Sets the background color to white.
Definition: vt_helper.h:175
constexpr const char * foreground_blue
Sets the foreground color to blue.
Definition: vt_helper.h:139
constexpr const char * foreground_default
Sets the foreground color to default.
Definition: vt_helper.h:151
constexpr const char * bright_background_blue
Sets the background color to bright blue.
Definition: vt_helper.h:217
constexpr const char * default_format
Resets the text format to the settings before modification.
Definition: vt_helper.h:106
constexpr const char * bright_foreground_yellow
Sets the foreground color to bright yellow.
Definition: vt_helper.h:190
constexpr const char * bright_background_white
Sets the background color to bright white.
Definition: vt_helper.h:226
constexpr const char * positive
Returns foreground and background colors to normal.
Definition: vt_helper.h:124
constexpr const char * bright_foreground_white
Sets the foreground color to bright white.
Definition: vt_helper.h:202
constexpr const char * background_default
Sets the background color to default.
Definition: vt_helper.h:178
constexpr const char * bright_background_black
Sets the background color to bright black.
Definition: vt_helper.h:205
constexpr const char * foreground_cyan
Sets the foreground color to cyan.
Definition: vt_helper.h:145
constexpr const char * bright_background_red
Sets the background color to bright red.
Definition: vt_helper.h:208
constexpr const char * foreground_magenta
Sets the foreground color to magenta.
Definition: vt_helper.h:142
constexpr const char * background_magenta
Sets the background color to magenta.
Definition: vt_helper.h:169
constexpr const char * bright_foreground_green
Sets the foreground color to bright green.
Definition: vt_helper.h:187
std::basic_string< CharType > get_extended_color(int red, int green, int blue, bool foreground=true)
Returns the virtual terminal sequence to the foreground or background color to an RGB color.
Definition: vt_helper.h:239
constexpr const char * foreground_black
Sets the foreground color to black.
Definition: vt_helper.h:127
constexpr const char * bright_background_yellow
Sets the background color to bright yellow.
Definition: vt_helper.h:214
constexpr const char * background_yellow
Sets the background color to yellow.
Definition: vt_helper.h:163
constexpr const char * foreground_red
Sets the foreground color to red.
Definition: vt_helper.h:130
constexpr const char * foreground_green
Sets the foreground color to green.
Definition: vt_helper.h:133
constexpr const char * underline
Adds underline.
Definition: vt_helper.h:115
constexpr const char * bright_background_cyan
Sets the background color to bright cyan.
Definition: vt_helper.h:223
constexpr const char * bright_foreground_red
Sets the foreground color to bright red.
Definition: vt_helper.h:184
constexpr const char * background_green
Sets the background color to green.
Definition: vt_helper.h:160
constexpr const char * bright_background_green
Sets the background color to bright green.
Definition: vt_helper.h:211
constexpr const char * background_black
Sets the background color to black.
Definition: vt_helper.h:154
constexpr const char * bright_background_magenta
Sets the background color to bright magenta.
Definition: vt_helper.h:220
constexpr const char * bright_foreground_black
Sets the foreground color to bright black.
Definition: vt_helper.h:181
constexpr const char * background_red
Sets the background color to red.
Definition: vt_helper.h:157
constexpr const char * no_bold_bright
Removes the brightness/intensity flag to the foreground color.
Definition: vt_helper.h:112
constexpr const char * foreground_yellow
Sets the foreground color to yellow.
Definition: vt_helper.h:136
constexpr const char * background_cyan
Sets the background color to cyan.
Definition: vt_helper.h:172
Namespace containing functions and types for working with virtual terminal sequences.
Definition: vt_helper.h:15
standard_stream
Represents one of the standard console streams.
Definition: console_helper.h:32
@ output
The standard output stream.
@ error
The standard error stream.
vt_result
Indicates the result of the set_console_vt_support operation.
Definition: console_helper.h:162
@ success
Virtual terminal sequences were successfully enabled.
@ failed
Virtual terminal sequences could not be enabled.
vt_result set_console_vt_support([[maybe_unused]] standard_stream stream, [[maybe_unused]] bool enable) noexcept
Enables or disables console support for virtual terminal sequences.
Definition: console_helper.h:223