-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_curses_console_commands_main.cpp
71 lines (57 loc) · 1.5 KB
/
test_curses_console_commands_main.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
#include "CursesConsole.hpp"
#include "Commands.hpp"
#include <iostream>
#include <thread>
using namespace Commands;
using Commands::Arg;
CursesConsole console;
COMMAND(int, print, (std::vector<Arg> args)) {
std::cout << "print: ";
for(auto& a : args) {
std::cout << a.to_string();
std::cout << " ";
}
std::cout << std::endl;
return 0;
}
COMMAND(void, info, (std::string str)) {
console.SetInfoString(str);
}
COMMAND(void, clear, ()) {
console.ClearLog();
}
int main() {
console.StartCurses();
console.SetCommandPrefix(">> ");
// calls code complete handler after each keystroke (but doesn't actually autocomplete)
console.SetAutoCodeComplete(true);
// this will display in info string possible commands to be completed, and will also autocomplete
console.SetCodeCompleteHandler( [](std::string cmd, int cursor) {
const int max_commands_to_complete = 5;
std::vector<std::string> vec = Command::Search(cmd, cursor, max_commands_to_complete);
if(!vec.empty()) {
std::string info = "";
for(auto& s : vec) {
info += s + " | ";
}
console.SetTmpInfoString("\n------\n" + info);
}
return Command::Complete(cmd, cursor);
});
while(1) {
console.SetInfoString(
"enter:"
"\n\tprint helloworld"
"\n\tprint 3 (+ 45 2 (- 7 2) 8 (* 2 5)) (/ 10 2.5)"
"\n\tprint (/f 10.6 2.57)"
"\n\tclear"
);
std::string s = console.Input();
try {
Command::Execute(s);
} catch(std::exception &e) {
std::cout << e.what() << "\n";
}
}
console.StopCurses();
}