1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-21 11:30:30 -04:00
nix/src/libutil/fmt.hh

170 lines
3.7 KiB
C++
Raw Normal View History

2020-04-21 15:25:41 -04:00
#pragma once
#include <boost/format.hpp>
#include <string>
#include <regex>
2020-04-21 15:25:41 -04:00
#include "ansicolor.hh"
namespace nix {
/* Inherit some names from other namespaces for convenience. */
using boost::format;
/* A variadic template that does nothing. Useful to call a function
for all variadic arguments but ignoring the result. */
struct nop { template<typename... T> nop(T...) {} };
struct FormatOrString
{
std::string s;
FormatOrString(std::string s) : s(std::move(s)) { };
2020-04-21 15:25:41 -04:00
template<class F>
FormatOrString(const F & f) : s(f.str()) { };
FormatOrString(const char * s) : s(s) { };
};
/* A helper for formatting strings. fmt(format, a_0, ..., a_n) is
equivalent to boost::format(format) % a_0 % ... %
... a_n. However, fmt(s) is equivalent to s (so no %-expansion
takes place). */
template<class F>
inline void formatHelper(F & f)
{
}
template<class F, typename T, typename... Args>
inline void formatHelper(F & f, const T & x, const Args & ... args)
{
formatHelper(f % x, args...);
}
inline std::string fmt(const std::string & s)
{
return s;
}
inline std::string fmt(const char * s)
{
return s;
}
inline std::string fmt(const FormatOrString & fs)
{
return fs.s;
}
template<typename... Args>
inline std::string fmt(const std::string & fs, const Args & ... args)
{
boost::format f(fs);
f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
formatHelper(f, args...);
return f.str();
}
// -----------------------------------------------------------------------------
// format function for hints in errors. same as fmt, except templated values
// are always in yellow.
template <class T>
2020-05-04 18:19:20 -04:00
struct yellowtxt
2020-04-21 15:25:41 -04:00
{
2020-05-06 16:07:20 -04:00
yellowtxt(const T &s) : value(s) {}
2020-10-07 10:33:19 -04:00
const T & value;
2020-04-21 15:25:41 -04:00
};
template <class T>
2020-10-07 10:33:19 -04:00
std::ostream & operator<<(std::ostream & out, const yellowtxt<T> & y)
2020-04-21 15:25:41 -04:00
{
2021-09-14 04:38:10 -04:00
return out << ANSI_WARNING << y.value << ANSI_NORMAL;
2020-04-21 15:25:41 -04:00
}
2020-05-04 18:19:20 -04:00
template <class T>
struct normaltxt
{
2020-10-07 10:33:19 -04:00
normaltxt(const T & s) : value(s) {}
const T & value;
2020-05-04 18:19:20 -04:00
};
template <class T>
2020-10-07 10:33:19 -04:00
std::ostream & operator<<(std::ostream & out, const normaltxt<T> & y)
2020-05-04 18:19:20 -04:00
{
return out << ANSI_NORMAL << y.value;
}
2020-04-21 15:25:41 -04:00
class hintformat
{
public:
hintformat(const std::string & format) : fmt(format)
2020-04-21 15:25:41 -04:00
{
2020-10-07 10:33:19 -04:00
fmt.exceptions(boost::io::all_error_bits ^
2020-06-19 17:29:19 -04:00
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
2020-04-21 15:25:41 -04:00
}
2020-10-07 10:33:19 -04:00
hintformat(const hintformat & hf)
: fmt(hf.fmt)
{ }
hintformat(format && fmt)
: fmt(std::move(fmt))
{ }
2020-04-21 15:25:41 -04:00
template<class T>
2020-10-07 10:33:19 -04:00
hintformat & operator%(const T & value)
2020-04-21 15:25:41 -04:00
{
2020-05-04 18:19:20 -04:00
fmt % yellowtxt(value);
2020-04-21 15:25:41 -04:00
return *this;
}
template<class T>
2020-10-07 10:33:19 -04:00
hintformat & operator%(const normaltxt<T> & value)
{
fmt % value.value;
return *this;
}
2020-04-21 15:25:41 -04:00
std::string str() const
{
return fmt.str();
}
private:
format fmt;
};
2020-10-07 10:33:19 -04:00
std::ostream & operator<<(std::ostream & os, const hintformat & hf);
2020-04-21 15:25:41 -04:00
template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
{
hintformat f(fs);
formatHelper(f, args...);
return f;
}
inline hintformat hintfmt(std::string plain_string)
2020-06-19 17:29:19 -04:00
{
// we won't be receiving any args in this case, so just print the original string
return hintfmt("%s", normaltxt(plain_string));
2020-06-19 17:29:19 -04:00
}
/* Highlight all the given matches in the given string `s` by wrapping
them between `prefix` and `postfix`.
If some matches overlap, then their union will be wrapped rather
than the individual matches. */
std::string hiliteMatches(
std::string_view s,
std::vector<std::smatch> matches,
std::string_view prefix,
std::string_view postfix);
2020-04-21 15:25:41 -04:00
}