fix Chinese input problem on Windows console

main
hkr04 2025-03-23 17:09:19 +08:00
parent f307e88e78
commit ec0d299833
2 changed files with 84 additions and 4 deletions

View File

@ -26,6 +26,43 @@ static void sigint_handler(int signo) {
} }
#endif #endif
static bool readline_utf8(std::string & line, bool multiline_input) {
#if defined(_WIN32)
std::wstring wline;
if (!std::getline(std::wcin, wline)) {
// Input stream is bad or EOF received
line.clear();
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
return false;
}
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);
line.resize(size_needed);
WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);
#else
if (!std::getline(std::cin, line)) {
// Input stream is bad or EOF received
line.clear();
return false;
}
#endif
if (!line.empty()) {
char last = line.back();
if (last == '/') { // Always return control on '/' symbol
line.pop_back();
return false;
}
if (last == '\\') { // '\\' changes the default action
line.pop_back();
multiline_input = !multiline_input;
}
}
line += '\n';
// By default, continue input if multiline_input is set
return multiline_input;
}
int main() { int main() {
// ctrl+C handling // ctrl+C handling
@ -41,6 +78,9 @@ int main() {
return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false; return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
}; };
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true); SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
_setmode(_fileno(stdin), _O_WTEXT); // wide character input mode
#endif #endif
} }
@ -54,8 +94,8 @@ int main() {
std::cout << "Enter your prompt (or 'exit' to quit): "; std::cout << "Enter your prompt (or 'exit' to quit): ";
} }
std::string prompt; std::string prompt;
std::getline(std::cin, prompt); readline_utf8(prompt, false);
if (prompt == "exit") { if (prompt == "exit" || prompt == "exit\n") {
logger->info("Goodbye!"); logger->info("Goodbye!");
break; break;
} }

View File

@ -26,6 +26,43 @@ static void sigint_handler(int signo) {
} }
#endif #endif
static bool readline_utf8(std::string & line, bool multiline_input) {
#if defined(_WIN32)
std::wstring wline;
if (!std::getline(std::wcin, wline)) {
// Input stream is bad or EOF received
line.clear();
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
return false;
}
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);
line.resize(size_needed);
WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);
#else
if (!std::getline(std::cin, line)) {
// Input stream is bad or EOF received
line.clear();
return false;
}
#endif
if (!line.empty()) {
char last = line.back();
if (last == '/') { // Always return control on '/' symbol
line.pop_back();
return false;
}
if (last == '\\') { // '\\' changes the default action
line.pop_back();
multiline_input = !multiline_input;
}
}
line += '\n';
// By default, continue input if multiline_input is set
return multiline_input;
}
int main() { int main() {
// ctrl+C handling // ctrl+C handling
@ -41,6 +78,9 @@ int main() {
return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false; return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
}; };
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true); SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
_setmode(_fileno(stdin), _O_WTEXT); // wide character input mode
#endif #endif
} }
@ -73,8 +113,8 @@ int main() {
} }
std::string prompt; std::string prompt;
std::getline(std::cin, prompt); readline_utf8(prompt, false);
if (prompt == "exit") { if (prompt == "exit" || prompt == "exit\n") {
logger->info("Goodbye!"); logger->info("Goodbye!");
break; break;
} }