Address review comments.
This commit is contained in:
parent
4c949ccc65
commit
ded9951701
@ -191,11 +191,11 @@ enum class color : uint32_t {
|
|||||||
yellow_green = 0x9ACD32 // rgb(154,205,50)
|
yellow_green = 0x9ACD32 // rgb(154,205,50)
|
||||||
}; // enum class color
|
}; // enum class color
|
||||||
|
|
||||||
enum class emphasis : uint32_t {
|
enum class emphasis : uint8_t {
|
||||||
bold = 1 << 1,
|
bold = 1,
|
||||||
italic = 1 << 3,
|
italic = 1 << 1,
|
||||||
underline = 1 << 4,
|
underline = 1 << 2,
|
||||||
strikethrough = 1 << 9
|
strikethrough = 1 << 3
|
||||||
}; // enum class emphasis
|
}; // enum class emphasis
|
||||||
|
|
||||||
// rgb is a struct for red, green and blue colors.
|
// rgb is a struct for red, green and blue colors.
|
||||||
@ -216,15 +216,15 @@ struct rgb {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Experimental text formatting support.
|
// Experimental text formatting support.
|
||||||
class text_format {
|
class text_style {
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR_DECL text_format(emphasis em) FMT_NOEXCEPT
|
FMT_CONSTEXPR_DECL text_style(emphasis em) FMT_NOEXCEPT
|
||||||
: set_foreground_color(),
|
: set_foreground_color(),
|
||||||
set_background_color(),
|
set_background_color(),
|
||||||
ems(em) {}
|
ems(em) {}
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL
|
FMT_CONSTEXPR_DECL
|
||||||
text_format &operator|=(const text_format &rhs) FMT_NOEXCEPT {
|
text_style &operator|=(const text_style &rhs) FMT_NOEXCEPT {
|
||||||
if (!set_foreground_color) {
|
if (!set_foreground_color) {
|
||||||
set_foreground_color = rhs.set_foreground_color;
|
set_foreground_color = rhs.set_foreground_color;
|
||||||
foreground_color = rhs.foreground_color;
|
foreground_color = rhs.foreground_color;
|
||||||
@ -243,17 +243,18 @@ public:
|
|||||||
background_color.b |= rhs.background_color.b;
|
background_color.b |= rhs.background_color.b;
|
||||||
}
|
}
|
||||||
|
|
||||||
ems = (emphasis)((uint32_t)ems | (uint32_t)rhs.ems);
|
ems = static_cast<emphasis>(static_cast<uint8_t>(ems) |
|
||||||
|
static_cast<uint8_t>(rhs.ems));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FMT_CONSTEXPR_DECL
|
friend FMT_CONSTEXPR_DECL
|
||||||
text_format operator|(text_format lhs, const text_format &rhs) FMT_NOEXCEPT {
|
text_style operator|(text_style lhs, const text_style &rhs) FMT_NOEXCEPT {
|
||||||
return lhs |= rhs;
|
return lhs |= rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL
|
FMT_CONSTEXPR_DECL
|
||||||
text_format &operator&=(const text_format &rhs) FMT_NOEXCEPT {
|
text_style &operator&=(const text_style &rhs) FMT_NOEXCEPT {
|
||||||
if (!set_foreground_color) {
|
if (!set_foreground_color) {
|
||||||
set_foreground_color = rhs.set_foreground_color;
|
set_foreground_color = rhs.set_foreground_color;
|
||||||
foreground_color = rhs.foreground_color;
|
foreground_color = rhs.foreground_color;
|
||||||
@ -272,12 +273,13 @@ public:
|
|||||||
background_color.b &= rhs.background_color.b;
|
background_color.b &= rhs.background_color.b;
|
||||||
}
|
}
|
||||||
|
|
||||||
ems = (emphasis)((uint32_t)ems & (uint32_t)rhs.ems);
|
ems = static_cast<emphasis>(static_cast<uint8_t>(ems) &
|
||||||
|
static_cast<uint8_t>(rhs.ems));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FMT_CONSTEXPR_DECL
|
friend FMT_CONSTEXPR_DECL
|
||||||
text_format operator&(text_format lhs, const text_format &rhs) FMT_NOEXCEPT {
|
text_style operator&(text_style lhs, const text_style &rhs) FMT_NOEXCEPT {
|
||||||
return lhs &= rhs;
|
return lhs &= rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,7 +302,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FMT_CONSTEXPR_DECL text_format(bool is_foreground,
|
FMT_CONSTEXPR_DECL text_style(bool is_foreground,
|
||||||
rgb text_color) FMT_NOEXCEPT
|
rgb text_color) FMT_NOEXCEPT
|
||||||
: set_foreground_color(), set_background_color(), ems() {
|
: set_foreground_color(), set_background_color(), ems() {
|
||||||
if (is_foreground) {
|
if (is_foreground) {
|
||||||
@ -313,8 +315,8 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FMT_CONSTEXPR_DECL text_format fg(rgb foreground) FMT_NOEXCEPT;
|
friend FMT_CONSTEXPR_DECL text_style fg(rgb foreground) FMT_NOEXCEPT;
|
||||||
friend FMT_CONSTEXPR_DECL text_format bg(rgb background) FMT_NOEXCEPT;
|
friend FMT_CONSTEXPR_DECL text_style bg(rgb background) FMT_NOEXCEPT;
|
||||||
|
|
||||||
rgb foreground_color;
|
rgb foreground_color;
|
||||||
rgb background_color;
|
rgb background_color;
|
||||||
@ -323,16 +325,16 @@ private:
|
|||||||
emphasis ems;
|
emphasis ems;
|
||||||
};
|
};
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL text_format fg(rgb foreground) FMT_NOEXCEPT {
|
FMT_CONSTEXPR_DECL text_style fg(rgb foreground) FMT_NOEXCEPT {
|
||||||
return text_format(/*is_foreground=*/true, foreground);
|
return text_style(/*is_foreground=*/true, foreground);
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL text_format bg(rgb background) FMT_NOEXCEPT {
|
FMT_CONSTEXPR_DECL text_style bg(rgb background) FMT_NOEXCEPT {
|
||||||
return text_format(/*is_foreground=*/false, background);
|
return text_style(/*is_foreground=*/false, background);
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL text_format operator|(emphasis lhs, emphasis rhs) FMT_NOEXCEPT {
|
FMT_CONSTEXPR_DECL text_style operator|(emphasis lhs, emphasis rhs) FMT_NOEXCEPT {
|
||||||
return text_format(lhs) | rhs;
|
return text_style(lhs) | rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
@ -424,7 +426,7 @@ inline void reset_color<wchar_t>(FILE *stream) FMT_NOEXCEPT {
|
|||||||
|
|
||||||
template <
|
template <
|
||||||
typename S, typename Char = typename internal::char_t<S>::type>
|
typename S, typename Char = typename internal::char_t<S>::type>
|
||||||
void vprint_text_format(const text_format &tf, const S &format,
|
void vprint(const text_style &tf, const S &format,
|
||||||
basic_format_args<typename buffer_context<Char>::type> args) {
|
basic_format_args<typename buffer_context<Char>::type> args) {
|
||||||
internal::fputs<Char>(internal::make_emphasis<Char>(tf.get_emphasis()), stdout);
|
internal::fputs<Char>(internal::make_emphasis<Char>(tf.get_emphasis()), stdout);
|
||||||
if (tf.has_foreground())
|
if (tf.has_foreground())
|
||||||
@ -441,17 +443,17 @@ void vprint_text_format(const text_format &tf, const S &format,
|
|||||||
Formats a string and prints it to stdout using ANSI escape sequences to
|
Formats a string and prints it to stdout using ANSI escape sequences to
|
||||||
specify text formatting.
|
specify text formatting.
|
||||||
Example:
|
Example:
|
||||||
fmt::print(fmt::emphasis::bold | fmt::color::red,
|
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
|
||||||
"Elapsed time: {0:.2f} seconds", 1.23);
|
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
*/
|
*/
|
||||||
template <typename String, typename... Args>
|
template <typename String, typename... Args>
|
||||||
typename std::enable_if<internal::is_string<String>::value>::type
|
typename std::enable_if<internal::is_string<String>::value>::type
|
||||||
print(const text_format &tf, const String &format_str, const Args & ... args) {
|
print(const text_style &tf, const String &format_str, const Args & ... args) {
|
||||||
internal::check_format_string<Args...>(format_str);
|
internal::check_format_string<Args...>(format_str);
|
||||||
typedef typename internal::char_t<String>::type char_t;
|
typedef typename internal::char_t<String>::type char_t;
|
||||||
typedef typename buffer_context<char_t>::type context_t;
|
typedef typename buffer_context<char_t>::type context_t;
|
||||||
format_arg_store<context_t, Args...> as{args...};
|
format_arg_store<context_t, Args...> as{args...};
|
||||||
vprint_text_format(tf, format_str, basic_format_args<context_t>(as));
|
vprint_text_style(tf, format_str, basic_format_args<context_t>(as));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -25,9 +25,9 @@
|
|||||||
#undef max
|
#undef max
|
||||||
|
|
||||||
#if FMT_HAS_CPP_ATTRIBUTE(noreturn)
|
#if FMT_HAS_CPP_ATTRIBUTE(noreturn)
|
||||||
#define FMT_NORETURN [[noreturn]]
|
# define FMT_NORETURN [[noreturn]]
|
||||||
#else
|
#else
|
||||||
#define FMT_NORETURN
|
# define FMT_NORETURN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using fmt::internal::fp;
|
using fmt::internal::fp;
|
||||||
@ -108,8 +108,10 @@ TEST(FPTest, Grisu2FormatCompilesWithNonIEEEDouble) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct ValueExtractor : fmt::internal::function<T> {
|
struct ValueExtractor: fmt::internal::function<T> {
|
||||||
T operator()(T value) { return value; }
|
T operator()(T value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
FMT_NORETURN T operator()(U) {
|
FMT_NORETURN T operator()(U) {
|
||||||
@ -176,8 +178,8 @@ TEST(FormatTest, FormatErrorCode) {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
fmt::memory_buffer buffer;
|
fmt::memory_buffer buffer;
|
||||||
std::string prefix(fmt::inline_buffer_size - msg.size() - sep.size() + 1,
|
std::string prefix(
|
||||||
'x');
|
fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
|
||||||
fmt::format_error_code(buffer, 42, prefix);
|
fmt::format_error_code(buffer, 42, prefix);
|
||||||
EXPECT_EQ(msg, to_string(buffer));
|
EXPECT_EQ(msg, to_string(buffer));
|
||||||
}
|
}
|
||||||
@ -186,7 +188,8 @@ TEST(FormatTest, FormatErrorCode) {
|
|||||||
// Test maximum buffer size.
|
// Test maximum buffer size.
|
||||||
msg = fmt::format("error {}", codes[i]);
|
msg = fmt::format("error {}", codes[i]);
|
||||||
fmt::memory_buffer buffer;
|
fmt::memory_buffer buffer;
|
||||||
std::string prefix(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
|
std::string prefix(
|
||||||
|
fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
|
||||||
fmt::format_error_code(buffer, codes[i], prefix);
|
fmt::format_error_code(buffer, codes[i], prefix);
|
||||||
EXPECT_EQ(prefix + sep + msg, to_string(buffer));
|
EXPECT_EQ(prefix + sep + msg, to_string(buffer));
|
||||||
std::size_t size = fmt::inline_buffer_size;
|
std::size_t size = fmt::inline_buffer_size;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user