mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-06-17 22:23:03 +00:00
Add console history.
This commit is contained in:
@@ -118,14 +118,65 @@ void SetupConsole() {
|
|||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::vector<std::string> ConsoleHistory {};
|
||||||
|
static size_t ConsoleHistoryReadIndex { 0 };
|
||||||
|
|
||||||
|
static inline void ConsoleHistoryAdd(const std::string& cmd) {
|
||||||
|
ConsoleHistory.push_back(cmd);
|
||||||
|
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
||||||
|
}
|
||||||
|
static std::string CompositeInput;
|
||||||
|
static bool CompositeInputExpected { false };
|
||||||
|
|
||||||
|
static void ProcessCompositeInput() {
|
||||||
|
if (memcmp(CompositeInput.data(), std::array<char, 2> { 91, 65 }.data(), 2) == 0) {
|
||||||
|
// UP ARROW
|
||||||
|
if (!ConsoleHistory.empty()) {
|
||||||
|
if (ConsoleHistoryReadIndex != 0) {
|
||||||
|
ConsoleHistoryReadIndex -= 1;
|
||||||
|
}
|
||||||
|
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
||||||
|
}
|
||||||
|
} else if (memcmp(CompositeInput.data(), std::array<char, 2> { 91, 66 }.data(), 2) == 0) {
|
||||||
|
// DOWN ARROW
|
||||||
|
if (!ConsoleHistory.empty()) {
|
||||||
|
if (ConsoleHistoryReadIndex != ConsoleHistory.size() - 1) {
|
||||||
|
ConsoleHistoryReadIndex += 1;
|
||||||
|
CInputBuff = ConsoleHistory.at(ConsoleHistoryReadIndex);
|
||||||
|
} else {
|
||||||
|
CInputBuff = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not composite input, we made a mistake, so lets just add it to the buffer like usual
|
||||||
|
CInputBuff += CompositeInput;
|
||||||
|
}
|
||||||
|
// ensure history doesnt grow too far beyond a max
|
||||||
|
static constexpr size_t MaxHistory = 30;
|
||||||
|
if (ConsoleHistory.size() > 2 * MaxHistory) {
|
||||||
|
std::vector<std::string> NewHistory(ConsoleHistory.begin() + ConsoleHistory.size() - MaxHistory, ConsoleHistory.end());
|
||||||
|
ConsoleHistory = std::move(NewHistory);
|
||||||
|
ConsoleHistoryReadIndex = ConsoleHistory.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[[noreturn]] void ReadCin() {
|
[[noreturn]] void ReadCin() {
|
||||||
DebugPrintTID();
|
DebugPrintTID();
|
||||||
while (true) {
|
while (true) {
|
||||||
int In = _getch();
|
int In = _getch();
|
||||||
//info(std::to_string(In));
|
//info(std::to_string(In));
|
||||||
|
if (CompositeInputExpected) {
|
||||||
|
CompositeInput += In;
|
||||||
|
if (CompositeInput.size() == 2) {
|
||||||
|
CompositeInputExpected = false;
|
||||||
|
ProcessCompositeInput();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (In == 13 || In == '\n') {
|
if (In == 13 || In == '\n') {
|
||||||
if (!CInputBuff.empty()) {
|
if (!CInputBuff.empty()) {
|
||||||
HandleInput(CInputBuff);
|
HandleInput(CInputBuff);
|
||||||
|
ConsoleHistoryAdd(CInputBuff);
|
||||||
CInputBuff.clear();
|
CInputBuff.clear();
|
||||||
}
|
}
|
||||||
} else if (In == 8 || In == 127) {
|
} else if (In == 8 || In == 127) {
|
||||||
@@ -135,6 +186,10 @@ void SetupConsole() {
|
|||||||
CInputBuff = "exit";
|
CInputBuff = "exit";
|
||||||
HandleInput(CInputBuff);
|
HandleInput(CInputBuff);
|
||||||
CInputBuff.clear();
|
CInputBuff.clear();
|
||||||
|
} else if (In == 27) {
|
||||||
|
// escape char, assume stuff follows
|
||||||
|
CompositeInputExpected = true;
|
||||||
|
CompositeInput.clear();
|
||||||
} else if (!isprint(In)) {
|
} else if (!isprint(In)) {
|
||||||
// ignore
|
// ignore
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user