koutil
Loading...
Searching...
No Matches
style.h
Go to the documentation of this file.
1#ifndef KOUTIL_TERM_STYLE_H
2#define KOUTIL_TERM_STYLE_H
3
4#include "koutil/util/utils.h"
5#include <array>
6#include <cstdint>
7#include <ostream>
8#include <string_view>
9#include <type_traits>
10
11namespace koutil::term {
12
13enum class Style : std::uint8_t {
14 NONE = 0,
15 BOLD = 1 << 0,
16 DIM = 1 << 1,
17 ITALIC = 1 << 2,
18 UNDERLINE = 1 << 3,
19 BLINK = 1 << 4,
20 INVERSE = 1 << 5,
21 HIDDEN = 1 << 6,
22 STRIKETHOUGH = 1 << 7,
23};
24
25constexpr Style operator+(Style a, Style b) {
26 using enum_type = std::underlying_type_t<Style>;
27
28 return static_cast<Style>(static_cast<enum_type>(a) | static_cast<enum_type>(b));
29}
30
31constexpr Style operator-(Style a, Style b) {
32 using enum_type = std::underlying_type_t<Style>;
33 return static_cast<Style>(static_cast<enum_type>(a) ^ (static_cast<enum_type>(b) & static_cast<enum_type>(a)));
34}
35
36constexpr Style operator&(Style a, Style b) {
37 using enum_type = std::underlying_type_t<Style>;
38 return static_cast<Style>(static_cast<enum_type>(a) & static_cast<enum_type>(b));
39}
40
47constexpr bool style_contains(Style style, Style flags) { return (style & flags) == flags; }
48
49std::ostream& operator<<(std::ostream& stream, Style style) {
50 using namespace std::string_view_literals;
51 if (style == Style::NONE) {
52 return stream;
53 }
54
55 /*
56 00 -> "22"
57 01 -> "22;1"
58 10 -> "22;2"
59 11 -> "1;2"
60 */
61 constexpr auto bold_dim_lookup = std::to_array<std::string_view>({ "22"sv, "22;1"sv, "22;2"sv, "1;2"sv });
62
63 stream << util::ESC << '[' << bold_dim_lookup[static_cast<int>(style & (Style::BOLD + Style::DIM))];
64
65 if (style_contains(style, Style::ITALIC)) {
66 stream << ";3";
67 } else {
68 stream << ";23";
69 }
70
71 if (style_contains(style, Style::UNDERLINE)) {
72 stream << ";4";
73 } else {
74 stream << ";24";
75 }
76
77 if (style_contains(style, Style::BLINK)) {
78 stream << ";5";
79 } else {
80 stream << ";25";
81 }
82
83 if (style_contains(style, Style::INVERSE)) {
84 stream << ";7";
85 } else {
86 stream << ";27";
87 }
88
89 if (style_contains(style, Style::HIDDEN)) {
90 stream << ";8";
91 } else {
92 stream << ";28";
93 }
94
96 stream << ";9m";
97 } else {
98 stream << ";29m";
99 }
100
101 return stream;
102}
103
109std::ostream& reset_all(std::ostream& stream) {
110 stream << util::ESC << "[0m";
111 return stream;
112}
113
114}
115
116#endif
Definition color.h:11
std::ostream & reset_all(std::ostream &stream)
Resets all text styles and colors to default.
Definition style.h:109
constexpr bool style_contains(Style style, Style flags)
Checks if a text style contains certain flags.
Definition style.h:47
Style
Definition style.h:13
std::ostream & operator<<(std::ostream &stream, ColorBG bg)
Definition color_operators.h:52
constexpr Style operator+(Style a, Style b)
Definition style.h:25
constexpr Style operator-(Style a, Style b)
Definition style.h:31
constexpr Style operator&(Style a, Style b)
Definition style.h:36
constexpr char ESC
Definition utils.h:20