Fix Console on Unix, adapt console behavior to that of a traditional

console, add Assert.h, add clang-format file with modified WebKit style
This commit is contained in:
Lion Kortlepel
2020-11-03 09:00:42 +01:00
parent 13e79e407c
commit eead954bf9
7 changed files with 306 additions and 178 deletions

View File

@@ -8,6 +8,8 @@
#include <conio.h>
#else // *nix
typedef unsigned long DWORD, *PDWORD, *LPDWORD;
#include <termios.h>
#include <unistd.h>
#endif // __WIN32
#include "Logger.h"
#include <iostream>
@@ -20,6 +22,7 @@ std::string CInputBuff;
std::mutex MLock;
Lua* LuaConsole;
void HandleInput(const std::string& cmd){
std::cout << std::endl;
if (cmd == "exit") {
exit(0);
}else LuaConsole->Execute(cmd);
@@ -28,13 +31,12 @@ void HandleInput(const std::string& cmd){
void ProcessOut(){
static size_t len = 2;
if(QConsoleOut.empty() && len == CInputBuff.length())return;
printf("%c[2K\r", 27);
for(const std::string& msg : QConsoleOut)
if(!msg.empty())std::cout << msg;
MLock.lock();
QConsoleOut.clear();
MLock.unlock();
std::cout << "> " << CInputBuff;
std::cout << "> " << CInputBuff << std::flush;
len = CInputBuff.length();
}
@@ -50,6 +52,32 @@ void ConsoleOut(const std::string& msg){
ProcessOut();
}
}
#ifndef __WIN32
static int _getch()
{
char buf = 0;
struct termios old;
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~unsigned(ICANON);
old.c_lflag &= ~unsigned(ECHO);
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// no echo printf("%c\n", buf);
return buf;
}
#endif // __WIN32
void SetupConsole(){
#ifdef __WIN32
DWORD outMode = 0;
@@ -71,19 +99,28 @@ void SetupConsole(){
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(GetLastError());
}
#else
#endif // __WIN32
}
[[noreturn]] void ReadCin(){
while (true){
int In = getchar();
if (In == 13) {
int In = _getch();
if (In == 13 || In == '\n') {
if(!CInputBuff.empty()) {
HandleInput(CInputBuff);
CInputBuff.clear();
}
}else if(In == 8){
if(!CInputBuff.empty())CInputBuff.pop_back();
}else CInputBuff += char(In);
} else if (In == 4) {
CInputBuff = "exit";
HandleInput(CInputBuff);
CInputBuff.clear();
}else {
printf("%c[2K\r", 27);
CInputBuff += char(In);
}
}
}
void ConsoleInit(){