Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
fmt.hh
Go to the documentation of this file.
1#pragma once
3
4#include <boost/format.hpp>
5#include <string>
6#include <filesystem>
8
9namespace nix {
10
23template<class F>
24inline void formatHelper(F & f)
25{
26}
27
28template<class F, typename T, typename... Args>
29inline void formatHelper(F & f, const T & x, const Args &... args)
30{
31 static_assert(!std::is_same_v<std::remove_cvref_t<T>, std::filesystem::path>);
32 static_assert(!(std::is_same_v<std::remove_cvref_t<Args>, std::filesystem::path> || ...));
33 // Interpolate one argument and then recurse.
34 formatHelper(f % x, args...);
35}
36
40inline void setExceptions(boost::format & fmt)
41{
42 fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit ^ boost::io::too_few_args_bit);
43}
44
67inline std::string fmt(const std::string & s)
68{
69 return s;
70}
71
72inline std::string fmt(std::string_view s)
73{
74 return std::string(s);
75}
76
77inline std::string fmt(const char * s)
78{
79 return s;
80}
81
82template<typename... Args>
83inline std::string fmt(const std::string & fs, const Args &... args)
84{
85 boost::format f(fs);
87 formatHelper(f, args...);
88 return f.str();
89}
90
98template<class T>
99struct Magenta
100{
101 Magenta(const T & s)
102 : value(s)
103 {
104 }
105
106 const T & value;
107};
108
114struct PathFmt
115{
116 explicit PathFmt(const std::filesystem::path & p)
117 {
118#ifdef _WIN32
119 auto s = p.u8string();
120 value = std::string(reinterpret_cast<const char *>(s.data()), s.size());
121#else
122 value = p.string();
123#endif
124 }
125
126 std::string value;
127};
128
129template<class T>
130std::ostream & operator<<(std::ostream & out, const Magenta<T> & y)
131{
132 return out << ANSI_WARNING << y.value << ANSI_NORMAL;
133}
134
135inline std::ostream & operator<<(std::ostream & out, const PathFmt & y)
136{
137 return out << "\"" << y.value << "\"";
138}
139
147template<class T>
148struct Uncolored
149{
150 Uncolored(const T & s)
151 : value(s)
152 {
153 }
154
155 const T & value;
156};
157
158template<class T>
159std::ostream & operator<<(std::ostream & out, const Uncolored<T> & y)
160{
161 return out << ANSI_NORMAL << y.value;
162}
163
169{
170private:
171 boost::format fmt;
172
173public:
178 HintFmt(const std::string & literal)
179 : HintFmt("%s", Uncolored(literal))
180 {
181 }
182
183 static HintFmt fromFormatString(const std::string & format)
184 {
185 return HintFmt(boost::format(format));
186 }
187
191 template<typename... Args>
192 HintFmt(const std::string & format, const Args &... args)
193 : HintFmt(boost::format(format), args...)
194 {
195 }
196
197 HintFmt(const HintFmt & hf)
198 : fmt(hf.fmt)
199 {
200 }
201
202 template<typename... Args>
203 HintFmt(boost::format && fmt, const Args &... args)
204 : fmt(std::move(fmt))
205 {
206 static_assert(!(std::is_same_v<std::remove_cvref_t<Args>, std::filesystem::path> || ...));
207 setExceptions(fmt);
208 formatHelper(*this, args...);
209 }
210
211 HintFmt & operator%(const std::filesystem::path & value) = delete;
212
213 template<class T>
214 HintFmt & operator%(const T & value)
215 {
216 fmt % Magenta(value);
217 return *this;
218 }
219
220 template<class T>
221 HintFmt & operator%(const Uncolored<T> & value)
222 {
223 fmt % value.value;
224 return *this;
225 }
226
230 HintFmt & operator%(const HintFmt & value)
231 {
232 fmt % value.str();
233 return *this;
234 }
235
236 HintFmt & operator=(const HintFmt & rhs) = default;
237
238 std::string str() const
239 {
240 return fmt.str();
241 }
242};
243
244std::ostream & operator<<(std::ostream & os, const HintFmt & hf);
245
246} // namespace nix
Some ANSI escape sequences.
Definition args.hh:29
Definition fmt.hh:169
HintFmt(const std::string &format, const Args &... args)
Definition fmt.hh:192
HintFmt & operator%(const HintFmt &value)
Definition fmt.hh:230
HintFmt(const std::string &literal)
Definition fmt.hh:178
std::string fmt(const std::string &s)
Definition fmt.hh:67
void setExceptions(boost::format &fmt)
Definition fmt.hh:40
Definition fmt.hh:100
Definition fmt.hh:149