Terminal detection, coloring policy, and ANSI styling utilities. More...
Enumerations | |
| enum class | sourcemeta::core::TerminalStream : std::uint8_t { TerminalStream::Stdin = 0 , TerminalStream::Stdout = 1 , TerminalStream::Stderr = 2 } |
| enum class | sourcemeta::core::TerminalColorPolicy : std::uint8_t { TerminalColorPolicy::WhenInteractive , TerminalColorPolicy::Disabled , TerminalColorPolicy::Always } |
| enum class | sourcemeta::core::TerminalStyle : std::uint8_t { TerminalStyle::None = 0 , TerminalStyle::Bold = 1 << 0 , TerminalStyle::Red = 1 << 1 , TerminalStyle::Green = 1 << 2 , TerminalStyle::Yellow = 1 << 3 , TerminalStyle::Blue = 1 << 4 , TerminalStyle::Cyan = 1 << 5 } |
Functions | |
| constexpr auto | sourcemeta::core::terminal_style_is_valid (TerminalStyle style) noexcept -> bool |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_is_interactive (TerminalStream stream) noexcept -> bool |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_is_interactive (int file_descriptor) noexcept -> bool |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_set_color_policy (TerminalColorPolicy policy) noexcept -> void |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_set_color_policy (TerminalStream stream, TerminalColorPolicy policy) noexcept -> void |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_reset_color_policy () noexcept -> void |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_reset_color_policy (TerminalStream stream) noexcept -> void |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_color_policy (TerminalStream stream=TerminalStream::Stdout) noexcept -> TerminalColorPolicy |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_color_enabled (TerminalStream stream) noexcept -> bool |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_sgr_sequence (TerminalStyle style) noexcept -> std::string_view |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_sgr_reset () noexcept -> std::string_view |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_paint (std::string_view text, TerminalStyle style, bool enabled=true) -> std::string |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_paint (TerminalStream stream, std::string_view text, TerminalStyle style) -> std::string |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_paint (std::ostream &output, std::string_view text, TerminalStyle style, bool enabled=true) -> std::ostream & |
| SOURCEMETA_CORE_TERMINAL_EXPORT auto | sourcemeta::core::terminal_paint (std::ostream &output, TerminalStream stream, std::string_view text, TerminalStyle style) -> std::ostream & |
Terminal detection, coloring policy, and ANSI styling utilities.
This functionality is included as follows:
|
strong |
Color policy governing ANSI styling output.
For example:
|
strong |
Standard I/O streams defined by POSIX.1-2017 (<unistd.h>).
For example:
| Enumerator | |
|---|---|
| Stdin | Standard input stream (POSIX.1-2017 STDIN_FILENO, 0). |
| Stdout | Standard output stream (POSIX.1-2017 STDOUT_FILENO, 1). |
| Stderr | Standard error stream (POSIX.1-2017 STDERR_FILENO, 2). |
|
strong |
Text styles and foreground colors defined by ECMA-48 (5th Edition, 1991), Section 8.3.117 "SGR - SELECT GRAPHIC RENDITION" (also standardized as ISO/IEC 6429).
Bitwise operators allow combining TerminalStyle::Bold with a foreground color (for example TerminalStyle::Bold | TerminalStyle::Red). Only one foreground color may be active at a time. If multiple foreground colors are combined, the first matching color in declaration order (Red > Green > Yellow > Blue > Cyan) takes precedence.
For example:
|
noexcept |
Determine whether styling is enabled for the specified stream.
For example:
|
noexcept |
Retrieve the current color policy for the specified stream.
For example:
|
noexcept |
Check whether the specified file descriptor is connected to an interactive terminal, according to POSIX.1-2017 isatty().
For example:
|
noexcept |
Check whether the specified stream is connected to an interactive terminal, according to POSIX.1-2017 isatty().
For example:
| SOURCEMETA_CORE_TERMINAL_EXPORT auto sourcemeta::core::terminal_paint | ( | std::ostream & | output, |
| std::string_view | text, | ||
| TerminalStyle | style, | ||
| bool | enabled = true ) -> std::ostream & |
Stream styled text with ECMA-48 Select Graphic Rendition (SGR) control sequences to an output stream without heap allocation.
For example:
| SOURCEMETA_CORE_TERMINAL_EXPORT auto sourcemeta::core::terminal_paint | ( | std::ostream & | output, |
| TerminalStream | stream, | ||
| std::string_view | text, | ||
| TerminalStyle | style ) -> std::ostream & |
Stream styled text with ECMA-48 Select Graphic Rendition (SGR) control sequences to an output stream for the given stream destination.
For example:
| SOURCEMETA_CORE_TERMINAL_EXPORT auto sourcemeta::core::terminal_paint | ( | std::string_view | text, |
| TerminalStyle | style, | ||
| bool | enabled = true ) -> std::string |
Wrap text in ECMA-48 Select Graphic Rendition (SGR) control sequences for the given style if enabled is true.
For example:
| SOURCEMETA_CORE_TERMINAL_EXPORT auto sourcemeta::core::terminal_paint | ( | TerminalStream | stream, |
| std::string_view | text, | ||
| TerminalStyle | style ) -> std::string |
Wrap text in ECMA-48 Select Graphic Rendition (SGR) control sequences for the given stream destination.
For example:
|
noexcept |
Reset the color policy across all streams to default (TerminalColorPolicy::WhenInteractive).
For example:
|
noexcept |
Reset the color policy for a specific stream to default (TerminalColorPolicy::WhenInteractive).
For example:
|
noexcept |
Set the global color policy across all streams.
For example:
|
noexcept |
Set the color policy for a specific stream.
For example:
|
noexcept |
Return the ECMA-48 Select Graphic Rendition (SGR) reset control sequence ("\033[0m").
For example:
|
noexcept |
Return the ECMA-48 Select Graphic Rendition (SGR) control sequence for the given style.
Returns an empty string view if style is TerminalStyle::None.
For example:
|
nodiscardconstexprnoexcept |
Check whether a terminal style configuration is valid and canonical.
A style is valid if it contains only defined bit flags and at most one foreground color. For example: