Repository: fmtlib/fmt
File: include/fmt/color.h (C/C++)

Code before:
  if (result < 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
}

template <> inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) {
  int result = std::fputws(chars, stream);
  if (result < 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
}

template <typename Char> inline void reset_color(FILE* stream) {
  fputs("\x1b[0m", stream);
}

template <> inline void reset_color<wchar_t>(FILE* stream) {
  fputs(L"\x1b[0m", stream);
}

template <typename Char> inline void reset_color(buffer<Char>& buffer) {
  auto reset_color = string_view("\x1b[0m");
  buffer.append(reset_color.begin(), reset_color.end());
}

template <typename Arg> struct styled_arg {
  FMT_CONSTEXPR styled_arg(const Arg& format_argument, text_style format_style)
      : argument(format_argument), style(format_style) {}

  const Arg& argument;
  text_style style;
};

template <typename Char>
void vformat_to(buffer<Char>& buf, const text_style& ts,
                basic_string_view<Char> format_str,
                basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  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());
  }

Review comment (suggestion):
I don't think the constructor is needed because you could use aggregate initialization `styled_arg{...}` instead.

Code after:
  if (result < 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
}

template <> inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) {
  int result = std::fputws(chars, stream);
  if (result < 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
}

template <typename Char> inline void reset_color(FILE* stream) {
  fputs("\x1b[0m", stream);
}

template <> inline void reset_color<wchar_t>(FILE* stream) {
  fputs(L"\x1b[0m", stream);
}

template <typename Char> inline void reset_color(buffer<Char>& buffer) {
  auto reset_color = string_view("\x1b[0m");
  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,
                basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  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());
  }
  detail::vformat_to(buf, format_str, args, {});
  if (has_style) detail::reset_color<Char>(buf);
}