mini-llvm 0.1.0
Loading...
Searching...
No Matches
Color.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <bitset>
6#include <concepts>
7#include <cstdio>
8#include <format>
9#include <initializer_list>
10#include <string>
11#include <string_view>
12#include <utility>
13
15
16namespace mini_llvm {
17
19
20class
21#ifdef _WIN32
23#endif
25public:
26#ifdef _WIN32
27 VTModeGuard(FILE *stream, bool enableVTMode);
29#else
30 VTModeGuard(FILE *, bool) {}
31#endif
32
33 VTModeGuard(const VTModeGuard &) = delete;
34 VTModeGuard &operator=(const VTModeGuard &) = delete;
35
38
39private:
40#ifdef _WIN32
41 void *hConsole_;
42 bool oldVTModeEnabled_;
43#endif
44};
45
47public:
48 explicit ColorGuard(bool enableColor);
50
51 ColorGuard(const ColorGuard &) = delete;
52 ColorGuard &operator=(const ColorGuard &) = delete;
53
54 ColorGuard(ColorGuard &&) = delete;
56
57private:
58 bool oldColorEnabled_;
59};
60
62 using Codes = std::bitset<128>;
63
64 std::string_view str;
66
67 ColoredStringView(std::string_view str)
68 : str(str) {}
69
70 ColoredStringView(std::string_view str, Codes codes)
71 : str(str), codes(codes) {}
72
73 operator std::string() const;
74};
75
77 using Codes = std::bitset<128>;
78
79 std::string str;
81
82 ColoredString(std::string str)
83 : str(std::move(str)) {}
84
86 : str(std::move(str)), codes(codes) {}
87
88 operator ColoredStringView() const {
90 }
91
92 operator std::string() const {
93 return std::string(ColoredStringView(str, codes));
94 }
95};
96
97class Color {
98public:
99 using Codes = std::bitset<128>;
100
101 explicit Color(Codes codes) : codes_(codes) {}
102
103 Color(std::initializer_list<int> il) {
104 for (int code : il) {
105 codes_.set(code);
106 }
107 }
108
109 Codes codes() const {
110 return codes_;
111 }
112
114 str.codes |= codes_;
115 return str;
116 }
117
118 ColoredStringView operator()(std::string_view str) const {
119 return (*this)(ColoredStringView(str));
120 }
121
122 template <typename T>
123 requires std::same_as<T &&, ColoredString &&>
124 ColoredString operator()(T &&str) const {
125 str.codes |= codes_;
126 return str;
127 }
128
129 template <typename T>
130 requires std::same_as<T &&, std::string &&>
131 ColoredString operator()(T &&str) const {
132 return (*this)(ColoredString(std::move(str)));
133 }
134
135private:
136 Codes codes_;
137};
138
140 return Color(~color.codes());
141}
142
143inline Color operator&(Color lhs, Color rhs) {
144 return Color(lhs.codes() & rhs.codes());
145}
146
147inline Color operator|(Color lhs, Color rhs) {
148 return Color(lhs.codes() | rhs.codes());
149}
150
151inline Color operator^(Color lhs, Color rhs) {
152 return Color(lhs.codes() ^ rhs.codes());
153}
154
155inline Color &operator&=(Color &lhs, Color rhs) {
156 lhs = lhs & rhs;
157 return lhs;
158}
159
160inline Color &operator|=(Color &lhs, Color rhs) {
161 lhs = lhs | rhs;
162 return lhs;
163}
164
165inline Color &operator^=(Color &lhs, Color rhs) {
166 lhs = lhs ^ rhs;
167 return lhs;
168}
169
170namespace colors {
171
172#define X(code, name) inline const Color name{code};
173#include "mini-llvm/utils/Color.def"
174#undef X
175
176} // namespace colors
177
178} // namespace mini_llvm
179
180template <>
181struct std::formatter<mini_llvm::ColoredStringView> {
182 constexpr auto parse(std::format_parse_context &ctx) {
183 return ctx.begin();
184 }
185
186 template <typename FormatContext>
187 auto format(const mini_llvm::ColoredStringView &str, FormatContext &ctx) const {
188 return std::format_to(ctx.out(), "{}", std::string(str));
189 }
190};
191
192template <>
193struct std::formatter<mini_llvm::ColoredString>
194 : std::formatter<mini_llvm::ColoredStringView> {};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
ColorGuard(const ColorGuard &)=delete
ColorGuard & operator=(const ColorGuard &)=delete
ColorGuard(ColorGuard &&)=delete
ColorGuard(bool enableColor)
ColorGuard & operator=(ColorGuard &&)=delete
Definition Color.h:97
ColoredString operator()(T &&str) const
Definition Color.h:124
std::bitset< 128 > Codes
Definition Color.h:99
ColoredStringView operator()(ColoredStringView str) const
Definition Color.h:113
ColoredStringView operator()(std::string_view str) const
Definition Color.h:118
Codes codes() const
Definition Color.h:109
Color(std::initializer_list< int > il)
Definition Color.h:103
Color(Codes codes)
Definition Color.h:101
ColoredString operator()(T &&str) const
Definition Color.h:131
VTModeGuard(const VTModeGuard &)=delete
VTModeGuard(VTModeGuard &&)=delete
VTModeGuard & operator=(const VTModeGuard &)=delete
VTModeGuard & operator=(VTModeGuard &&)=delete
VTModeGuard(FILE *, bool)
Definition Color.h:30
Definition Color.h:170
Definition GraphColoringAllocator.h:13
Color operator^(Color lhs, Color rhs)
Definition Color.h:151
Color operator~(Color color)
Definition Color.h:139
Color operator&(Color lhs, Color rhs)
Definition Color.h:143
Color operator|(Color lhs, Color rhs)
Definition Color.h:147
Color & operator|=(Color &lhs, Color rhs)
Definition Color.h:160
Color color(Diagnostic::Level level)
Definition Diagnostic.h:48
Color & operator^=(Color &lhs, Color rhs)
Definition Color.h:165
Color & operator&=(Color &lhs, Color rhs)
Definition Color.h:155
MINI_LLVM_EXPORT bool supportsColor(FILE *stream)
Definition Color.h:61
Codes codes
Definition Color.h:65
ColoredStringView(std::string_view str)
Definition Color.h:67
ColoredStringView(std::string_view str, Codes codes)
Definition Color.h:70
std::bitset< 128 > Codes
Definition Color.h:62
std::string_view str
Definition Color.h:64
Definition Color.h:76
std::bitset< 128 > Codes
Definition Color.h:77
ColoredString(std::string str, Codes codes)
Definition Color.h:85
Codes codes
Definition Color.h:80
std::string str
Definition Color.h:79
ColoredString(std::string str)
Definition Color.h:82
constexpr auto parse(std::format_parse_context &ctx)
Definition Color.h:182
auto format(const mini_llvm::ColoredStringView &str, FormatContext &ctx) const
Definition Color.h:187