ConsoleLib.h->ConsoleLib.hpp ConsoleLib::initConsole()->ConsoleLib::init()

This commit is contained in:
CupWater
2026-08-16 14:10:56 +08:00
parent 5f51a52ca6
commit 91ff1324aa
6 changed files with 21 additions and 21 deletions
+4 -1
View File
@@ -1,3 +1,6 @@
build
.vscode
example/build
example/build
.idea
cmake-build-debug
cmake-build-release
+1 -1
View File
@@ -4,7 +4,7 @@ project(ConsoleLib VERSION 0.1.0 LANGUAGES C CXX)
option(ConsoleLib_BUILD_EXAMPLES "Build ConsoleLib examples" ON)
# 静态库:Linux/macOS 生成 libConsoleLib.aWindows 生成 ConsoleLib.lib
add_library(ConsoleLib STATIC ConsoleLib.cpp include/ConsoleLib.h)
add_library(ConsoleLib STATIC ConsoleLib.cpp include/ConsoleLib.hpp)
# 共享库:Linux/macOS 生成 libConsoleLib.soWindows 生成 ConsoleLib.dll
add_library(ConsoleLib_shared SHARED ConsoleLib.cpp)
+12 -15
View File
@@ -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) {
+2 -2
View File
@@ -1,10 +1,10 @@
#include <ConsoleLib.h>
#include <ConsoleLib.hpp>
#include <atomic>
#include <string>
using namespace ConsoleLib;
signed main() {
initConsole();
init();
clearConsole();
printToConsole("HelloWorld!\r\n");
printToConsole("Red", 0xff0000);
+1 -1
View File
@@ -1,4 +1,4 @@
#include <ConsoleLib.h>
#include <ConsoleLib.hpp>
using namespace ConsoleLib;
@@ -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();