1
0

feat: add windows-spec console patch

This commit is contained in:
2025-08-22 21:51:32 +08:00
parent 9e994dd4f0
commit 4bfba6f243
7 changed files with 91 additions and 39 deletions

View 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

View 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