2023-02-12 18:08:29 +08:00
|
|
|
#include "TerminalHelper.hpp"
|
2023-02-11 22:23:19 +08:00
|
|
|
#include <VTUtils.hpp>
|
2023-08-26 20:34:51 +08:00
|
|
|
#include <VTEncoding.hpp>
|
2023-02-12 18:08:29 +08:00
|
|
|
#include <cstdarg>
|
2023-08-26 20:34:51 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
2023-02-11 22:23:19 +08:00
|
|
|
|
2024-08-17 20:43:27 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
2023-02-11 22:23:19 +08:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <io.h>
|
2023-02-12 18:08:29 +08:00
|
|
|
#include <fcntl.h>
|
2023-02-11 22:23:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Unvirt {
|
2023-02-12 18:08:29 +08:00
|
|
|
namespace TerminalHelper {
|
2023-02-11 22:23:19 +08:00
|
|
|
|
|
|
|
// all of these functions only works on Windows platform
|
|
|
|
// due to shitty Windows implementations.
|
|
|
|
|
|
|
|
bool EnsureTerminalColor(void) {
|
2024-08-17 20:43:27 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
2023-02-11 22:23:19 +08:00
|
|
|
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) {
|
2024-08-17 20:43:27 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
2023-02-11 22:23:19 +08:00
|
|
|
if (!SetConsoleCP(CP_UTF8)) return false;
|
|
|
|
if (!SetConsoleOutputCP(CP_UTF8)) return false;
|
2023-02-12 18:08:29 +08:00
|
|
|
|
|
|
|
/*_setmode(_fileno(stdout), _O_U8TEXT);*/
|
|
|
|
_setmode(_fileno(stdin), _O_U16TEXT);
|
2023-02-11 22:23:19 +08:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-26 20:34:51 +08:00
|
|
|
void GetCmdLine(std::string& u8cmd) {
|
2023-09-22 22:31:51 +08:00
|
|
|
fputs(UNVIRT_TERMCOL_LIGHT_GREEN(("Unvirt> ")), stdout);
|
2024-08-17 20:43:27 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
2023-08-26 20:34:51 +08:00
|
|
|
std::wstring wcmd;
|
|
|
|
std::getline(std::wcin, wcmd);
|
|
|
|
LibCmo::EncodingHelper::WcharToChar(wcmd, u8cmd, CP_UTF8);
|
|
|
|
#else
|
|
|
|
std::getline(std::cin, u8cmd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-02-11 22:23:19 +08:00
|
|
|
}
|
|
|
|
}
|