Refactor detail::print() by splitting into two functions.
The part with SetConsoleW is a separate function, without fwrite().
This commit is contained in:
parent
c48be439f1
commit
0cbc004949
@ -1473,17 +1473,13 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) {
|
|||||||
return to_string(buffer);
|
return to_string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
#ifdef _WIN32
|
||||||
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
|
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
|
||||||
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
|
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
|
||||||
void*, const void*, dword, dword*, void*);
|
void*, const void*, dword, dword*, void*);
|
||||||
} // namespace detail
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace detail {
|
FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) {
|
||||||
FMT_FUNC void print(std::FILE* f, string_view text) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
auto fd = _fileno(f);
|
auto fd = _fileno(f);
|
||||||
if (_isatty(fd)) {
|
if (_isatty(fd)) {
|
||||||
detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
|
detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
|
||||||
@ -1491,11 +1487,20 @@ FMT_FUNC void print(std::FILE* f, string_view text) {
|
|||||||
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
|
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
|
||||||
u16.c_str(), static_cast<uint32_t>(u16.size()),
|
u16.c_str(), static_cast<uint32_t>(u16.size()),
|
||||||
&written, nullptr)) {
|
&written, nullptr)) {
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
// Fallback to fwrite on failure. It can happen if the output has been
|
|
||||||
// redirected to NUL.
|
|
||||||
}
|
}
|
||||||
|
// 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
|
#endif
|
||||||
detail::fwrite_fully(text.data(), 1, text.size(), f);
|
detail::fwrite_fully(text.data(), 1, text.size(), f);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -921,6 +921,7 @@ struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
|
|||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
bool write_console_on_windows(std::FILE* f, string_view text);
|
||||||
FMT_API void print(std::FILE*, string_view);
|
FMT_API void print(std::FILE*, string_view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user