Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement styled arguments #2793

Merged
merged 11 commits into from
Mar 8, 2022
90 changes: 90 additions & 0 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ template <typename Char> inline void reset_color(buffer<Char>& buffer) {
buffer.append(reset_color.begin(), reset_color.end());
}

template <typename T> struct styled_arg {
const T& value;
text_style style;
};

template <typename Char>
void vformat_to(buffer<Char>& buf, const text_style& ts,
basic_string_view<Char> format_str,
Expand Down Expand Up @@ -630,6 +635,91 @@ inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
}

template <typename T, typename Char>
struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
template <typename FormatContext>
auto format(const detail::styled_arg<T>& arg, FormatContext& ctx) const
-> decltype(ctx.out()) {
const auto& ts = arg.style;
const auto& value = arg.value;
auto out = ctx.out();

using detail::get_buffer;
auto&& buf = get_buffer<Char>(out);
rbrugo marked this conversation as resolved.
Show resolved Hide resolved

bool has_style = false;
if (ts.has_emphasis()) {
has_style = true;
auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
buf.append(emphasis.begin(), emphasis.end());
}
if (ts.has_foreground()) {
has_style = true;
auto foreground =
detail::make_foreground_color<Char>(ts.get_foreground());
buf.append(foreground.begin(), foreground.end());
}
if (ts.has_background()) {
has_style = true;
auto background =
detail::make_background_color<Char>(ts.get_background());
buf.append(background.begin(), background.end());
}
out = formatter<T, Char>::format(value, ctx);
if (has_style) detail::reset_color<Char>(buf);
return out;
}
};

/**
\rst
Returns an argument that will be formatted using ANSI escape sequences,
to be used in a formatting function.

**Example**::

fmt::print("Elapsed time: {s:.2f} seconds",
fmt::styled(1.23, fmt::fg(fmt::colors::green) |
fmt::bg(fmt::color::blue))); \endrst
rbrugo marked this conversation as resolved.
Show resolved Hide resolved
*/
template <typename T>
FMT_CONSTEXPR auto styled(const T& value, text_style ts = {})
rbrugo marked this conversation as resolved.
Show resolved Hide resolved
-> detail::styled_arg<remove_cvref_t<T>> {
return detail::styled_arg<remove_cvref_t<T>>{value, ts};
}

/**
\rst
Returns an argument surrounded by the ANSI escape sequences of the color,
to be used in a formatting function.

**Example**::

fmt::print("Elapsed time: {s:.2f} seconds", fmt::styled(1.23,
fmt::colors::green)); \endrst
*/
template <typename T>
FMT_CONSTEXPR auto styled(const T& value, detail::color_type color)
-> detail::styled_arg<remove_cvref_t<T>> {
return detail::styled_arg<remove_cvref_t<T>>{value, fg(color)};
}
rbrugo marked this conversation as resolved.
Show resolved Hide resolved

/**
\rst
Returns an argument associated with an emphasis, to be used
in a formatting function.

**Example**::

fmt::print("Elapsed time: {s:.2f} seconds", fmt::styled(1.23,
fmt::emphasis::italic)); \endrst
*/
template <typename T>
FMT_CONSTEXPR auto styled(const T& value, emphasis em)
-> detail::styled_arg<remove_cvref_t<T>> {
return detail::styled_arg<remove_cvref_t<T>>{value, text_style(em)};
}
rbrugo marked this conversation as resolved.
Show resolved Hide resolved

FMT_MODULE_EXPORT_END
FMT_END_NAMESPACE

Expand Down
6 changes: 6 additions & 0 deletions test/color-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ TEST(color_test, format) {
"\x1b[105mtbmagenta\x1b[0m");
EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "{}", "foo"),
"\x1b[31mfoo\x1b[0m");
EXPECT_EQ(fmt::format("{}{}", fmt::styled("red", fmt::color::red),
fmt::styled("bold", fmt::emphasis::bold)),
"\x1b[38;2;255;000;000mred\x1b[0m\x1b[1mbold\x1b[0m");
EXPECT_EQ(fmt::format("{}", fmt::styled("bar", fg(fmt::color::blue) |
fmt::emphasis::underline)),
"\x1b[4m\x1b[38;2;000;000;255mbar\x1b[0m");
}

TEST(color_test, format_to) {
Expand Down