Skip to content

Commit

Permalink
Add color format_to overloads
Browse files Browse the repository at this point in the history
* Fix variable size basic_memory_buffer colorization
* Fix an unused arguments warning on GCC that blocks the CI otherwise
* Ref #1842
* Ref #1593
  • Loading branch information
Naios committed Sep 4, 2020
1 parent f8e00a0 commit 9d44ba4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
49 changes: 46 additions & 3 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,16 +463,16 @@ template <> inline void reset_color<wchar_t>(FILE* stream) FMT_NOEXCEPT {
}

template <typename Char>
inline void reset_color(basic_memory_buffer<Char>& buffer) FMT_NOEXCEPT {
inline void reset_color(buffer<Char>& buffer) FMT_NOEXCEPT {
const char* begin = data::reset_color;
const char* end = begin + sizeof(data::reset_color) - 1;
buffer.append(begin, end);
}

template <typename Char>
void vformat_to(basic_memory_buffer<Char>& buf, const text_style& ts,
void vformat_to(buffer<Char>& buf, const text_style& ts,
basic_string_view<Char> format_str,
basic_format_args<buffer_context<Char>> args) {
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
bool has_style = false;
if (ts.has_emphasis()) {
has_style = true;
Expand Down Expand Up @@ -563,6 +563,49 @@ inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
fmt::make_args_checked<Args...>(format_str, args...));
}

/** Formats a string with the given text_style and writes the output to ``out``.
*/
template <typename OutputIt, typename S, typename Char = char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt>::value)>
OutputIt vformat_to(
OutputIt out, const text_style& ts, const S& format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
decltype(detail::get_buffer<Char>(out)) buf(detail::get_buffer_init(out));
detail::vformat_to(buf, ts, to_string_view(format_str), args);
return detail::get_iterator(buf);
}

/**
\rst
Formats arguments with the given text_style, writes the result to the output
iterator ``out`` and returns the iterator past the end of the output range.
**Example**::
std::vector<char> out;
fmt::format_to(std::back_inserter(out),
fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
\endrst
*/
template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt>::value&&
detail::is_string<S>::value)>
inline OutputIt format_to(OutputIt out, const text_style& ts,
const S& format_str, Args&&... args) {
return vformat_to(out, ts, to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...));
}

template <typename S, typename... Args, size_t SIZE = inline_buffer_size,
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
inline typename buffer_context<Char>::iterator format_to(
basic_memory_buffer<Char, SIZE>& buf, const text_style& ts,
const S& format_str, Args&&... args) {
detail::vformat_to(buf, ts, to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str, args...));
return detail::buffer_appender<Char>(buf);
}

FMT_END_NAMESPACE

#endif // FMT_COLOR_H_
22 changes: 22 additions & 0 deletions test/color-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include "fmt/color.h"

#include <iterator>
#include <string>
#include <utility>

#include "gtest-extra.h"

TEST(ColorsTest, ColorsPrint) {
Expand Down Expand Up @@ -84,3 +88,21 @@ TEST(ColorsTest, Format) {
EXPECT_EQ(fmt::format(fg(fmt::terminal_color::red), "{}", "foo"),
"\x1b[31mfoo\x1b[0m");
}

TEST(ColorsTest, FormatToOutAcceptsTextStyle) {
fmt::text_style const ts = fg(fmt::rgb(255, 20, 30));
std::string out;
fmt::format_to(std::back_inserter(out), ts, "rgb(255,20,30){}{}{}", 1, 2, 3);

EXPECT_EQ(fmt::to_string(out),
"\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
}

TEST(ColorsTest, FormatToAcceptsTextStyle) {
fmt::text_style const ts = fg(fmt::rgb(255, 20, 30));
fmt::basic_memory_buffer<char> out;
fmt::format_to(out, ts, "rgb(255,20,30){}{}{}", 1, 2, 3);

EXPECT_EQ(fmt::to_string(out),
"\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
}
8 changes: 7 additions & 1 deletion test/gtest-extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ std::string read(fmt::file& f, size_t count);
read(file, fmt::string_view(expected_content).size()))

#else
# define EXPECT_WRITE(file, statement, expected_output) SUCCEED()
# define EXPECT_WRITE(file, statement, expected_output) \
do { \
(void)(file); \
(void)(statement); \
(void)(expected_output); \
SUCCEED(); \
} while (false)
#endif // FMT_USE_FCNTL

template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> {
Expand Down

0 comments on commit 9d44ba4

Please sign in to comment.