-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathTerminalInput.cpp
82 lines (66 loc) · 1.55 KB
/
TerminalInput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <StdInc.h>
#include <linenoise.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <CoreConsole.h>
#include <ServerInstanceBase.h>
using namespace std::chrono_literals;
static InitFunction initFunction([]()
{
linenoiseInstallWindowChangeHandler();
fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance)
{
instance->OnInitialConfiguration.Connect([=]()
{
static fwRefContainer<console::Context> con = instance->GetComponent<console::Context>();
linenoiseSetCompletionCallback([](const char* prefix, linenoiseCompletions* lc)
{
static std::set<std::string> cmds;
cmds.clear();
con->GetCommandManager()->ForAllCommands([&](const std::string& cmd)
{
if (strncmp(cmd.c_str(), prefix, strlen(prefix)) == 0)
{
cmds.insert(cmd);
}
});
for (auto& cmd : cmds)
{
linenoiseAddCompletion(lc, cmd.c_str());
}
});
std::thread([=]()
{
if (!isatty(fileno(stdin)))
{
return;
}
while (true)
{
char* result = linenoise("cfx> ");
// null result?
if (result == nullptr)
{
std::exit(0);
break;
}
// make a string and free
std::string resultStr = result;
free(result);
// handle input
con->AddToBuffer(resultStr);
con->AddToBuffer("\n");
linenoiseHistoryAdd(resultStr.c_str());
// wait until console buffer was processed
while (!con->IsBufferEmpty())
{
std::this_thread::sleep_for(25ms);
}
}
}).detach();
}, 9999);
});
});