Repository: fmtlib/fmt
File: include/fmt/format-inl.h (C/C++)

Code before:
    return;
  }
  FMT_CATCH(...) {}
  format_error_code(out, error_code, message);
}

FMT_FUNC void report_system_error(int error_code,
                                  const char* message) noexcept {
  report_error(format_system_error, error_code, message);
}

FMT_FUNC std::string vformat(string_view fmt, format_args args) {
  // Don't optimize the "{}" case to keep the binary size small and because it
  // can be better optimized in fmt::format anyway.
  auto buffer = memory_buffer();
  detail::vformat_to(buffer, fmt, args);
  return to_string(buffer);
}

namespace detail {
#ifdef _WIN32
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW(  //
    void*, const void*, dword, dword*, void*);

FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) {
  auto fd = _fileno(f);
  if (_isatty(fd)) {
    detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
    auto written = detail::dword();
    if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
                              u16.c_str(), static_cast<uint32_t>(u16.size()),
                              &written, nullptr)) {
      return true;
    }
  }
  // We return false if the file descriptor was not TTY, or it was but
  // SetConsoleW failed which can happen if the output has been redirected to
  // NUL. In both cases when we return false, we should attempt to do regular
  // write via fwrite or std::ostream::write.
  return false;
}
#endif

FMT_FUNC void print(std::FILE* f, string_view text) {
#ifdef _WIN32
  if (write_console_on_windows(f, text)) return;
#endif
  detail::fwrite_fully(text.data(), 1, text.size(), f);
}
}  // namespace detail

Review comment (suggestion):
Let's rename it to just `write_console` since it's obvious that this function is only used on Windows from context.

Code after:
    return;
  }
  FMT_CATCH(...) {}
  format_error_code(out, error_code, message);
}

FMT_FUNC void report_system_error(int error_code,
                                  const char* message) noexcept {
  report_error(format_system_error, error_code, message);
}

FMT_FUNC std::string vformat(string_view fmt, format_args args) {
  // Don't optimize the "{}" case to keep the binary size small and because it
  // can be better optimized in fmt::format anyway.
  auto buffer = memory_buffer();
  detail::vformat_to(buffer, fmt, args);
  return to_string(buffer);
}

namespace detail {
#ifdef _WIN32
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW(  //
    void*, const void*, dword, dword*, void*);

FMT_FUNC bool write_console(std::FILE* f, string_view text) {
  auto fd = _fileno(f);
  if (_isatty(fd)) {
    detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
    auto written = detail::dword();
    if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
                              u16.c_str(), static_cast<uint32_t>(u16.size()),
                              &written, nullptr)) {
      return true;
    }
  }
  // We return false if the file descriptor was not TTY, or it was but
  // SetConsoleW failed which can happen if the output has been redirected to
  // NUL. In both cases when we return false, we should attempt to do regular
  // write via fwrite or std::ostream::write.
  return false;
}
#endif

FMT_FUNC void print(std::FILE* f, string_view text) {
#ifdef _WIN32
  if (write_console(f, text)) return;
#endif
  detail::fwrite_fully(text.data(), 1, text.size(), f);
}
}  // namespace detail