Nix 2.26.3
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 "ansicolor.hh"
7
8
9namespace nix {
10
23template<class F>
24inline void formatHelper(F & f)
25{ }
26
27template<class F, typename T, typename... Args>
28inline void formatHelper(F & f, const T & x, const Args & ... args)
29{
30 // Interpolate one argument and then recurse.
31 formatHelper(f % x, args...);
32}
33
37inline void setExceptions(boost::format & fmt)
38{
39 fmt.exceptions(
40 boost::io::all_error_bits ^
41 boost::io::too_many_args_bit ^
42 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) : value(s) {}
102 const T & value;
103};
104
105template <class T>
106std::ostream & operator<<(std::ostream & out, const Magenta<T> & y)
107{
108 return out << ANSI_WARNING << y.value << ANSI_NORMAL;
109}
110
118template <class T>
119struct Uncolored
120{
121 Uncolored(const T & s) : value(s) {}
122 const T & value;
123};
124
125template <class T>
126std::ostream & operator<<(std::ostream & out, const Uncolored<T> & y)
127{
128 return out << ANSI_NORMAL << y.value;
129}
130
136{
137private:
138 boost::format fmt;
139
140public:
145 HintFmt(const std::string & literal)
146 : HintFmt("%s", Uncolored(literal))
147 { }
148
149 static HintFmt fromFormatString(const std::string & format) {
150 return HintFmt(boost::format(format));
151 }
152
156 template<typename... Args>
157 HintFmt(const std::string & format, const Args & ... args)
158 : HintFmt(boost::format(format), args...)
159 { }
160
161 HintFmt(const HintFmt & hf)
162 : fmt(hf.fmt)
163 { }
164
165 template<typename... Args>
166 HintFmt(boost::format && fmt, const Args & ... args)
167 : fmt(std::move(fmt))
168 {
169 setExceptions(fmt);
170 formatHelper(*this, args...);
171 }
172
173 template<class T>
174 HintFmt & operator%(const T & value)
175 {
176 fmt % Magenta(value);
177 return *this;
178 }
179
180 template<class T>
181 HintFmt & operator%(const Uncolored<T> & value)
182 {
183 fmt % value.value;
184 return *this;
185 }
186
187 HintFmt & operator=(HintFmt const & rhs) = default;
188
189 std::string str() const
190 {
191 return fmt.str();
192 }
193};
194
195std::ostream & operator<<(std::ostream & os, const HintFmt & hf);
196
197}
Some ANSI escape sequences.
Definition args.hh:28
Definition fmt.hh:136
HintFmt(const std::string &format, const Args &... args)
Definition fmt.hh:157
HintFmt(const std::string &literal)
Definition fmt.hh:145
void formatHelper(F &f)
Definition fmt.hh:24
std::string fmt(const std::string &s)
Definition fmt.hh:67
void setExceptions(boost::format &fmt)
Definition fmt.hh:37
Magenta(const T &s)
Definition lexer.l:491
boost::format f(fs)
formatHelper(f % x, args...)
return s
Definition lexer.l:459
T x
Definition lexer.l:2648
const T & value
Definition lexer.l:492
std::vector< Expr * > args
Definition lexer.l:6126
Definition fmt.hh:100
Definition fmt.hh:120