Make fmt::println support color
This commit is contained in:
parent
e406ddbfaf
commit
a19700aba6
@ -460,10 +460,11 @@ void vformat_to(buffer<Char>& buf, const text_style& ts,
|
|||||||
FMT_END_DETAIL_NAMESPACE
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
inline void vprint(std::FILE* f, const text_style& ts, string_view fmt,
|
inline void vprint(std::FILE* f, const text_style& ts, string_view fmt,
|
||||||
format_args args) {
|
bool new_line, format_args args) {
|
||||||
// Legacy wide streams are not supported.
|
// Legacy wide streams are not supported.
|
||||||
auto buf = memory_buffer();
|
auto buf = memory_buffer();
|
||||||
detail::vformat_to(buf, ts, fmt, args);
|
detail::vformat_to(buf, ts, fmt, args);
|
||||||
|
if (new_line) buf.push_back('\n');
|
||||||
if (detail::is_utf8()) {
|
if (detail::is_utf8()) {
|
||||||
detail::print(f, string_view(buf.begin(), buf.size()));
|
detail::print(f, string_view(buf.begin(), buf.size()));
|
||||||
return;
|
return;
|
||||||
@ -489,7 +490,26 @@ template <typename S, typename... Args,
|
|||||||
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
void print(std::FILE* f, const text_style& ts, const S& format_str,
|
void print(std::FILE* f, const text_style& ts, const S& format_str,
|
||||||
const Args&... args) {
|
const Args&... args) {
|
||||||
vprint(f, ts, format_str,
|
vprint(f, ts, format_str, false,
|
||||||
|
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Formats a string and prints it to the specified file stream using ANSI
|
||||||
|
escape sequences to specify text formatting followed by a newline.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::println(fmt::emphasis::bold | fg(fmt::color::red),
|
||||||
|
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename S, typename... Args,
|
||||||
|
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
|
void println(std::FILE* f, const text_style& ts, const S& format_str,
|
||||||
|
const Args&... args) {
|
||||||
|
vprint(f, ts, format_str, true,
|
||||||
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
|
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -510,6 +530,23 @@ void print(const text_style& ts, const S& format_str, const Args&... args) {
|
|||||||
return print(stdout, ts, format_str, args...);
|
return print(stdout, ts, format_str, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Formats a string and prints it to stdout using ANSI escape sequences to
|
||||||
|
specify text formatting followed by a newline.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::println(fmt::emphasis::bold | fg(fmt::color::red),
|
||||||
|
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename S, typename... Args,
|
||||||
|
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
|
void println(const text_style& ts, const S& format_str, const Args&... args) {
|
||||||
|
return println(stdout, ts, format_str, args...);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline std::basic_string<Char> vformat(
|
inline std::basic_string<Char> vformat(
|
||||||
const text_style& ts, const S& format_str,
|
const text_style& ts, const S& format_str,
|
||||||
|
|||||||
@ -69,4 +69,6 @@ TEST(color_test, format_to) {
|
|||||||
TEST(color_test, print) {
|
TEST(color_test, print) {
|
||||||
EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
|
EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
|
||||||
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
|
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
|
||||||
|
EXPECT_WRITE(stdout, fmt::println(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
|
||||||
|
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user