ConsoleLib.h->ConsoleLib.hpp ConsoleLib::initConsole()->ConsoleLib::init()
This commit is contained in:
+4
-1
@@ -1,3 +1,6 @@
|
|||||||
build
|
build
|
||||||
.vscode
|
.vscode
|
||||||
example/build
|
example/build
|
||||||
|
.idea
|
||||||
|
cmake-build-debug
|
||||||
|
cmake-build-release
|
||||||
+1
-1
@@ -4,7 +4,7 @@ project(ConsoleLib VERSION 0.1.0 LANGUAGES C CXX)
|
|||||||
option(ConsoleLib_BUILD_EXAMPLES "Build ConsoleLib examples" ON)
|
option(ConsoleLib_BUILD_EXAMPLES "Build ConsoleLib examples" ON)
|
||||||
|
|
||||||
# 静态库:Linux/macOS 生成 libConsoleLib.a,Windows 生成 ConsoleLib.lib
|
# 静态库: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
|
# 共享库:Linux/macOS 生成 libConsoleLib.so,Windows 生成 ConsoleLib.dll
|
||||||
add_library(ConsoleLib_shared SHARED ConsoleLib.cpp)
|
add_library(ConsoleLib_shared SHARED ConsoleLib.cpp)
|
||||||
|
|||||||
+12
-15
@@ -1,4 +1,4 @@
|
|||||||
#include "include/ConsoleLib.h"
|
#include "include/ConsoleLib.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <locale.h>
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@@ -48,7 +47,7 @@ void recordKey(Keycode keycode) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(g_keyMutex);
|
std::lock_guard lock(g_keyMutex);
|
||||||
g_keyTable[static_cast<size_t>(keycode)] = true;
|
g_keyTable[static_cast<size_t>(keycode)] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +56,7 @@ Keycode parseEscapeSequence(const char* buffer, size_t length) {
|
|||||||
return KEY_UNKNOWN;
|
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
|
// Unix/Linux VT 序列:ESC [ X / ESC O X
|
||||||
if (first == 0x1B) {
|
if (first == 0x1B) {
|
||||||
@@ -145,13 +144,13 @@ void restoreConsoleMode();
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
static HANDLE g_hStdin = INVALID_HANDLE_VALUE;
|
static auto g_hStdin = INVALID_HANDLE_VALUE;
|
||||||
static HANDLE g_hStdout = INVALID_HANDLE_VALUE;
|
static auto g_hStdout = INVALID_HANDLE_VALUE;
|
||||||
static DWORD g_dwOriginalInMode = 0;
|
static DWORD g_dwOriginalInMode = 0;
|
||||||
static DWORD g_dwOriginalOutMode = 0;
|
static DWORD g_dwOriginalOutMode = 0;
|
||||||
static bool g_bInitialized = false;
|
static bool g_bInitialized = false;
|
||||||
|
|
||||||
void initConsole() {
|
void init() {
|
||||||
if (g_bInitialized) {
|
if (g_bInitialized) {
|
||||||
startInputThread();
|
startInputThread();
|
||||||
return;
|
return;
|
||||||
@@ -192,14 +191,14 @@ void initConsole() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clearConsole() {
|
void clearConsole() {
|
||||||
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
if (hOut == INVALID_HANDLE_VALUE) {
|
if (hOut == INVALID_HANDLE_VALUE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||||
DWORD count;
|
DWORD count;
|
||||||
COORD homeCoords = {0, 0};
|
const COORD homeCoords = {0, 0};
|
||||||
|
|
||||||
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
|
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
|
||||||
return;
|
return;
|
||||||
@@ -216,7 +215,7 @@ void RawPrint(std::string_view text) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD bytesToWrite = static_cast<DWORD>(text.size());
|
const DWORD bytesToWrite = text.size();
|
||||||
DWORD bytesWritten = 0;
|
DWORD bytesWritten = 0;
|
||||||
WriteFile(g_hStdout, text.data(), bytesToWrite, &bytesWritten, nullptr);
|
WriteFile(g_hStdout, text.data(), bytesToWrite, &bytesWritten, nullptr);
|
||||||
}
|
}
|
||||||
@@ -243,8 +242,7 @@ char getinput() {
|
|||||||
return buffer[0];
|
return buffer[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
Keycode key = parseEscapeSequence(buffer, charsRead);
|
if (const Keycode key = parseEscapeSequence(buffer, charsRead); key != KEY_UNKNOWN) {
|
||||||
if (key != KEY_UNKNOWN) {
|
|
||||||
return 0; // 特殊键请使用 getConsoleKeyInput() / isKeyPressed()
|
return 0; // 特殊键请使用 getConsoleKeyInput() / isKeyPressed()
|
||||||
}
|
}
|
||||||
return buffer[0];
|
return buffer[0];
|
||||||
@@ -269,8 +267,7 @@ void restoreConsoleMode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void startInputThread() {
|
void startInputThread() {
|
||||||
bool expected = false;
|
if (bool expected = false; !g_inputRunning.compare_exchange_strong(expected, true)) {
|
||||||
if (!g_inputRunning.compare_exchange_strong(expected, true)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +311,7 @@ void startInputThread() {
|
|||||||
static struct termios original_termios;
|
static struct termios original_termios;
|
||||||
static bool raw_mode_enabled = false;
|
static bool raw_mode_enabled = false;
|
||||||
|
|
||||||
void initConsole() {
|
void init() {
|
||||||
if (!raw_mode_enabled) {
|
if (!raw_mode_enabled) {
|
||||||
struct termios raw;
|
struct termios raw;
|
||||||
if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
|
if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#include <ConsoleLib.h>
|
#include <ConsoleLib.hpp>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <string>
|
#include <string>
|
||||||
using namespace ConsoleLib;
|
using namespace ConsoleLib;
|
||||||
signed main() {
|
signed main() {
|
||||||
initConsole();
|
init();
|
||||||
clearConsole();
|
clearConsole();
|
||||||
printToConsole("HelloWorld!\r\n");
|
printToConsole("HelloWorld!\r\n");
|
||||||
printToConsole("Red", 0xff0000);
|
printToConsole("Red", 0xff0000);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <ConsoleLib.h>
|
#include <ConsoleLib.hpp>
|
||||||
|
|
||||||
using namespace ConsoleLib;
|
using namespace ConsoleLib;
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ constexpr uint8_t DEFAULT_CONSOLE_INPUT_FREQUENCY = 50;
|
|||||||
typedef std::uint32_t Color;
|
typedef std::uint32_t Color;
|
||||||
|
|
||||||
namespace ConsoleLib {
|
namespace ConsoleLib {
|
||||||
void initConsole();
|
void init();
|
||||||
void stopInputThread();
|
void stopInputThread();
|
||||||
void restoreConsoleMode();
|
void restoreConsoleMode();
|
||||||
|
|
||||||
Reference in New Issue
Block a user