Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
terminal.h
1#ifndef SOURCEMETA_CORE_TERMINAL_H_
2#define SOURCEMETA_CORE_TERMINAL_H_
3
4#ifndef SOURCEMETA_CORE_TERMINAL_EXPORT
5#include <sourcemeta/core/terminal_export.h>
6#endif
7
8#include <array> // std::array
9#include <cstdint> // std::uint8_t
10#include <iosfwd> // std::ostream
11#include <string> // std::string
12#include <string_view> // std::string_view
13#include <type_traits> // std::underlying_type_t
14
23
24namespace sourcemeta::core {
25
41enum class TerminalStream : std::uint8_t {
45 Stdin = 0,
49 Stdout = 1,
54};
55
84
110enum class TerminalStyle : std::uint8_t {
112 None = 0,
114 Bold = 1 << 0,
116 Red = 1 << 1,
118 Green = 1 << 2,
120 Yellow = 1 << 3,
122 Blue = 1 << 4,
124 Cyan = 1 << 5
125};
126
142[[nodiscard]] constexpr auto operator|(TerminalStyle left,
143 TerminalStyle right) noexcept
144 -> TerminalStyle {
145 using Underlying = std::underlying_type_t<TerminalStyle>;
146 return static_cast<TerminalStyle>(static_cast<Underlying>(left) |
147 static_cast<Underlying>(right));
148}
149
165[[nodiscard]] constexpr auto operator&(TerminalStyle left,
166 TerminalStyle right) noexcept
167 -> TerminalStyle {
168 using Underlying = std::underlying_type_t<TerminalStyle>;
169 return static_cast<TerminalStyle>(static_cast<Underlying>(left) &
170 static_cast<Underlying>(right));
171}
172
187[[nodiscard]] constexpr auto operator~(TerminalStyle value) noexcept
188 -> TerminalStyle {
189 using Underlying = std::underlying_type_t<TerminalStyle>;
190 return static_cast<TerminalStyle>(~static_cast<Underlying>(value));
191}
192
207[[nodiscard]] constexpr auto operator^(TerminalStyle left,
208 TerminalStyle right) noexcept
209 -> TerminalStyle {
210 using Underlying = std::underlying_type_t<TerminalStyle>;
211 return static_cast<TerminalStyle>(static_cast<Underlying>(left) ^
212 static_cast<Underlying>(right));
213}
214
233[[nodiscard]] constexpr auto
234terminal_style_is_valid(TerminalStyle style) noexcept -> bool {
235 using Underlying = std::underlying_type_t<TerminalStyle>;
236 constexpr Underlying DEFINED_MASK{
237 static_cast<Underlying>(TerminalStyle::Bold) |
238 static_cast<Underlying>(TerminalStyle::Red) |
239 static_cast<Underlying>(TerminalStyle::Green) |
240 static_cast<Underlying>(TerminalStyle::Yellow) |
241 static_cast<Underlying>(TerminalStyle::Blue) |
242 static_cast<Underlying>(TerminalStyle::Cyan)};
243
244 const auto raw{static_cast<Underlying>(style)};
245 if ((raw & ~DEFINED_MASK) != 0) {
246 return false;
247 }
248
249 constexpr std::array<TerminalStyle, 5> COLORS{
252
253 int color_count{0};
254 for (const auto color : COLORS) {
255 if ((style & color) != TerminalStyle::None) {
256 ++color_count;
257 if (color_count > 1) {
258 return false;
259 }
260 }
261 }
262
263 return true;
264}
265
281SOURCEMETA_CORE_TERMINAL_EXPORT
282auto terminal_is_interactive(TerminalStream stream) noexcept -> bool;
283
299SOURCEMETA_CORE_TERMINAL_EXPORT
300auto terminal_is_interactive(int file_descriptor) noexcept -> bool;
301
314SOURCEMETA_CORE_TERMINAL_EXPORT
315auto terminal_set_color_policy(TerminalColorPolicy policy) noexcept -> void;
316
330SOURCEMETA_CORE_TERMINAL_EXPORT
332 TerminalColorPolicy policy) noexcept -> void;
333
351SOURCEMETA_CORE_TERMINAL_EXPORT
352auto terminal_reset_color_policy() noexcept -> void;
353
374SOURCEMETA_CORE_TERMINAL_EXPORT
375auto terminal_reset_color_policy(TerminalStream stream) noexcept -> void;
376
392SOURCEMETA_CORE_TERMINAL_EXPORT
394 TerminalStream stream = TerminalStream::Stdout) noexcept
396
409SOURCEMETA_CORE_TERMINAL_EXPORT
410auto terminal_color_enabled(TerminalStream stream) noexcept -> bool;
411
432SOURCEMETA_CORE_TERMINAL_EXPORT
433auto terminal_sgr_sequence(TerminalStyle style) noexcept -> std::string_view;
434
451SOURCEMETA_CORE_TERMINAL_EXPORT
452auto terminal_sgr_reset() noexcept -> std::string_view;
453
472SOURCEMETA_CORE_TERMINAL_EXPORT
473auto terminal_paint(std::string_view text, TerminalStyle style,
474 bool enabled = true) -> std::string;
475
493SOURCEMETA_CORE_TERMINAL_EXPORT
494auto terminal_paint(TerminalStream stream, std::string_view text,
495 TerminalStyle style) -> std::string;
496
514SOURCEMETA_CORE_TERMINAL_EXPORT
515auto terminal_paint(std::ostream &output, std::string_view text,
516 TerminalStyle style, bool enabled = true) -> std::ostream &;
517
536SOURCEMETA_CORE_TERMINAL_EXPORT
537auto terminal_paint(std::ostream &output, TerminalStream stream,
538 std::string_view text, TerminalStyle style)
539 -> std::ostream &;
540
541} // namespace sourcemeta::core
542
543#endif
@ None
The cookie is sent on every cross-site request.
Definition http.h:615
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_is_interactive(TerminalStream stream) noexcept -> bool
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_sgr_reset() noexcept -> std::string_view
constexpr auto terminal_style_is_valid(TerminalStyle style) noexcept -> bool
Definition terminal.h:234
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_set_color_policy(TerminalColorPolicy policy) noexcept -> void
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_color_policy(TerminalStream stream=TerminalStream::Stdout) noexcept -> TerminalColorPolicy
TerminalStyle
Definition terminal.h:110
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_color_enabled(TerminalStream stream) noexcept -> bool
TerminalStream
Definition terminal.h:41
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_reset_color_policy() noexcept -> void
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_paint(std::string_view text, TerminalStyle style, bool enabled=true) -> std::string
TerminalColorPolicy
Definition terminal.h:71
SOURCEMETA_CORE_TERMINAL_EXPORT auto terminal_sgr_sequence(TerminalStyle style) noexcept -> std::string_view
@ Cyan
Cyan foreground color (ECMA-48 SGR parameter 36).
Definition terminal.h:124
@ Bold
Bold or increased intensity (ECMA-48 SGR parameter 1).
Definition terminal.h:114
@ Yellow
Yellow foreground color (ECMA-48 SGR parameter 33).
Definition terminal.h:120
@ None
Normal display / no style (plain text, ECMA-48 SGR parameter 0).
Definition terminal.h:112
@ Blue
Blue foreground color (ECMA-48 SGR parameter 34).
Definition terminal.h:122
@ Green
Green foreground color (ECMA-48 SGR parameter 32).
Definition terminal.h:118
@ Red
Red foreground color (ECMA-48 SGR parameter 31).
Definition terminal.h:116
@ Stdout
Definition terminal.h:49
@ Stdin
Definition terminal.h:45
@ Stderr
Definition terminal.h:53
@ Always
Definition terminal.h:82
@ Disabled
Styling is unconditionally suppressed.
Definition terminal.h:79
@ WhenInteractive
Definition terminal.h:77