libcmo21/Unvirt/TerminalHelper.cpp
yyc12345 e682a87d25 refactor: refactor project
- rename LIBCMO_DISABLE_COPY_MOVE -> YYCC_DEL_CLS_COPY_MOVE and LIBCMO_DEFAULT_COPY_MOVE -> YYCC_DEF_CLS_COPY_MOVE.
- fix Vector declaration generator. throw exception when operator[] face invalid index, instead of do fallback.
- rename VTAll.hpp -> VTInternal.hpp and VYUserAll -> VTAll.hpp for easy to understand.
- fix project name error in Doxygen template.
- replace all LIBCMO_OS_WIN32 to YYCC_OS == YYCC_OS_WINDOWS.
- fix some compile error (involving utf8 encoding) but not the final result.
- use correct way to include std-image library (use <> instead of "")
- finish documentation for VTUtils.hpp and VTEncoding.hpp.
2024-08-17 20:43:27 +08:00

61 lines
1.4 KiB
C++

#include "TerminalHelper.hpp"
#include <VTUtils.hpp>
#include <VTEncoding.hpp>
#include <cstdarg>
#include <iostream>
#include <cstdio>
#if YYCC_OS == YYCC_OS_WINDOWS
#include <Windows.h>
#include <cstdio>
#include <io.h>
#include <fcntl.h>
#endif
namespace Unvirt {
namespace TerminalHelper {
// all of these functions only works on Windows platform
// due to shitty Windows implementations.
bool EnsureTerminalColor(void) {
#if YYCC_OS == YYCC_OS_WINDOWS
if (_isatty(_fileno(stdout))) {
HANDLE h_output;
DWORD dw_mode;
h_output = (HANDLE)_get_osfhandle(_fileno(stdout));
if (!GetConsoleMode(h_output, &dw_mode)) return false;
if (!SetConsoleMode(h_output, dw_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return false;
return true;
} else return false;
#else
return true;
#endif
}
bool EnsureTerminalEncoding(void) {
#if YYCC_OS == YYCC_OS_WINDOWS
if (!SetConsoleCP(CP_UTF8)) return false;
if (!SetConsoleOutputCP(CP_UTF8)) return false;
/*_setmode(_fileno(stdout), _O_U8TEXT);*/
_setmode(_fileno(stdin), _O_U16TEXT);
#endif
return true;
}
void GetCmdLine(std::string& u8cmd) {
fputs(UNVIRT_TERMCOL_LIGHT_GREEN(("Unvirt> ")), stdout);
#if YYCC_OS == YYCC_OS_WINDOWS
std::wstring wcmd;
std::getline(std::wcin, wcmd);
LibCmo::EncodingHelper::WcharToChar(wcmd, u8cmd, CP_UTF8);
#else
std::getline(std::cin, u8cmd);
#endif
}
}
}