feat: add windows-spec console patch
This commit is contained in:
38
src/yycc/windows/console.cpp
Normal file
38
src/yycc/windows/console.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "console.hpp"
|
||||
#if defined(YYCC_OS_WINDOWS) && defined(YYCC_STL_MSSTL)
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "import_guard_head.hpp"
|
||||
#include <Windows.h>
|
||||
#include <io.h>
|
||||
#include "import_guard_tail.hpp"
|
||||
|
||||
namespace yycc::windows::console {
|
||||
|
||||
static ExecResult<void> colorful_fs(FILE* fs) {
|
||||
if (!_isatty(_fileno(fs))) {
|
||||
return std::unexpected(ExecError::NotTty);
|
||||
}
|
||||
|
||||
HANDLE h_output;
|
||||
DWORD dw_mode;
|
||||
|
||||
h_output = (HANDLE) _get_osfhandle(_fileno(fs));
|
||||
if (!GetConsoleMode(h_output, &dw_mode)) {
|
||||
return std::unexpected(ExecError::GetMode);
|
||||
}
|
||||
if (!SetConsoleMode(h_output, dw_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT)) {
|
||||
return std::unexpected(ExecError::SetMode);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ExecResult<void> colorful_console() {
|
||||
return colorful_fs(stdout).and_then([]() { return colorful_fs(stderr); });
|
||||
}
|
||||
|
||||
} // namespace yycc::windows::console
|
||||
|
||||
#endif
|
33
src/yycc/windows/console.hpp
Normal file
33
src/yycc/windows/console.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "../macro/os_detector.hpp"
|
||||
#include "../macro/stl_detector.hpp"
|
||||
#if defined(YYCC_OS_WINDOWS) && defined(YYCC_STL_MSSTL)
|
||||
|
||||
#include <expected>
|
||||
|
||||
/**
|
||||
* @brief The namespace provide patches for Windows console.
|
||||
*/
|
||||
namespace yycc::windows::console {
|
||||
|
||||
/// @brief Error occurs in this module.
|
||||
enum class ExecError {
|
||||
NotTty, ///< Given stream is not TTY.
|
||||
GetMode, ///< Can not get stream mode.
|
||||
SetMode, ///< Can not set stream mode.
|
||||
};
|
||||
|
||||
/// @brief Result type used in this module.
|
||||
template<typename T>
|
||||
using ExecResult = std::expected<T, ExecError>;
|
||||
|
||||
/**
|
||||
* @brief Enable console color support for Windows.
|
||||
* @details This actually is enable virtual console feature for \c stdout and \c stderr.
|
||||
* @return Nothing or error occurs.
|
||||
*/
|
||||
ExecResult<void> colorful_console();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user