mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2025-08-18 12:45:36 +00:00
Console.cpp: Fix console history crashes and inconsistent behavior
This commit is contained in:
parent
5fb7c459c6
commit
e2611e13e0
@ -18,6 +18,7 @@ typedef unsigned long DWORD, *PDWORD, *LPDWORD;
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <deque>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -40,9 +41,6 @@ void HandleInput(const std::string& cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ProcessOut() {
|
void ProcessOut() {
|
||||||
static size_t len = 2;
|
|
||||||
if (QConsoleOut.empty() && len == CInputBuff.length())
|
|
||||||
return;
|
|
||||||
printf("%c[2K\r", 27);
|
printf("%c[2K\r", 27);
|
||||||
for (const std::string& msg : QConsoleOut)
|
for (const std::string& msg : QConsoleOut)
|
||||||
if (!msg.empty())
|
if (!msg.empty())
|
||||||
@ -51,7 +49,6 @@ void ProcessOut() {
|
|||||||
QConsoleOut.clear();
|
QConsoleOut.clear();
|
||||||
MLock.unlock();
|
MLock.unlock();
|
||||||
std::cout << "> " << CInputBuff << std::flush;
|
std::cout << "> " << CInputBuff << std::flush;
|
||||||
len = CInputBuff.length();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConsoleOut(const std::string& msg) {
|
void ConsoleOut(const std::string& msg) {
|
||||||
@ -125,12 +122,34 @@ void SetupConsole() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<std::string> ConsoleHistory {};
|
static std::vector<std::string> ConsoleHistory {};
|
||||||
|
// buffer used to revert back to what we were writing when we go all the way forward in history
|
||||||
|
static std::string LastInputBuffer {};
|
||||||
static size_t ConsoleHistoryReadIndex { 0 };
|
static size_t ConsoleHistoryReadIndex { 0 };
|
||||||
|
|
||||||
static inline void ConsoleHistoryAdd(const std::string& cmd) {
|
static inline void ConsoleHistoryAdd(const std::string& cmd) {
|
||||||
|
LastInputBuffer.clear();
|
||||||
ConsoleHistory.push_back(cmd);
|
ConsoleHistory.push_back(cmd);
|
||||||
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void ConsoleHistoryEnsureBounds() {
|
||||||
|
if (ConsoleHistoryReadIndex >= ConsoleHistory.size()) {
|
||||||
|
ConsoleHistoryReadIndex = ConsoleHistory.size() - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ConsoleHistoryGoBack() {
|
||||||
|
if (ConsoleHistoryReadIndex > 0) {
|
||||||
|
--ConsoleHistoryReadIndex;
|
||||||
|
ConsoleHistoryEnsureBounds();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ConsoleHistoryGoForward() {
|
||||||
|
++ConsoleHistoryReadIndex;
|
||||||
|
ConsoleHistoryEnsureBounds();
|
||||||
|
}
|
||||||
|
|
||||||
static std::string CompositeInput;
|
static std::string CompositeInput;
|
||||||
static bool CompositeInputExpected { false };
|
static bool CompositeInputExpected { false };
|
||||||
|
|
||||||
@ -142,11 +161,8 @@ static void ProcessCompositeInput() {
|
|||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
|
|
||||||
// UP ARROW
|
// UP ARROW
|
||||||
// info(std::to_string(ConsoleHistoryReadIndex));
|
|
||||||
if (!ConsoleHistory.empty()) {
|
if (!ConsoleHistory.empty()) {
|
||||||
if (ConsoleHistoryReadIndex != 0) {
|
ConsoleHistoryGoBack();
|
||||||
ConsoleHistoryReadIndex -= 1;
|
|
||||||
}
|
|
||||||
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@ -157,12 +173,11 @@ static void ProcessCompositeInput() {
|
|||||||
|
|
||||||
// DOWN ARROW
|
// DOWN ARROW
|
||||||
if (!ConsoleHistory.empty()) {
|
if (!ConsoleHistory.empty()) {
|
||||||
if (ConsoleHistoryReadIndex != ConsoleHistory.size() - 1) {
|
if (ConsoleHistoryReadIndex == ConsoleHistory.size() - 1) {
|
||||||
ConsoleHistoryReadIndex += 1;
|
CInputBuff = LastInputBuffer;
|
||||||
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
|
||||||
} else {
|
} else {
|
||||||
CInputBuff = "";
|
ConsoleHistoryGoForward();
|
||||||
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -172,7 +187,7 @@ static void ProcessCompositeInput() {
|
|||||||
// ensure history doesnt grow too far beyond a max
|
// ensure history doesnt grow too far beyond a max
|
||||||
static constexpr size_t MaxHistory = 10;
|
static constexpr size_t MaxHistory = 10;
|
||||||
if (ConsoleHistory.size() > 2 * MaxHistory) {
|
if (ConsoleHistory.size() > 2 * MaxHistory) {
|
||||||
std::vector<std::string> NewHistory(ConsoleHistory.begin() + ConsoleHistory.size() - MaxHistory, ConsoleHistory.end());
|
decltype(ConsoleHistory) NewHistory(ConsoleHistory.begin() + ConsoleHistory.size() - MaxHistory, ConsoleHistory.end());
|
||||||
ConsoleHistory = std::move(NewHistory);
|
ConsoleHistory = std::move(NewHistory);
|
||||||
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
||||||
}
|
}
|
||||||
@ -233,6 +248,7 @@ void ReadCin() {
|
|||||||
// ignore
|
// ignore
|
||||||
} else {
|
} else {
|
||||||
CInputBuff += char(In);
|
CInputBuff += char(In);
|
||||||
|
LastInputBuffer = CInputBuff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user