ConsoleLib.h->ConsoleLib.hpp ConsoleLib::initConsole()->ConsoleLib::init()
This commit is contained in:
+12
-15
@@ -1,4 +1,4 @@
|
||||
#include "include/ConsoleLib.h"
|
||||
#include "include/ConsoleLib.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <thread>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <locale.h>
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
@@ -48,7 +47,7 @@ void recordKey(Keycode keycode) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_keyMutex);
|
||||
std::lock_guard lock(g_keyMutex);
|
||||
g_keyTable[static_cast<size_t>(keycode)] = true;
|
||||
}
|
||||
|
||||
@@ -57,7 +56,7 @@ Keycode parseEscapeSequence(const char* buffer, size_t length) {
|
||||
return KEY_UNKNOWN;
|
||||
}
|
||||
|
||||
const unsigned char first = static_cast<unsigned char>(buffer[0]);
|
||||
const auto first = static_cast<unsigned char>(buffer[0]);
|
||||
|
||||
// Unix/Linux VT 序列:ESC [ X / ESC O X
|
||||
if (first == 0x1B) {
|
||||
@@ -145,13 +144,13 @@ void restoreConsoleMode();
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
static HANDLE g_hStdin = INVALID_HANDLE_VALUE;
|
||||
static HANDLE g_hStdout = INVALID_HANDLE_VALUE;
|
||||
static auto g_hStdin = INVALID_HANDLE_VALUE;
|
||||
static auto g_hStdout = INVALID_HANDLE_VALUE;
|
||||
static DWORD g_dwOriginalInMode = 0;
|
||||
static DWORD g_dwOriginalOutMode = 0;
|
||||
static bool g_bInitialized = false;
|
||||
|
||||
void initConsole() {
|
||||
void init() {
|
||||
if (g_bInitialized) {
|
||||
startInputThread();
|
||||
return;
|
||||
@@ -192,14 +191,14 @@ void initConsole() {
|
||||
}
|
||||
|
||||
void clearConsole() {
|
||||
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (hOut == INVALID_HANDLE_VALUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
DWORD count;
|
||||
COORD homeCoords = {0, 0};
|
||||
const COORD homeCoords = {0, 0};
|
||||
|
||||
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
|
||||
return;
|
||||
@@ -216,7 +215,7 @@ void RawPrint(std::string_view text) {
|
||||
return;
|
||||
}
|
||||
|
||||
DWORD bytesToWrite = static_cast<DWORD>(text.size());
|
||||
const DWORD bytesToWrite = text.size();
|
||||
DWORD bytesWritten = 0;
|
||||
WriteFile(g_hStdout, text.data(), bytesToWrite, &bytesWritten, nullptr);
|
||||
}
|
||||
@@ -243,8 +242,7 @@ char getinput() {
|
||||
return buffer[0];
|
||||
}
|
||||
|
||||
Keycode key = parseEscapeSequence(buffer, charsRead);
|
||||
if (key != KEY_UNKNOWN) {
|
||||
if (const Keycode key = parseEscapeSequence(buffer, charsRead); key != KEY_UNKNOWN) {
|
||||
return 0; // 特殊键请使用 getConsoleKeyInput() / isKeyPressed()
|
||||
}
|
||||
return buffer[0];
|
||||
@@ -269,8 +267,7 @@ void restoreConsoleMode() {
|
||||
}
|
||||
|
||||
void startInputThread() {
|
||||
bool expected = false;
|
||||
if (!g_inputRunning.compare_exchange_strong(expected, true)) {
|
||||
if (bool expected = false; !g_inputRunning.compare_exchange_strong(expected, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -314,7 +311,7 @@ void startInputThread() {
|
||||
static struct termios original_termios;
|
||||
static bool raw_mode_enabled = false;
|
||||
|
||||
void initConsole() {
|
||||
void init() {
|
||||
if (!raw_mode_enabled) {
|
||||
struct termios raw;
|
||||
if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
|
||||
|
||||
Reference in New Issue
Block a user