Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
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>
7
#include "
nix/util/ansicolor.hh
"
8
9
namespace
nix {
10
23
template
<
class
F>
24
inline
void
formatHelper(F & f)
25
{
26
}
27
28
template
<
class
F,
typename
T,
typename
... Args>
29
inline
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
40
inline
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
67
inline
std::string
fmt
(
const
std::string & s)
68
{
69
return
s;
70
}
71
72
inline
std::string fmt(std::string_view s)
73
{
74
return
std::string(s);
75
}
76
77
inline
std::string
fmt
(
const
char
* s)
78
{
79
return
s;
80
}
81
82
template
<
typename
...
Args
>
83
inline
std::string
fmt
(
const
std::string & fs,
const
Args
&... args)
84
{
85
boost::format f(fs);
86
setExceptions
(f);
87
formatHelper(f, args...);
88
return
f.str();
89
}
90
98
template
<
class
T>
99
struct
Magenta
100
{
101
Magenta(
const
T & s)
102
: value(s)
103
{
104
}
105
106
const
T & value;
107
};
108
114
struct
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
129
template
<
class
T>
130
std::ostream & operator<<(std::ostream & out,
const
Magenta<T>
& y)
131
{
132
return
out << ANSI_WARNING << y.value << ANSI_NORMAL;
133
}
134
135
inline
std::ostream & operator<<(std::ostream & out,
const
PathFmt & y)
136
{
137
return
out <<
"\""
<< y.value <<
"\""
;
138
}
139
147
template
<
class
T>
148
struct
Uncolored
149
{
150
Uncolored(
const
T & s)
151
: value(s)
152
{
153
}
154
155
const
T & value;
156
};
157
158
template
<
class
T>
159
std::ostream & operator<<(std::ostream & out,
const
Uncolored<T>
& y)
160
{
161
return
out << ANSI_NORMAL << y.value;
162
}
163
168
class
HintFmt
169
{
170
private
:
171
boost::format fmt;
172
173
public
:
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
244
std::ostream & operator<<(std::ostream & os,
const
HintFmt
& hf);
245
246
}
// namespace nix
ansicolor.hh
Some ANSI escape sequences.
nix::Args
Definition
args.hh:29
nix::HintFmt
Definition
fmt.hh:169
nix::HintFmt::HintFmt
HintFmt(const std::string &format, const Args &... args)
Definition
fmt.hh:192
nix::HintFmt::operator%
HintFmt & operator%(const HintFmt &value)
Definition
fmt.hh:230
nix::HintFmt::HintFmt
HintFmt(const std::string &literal)
Definition
fmt.hh:178
nix::fmt
std::string fmt(const std::string &s)
Definition
fmt.hh:67
nix::setExceptions
void setExceptions(boost::format &fmt)
Definition
fmt.hh:40
nix::Magenta
Definition
fmt.hh:100
nix::Uncolored
Definition
fmt.hh:149
libutil
include
nix
util
fmt.hh
Generated by
1.18.0