diff --git a/.gitignore b/.gitignore index c00a89a..c7278d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ build .vscode -example/build \ No newline at end of file +example/build +.idea +cmake-build-debug +cmake-build-release \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 36c5402..c17ec8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(ConsoleLib VERSION 0.1.0 LANGUAGES C CXX) option(ConsoleLib_BUILD_EXAMPLES "Build ConsoleLib examples" ON) # 静态库:Linux/macOS 生成 libConsoleLib.a,Windows 生成 ConsoleLib.lib -add_library(ConsoleLib STATIC ConsoleLib.cpp include/ConsoleLib.h) +add_library(ConsoleLib STATIC ConsoleLib.cpp include/ConsoleLib.hpp) # 共享库:Linux/macOS 生成 libConsoleLib.so,Windows 生成 ConsoleLib.dll add_library(ConsoleLib_shared SHARED ConsoleLib.cpp) diff --git a/ConsoleLib.cpp b/ConsoleLib.cpp index 3530fac..a989394 100644 --- a/ConsoleLib.cpp +++ b/ConsoleLib.cpp @@ -1,4 +1,4 @@ -#include "include/ConsoleLib.h" +#include "include/ConsoleLib.hpp" #include #include @@ -9,7 +9,6 @@ #include #ifdef _WIN32 -#include #include #include #include @@ -48,7 +47,7 @@ void recordKey(Keycode keycode) { return; } - std::lock_guard lock(g_keyMutex); + std::lock_guard lock(g_keyMutex); g_keyTable[static_cast(keycode)] = true; } @@ -57,7 +56,7 @@ Keycode parseEscapeSequence(const char* buffer, size_t length) { return KEY_UNKNOWN; } - const unsigned char first = static_cast(buffer[0]); + const auto first = static_cast(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(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) { diff --git a/example/helloworld/main.cpp b/example/helloworld/main.cpp index 6809e72..d41d9df 100644 --- a/example/helloworld/main.cpp +++ b/example/helloworld/main.cpp @@ -1,10 +1,10 @@ -#include +#include #include #include using namespace ConsoleLib; signed main() { - initConsole(); + init(); clearConsole(); printToConsole("HelloWorld!\r\n"); printToConsole("Red", 0xff0000); diff --git a/example/keyinput/main.cpp b/example/keyinput/main.cpp index 4075efe..f2e947d 100644 --- a/example/keyinput/main.cpp +++ b/example/keyinput/main.cpp @@ -1,4 +1,4 @@ -#include +#include using namespace ConsoleLib; diff --git a/include/ConsoleLib.h b/include/ConsoleLib.hpp similarity index 98% rename from include/ConsoleLib.h rename to include/ConsoleLib.hpp index 3ad49b5..052df1f 100644 --- a/include/ConsoleLib.h +++ b/include/ConsoleLib.hpp @@ -76,7 +76,7 @@ constexpr uint8_t DEFAULT_CONSOLE_INPUT_FREQUENCY = 50; typedef std::uint32_t Color; namespace ConsoleLib { -void initConsole(); +void init(); void stopInputThread(); void restoreConsoleMode();