-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
200 lines (185 loc) · 5.93 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "executor.hpp"
#include "formattor.hpp"
#include "scpi.hpp"
#include "yaml.hpp"
#include "fancy_cin.hpp"
#include <fstream>
void show_help() {
std::cout << R"(==== Help ====
NORMAL mode:
Ctrl-D: Quit and save
Ctrl-Q: Quit and discard
Ctrl-\: Quit immediately
Ctrl-S: Save but don't quit
Ctrl-C: Interrupt
r/Home: Reverse everything
c/Enter: Start/Continue execution
s/Right: Step-in
n/Down: Step-over
f/End: Step-out
Backscape: Reverse step-in
Up: Reverse step-over
Left: Reverse step-out
Space: Refresh display
INSERT mode:
Ctrl-C: Abort this step
Enter: Save and proceed
Ctrl-Q: Quit program immediately
)" << std::flush;
}
enum class normal {
QUIT_SAVE,
QUIT_NO_SAVE,
SAVE_NO_QUIT,
REFRESH,
RUN,
};
normal process_normal_mode(executor &exc) {
std::cout << "\n\nNORMAL mode, command requested" << std::endl;
input_again:
switch (auto ui = fancy::event_loop(); ui.kind) {
case fancy::SIGNAL:
std::cout << "To quit & save, press Ctrl-D" << std::endl;
goto input_again;
case fancy::CTRL_CHAR:
switch (ui.chr) {
case 'D':
return normal::QUIT_SAVE;
case 'S':
return normal::SAVE_NO_QUIT;
case 'Q':
return normal::QUIT_NO_SAVE;
case 'M': // Enter
exc.start();
return normal::RUN;
case 'H': // Backscape
exc.reverse_step_in();
return normal::REFRESH;
default:
std::cout << "Unexpected command Ctrl-" << ui.chr << std::endl;
goto input_again;
}
case fancy::CHAR:
switch (ui.chr) {
case 'r':
exc.reset();
return normal::REFRESH;
case 'c':
exc.start();
return normal::RUN;
case 's':
exc.step_in();
return normal::RUN;
case 'n':
exc.step_over();
return normal::RUN;
case 'f':
exc.step_out();
return normal::RUN;
case ' ':
return normal::REFRESH;
case 'h':
case '?':
show_help();
goto input_again;
default:
std::cout << "Unexpected command " << ui.chr << " (" << (int)ui.chr << ")" << std::endl;
goto input_again;
}
case fancy::ESCAPE:
switch (ui.escape) {
case fancy::escape_t::LEFT:
exc.reverse_step_out();
return normal::REFRESH;
case fancy::escape_t::RIGHT:
exc.step_in();
return normal::RUN;
case fancy::escape_t::UP:
exc.reverse_step_over();
return normal::REFRESH;
case fancy::escape_t::DOWN:
exc.step_over();
return normal::RUN;
case fancy::escape_t::HOME:
exc.reset();
return normal::REFRESH;
case fancy::escape_t::END:
exc.step_out();
return normal::RUN;
case fancy::escape_t::F1:
show_help();
goto input_again;
default:
std::cout << "Unexpected escape command " << (int)ui.escape << std::endl;
goto input_again;
}
case fancy::EOT:
return normal::QUIT_SAVE;
default:
std::cout << "Unexpected kind " << (int)ui.kind << std::endl;
goto input_again;
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cerr << "Usage: lab <profile.yaml> <data.json>\n";
return 1;
}
auto str = read_file(argv[1]);
auto tree = parse_string(str);
tree.resolve();
chnl_map chnls;
tree["channels"] >> chnls;
profile_t profile;
tree["steps"] >> profile.steps;
step::resolve_parent(profile.steps);
if (std::ifstream fin{ argv[2] }; fin.good()) {
fin >> profile;
} else {
std::cout << "Warning: Cannot open the file " << argv[2] << " for reading\n";
std::cout << "\e[1m\e[35mPlease indicate the name of the new profile:\e[0m" << std::endl;
std::getline(std::cin, profile.name);
}
formattor fmt{ &profile };
executor exc{ &chnls, &profile };
fancy::init();
auto save = [&]{
if (std::ofstream fout{ argv[2] }; fout.good()) {
fout << profile;
} else {
std::cout << "Warning: Cannot open the file " << argv[2] << " for writing, writing to stdout instead\n\n";
std::cout << profile;
}
};
fmt.show();
while (true) {
switch (process_normal_mode(exc)) {
case normal::SAVE_NO_QUIT:
save();
break;
case normal::QUIT_SAVE:
save();
case normal::QUIT_NO_SAVE:
return 0;
case normal::RUN:
try {
while (exc.run()) {
if (auto ui = fancy::check_sig(); ui.kind) {
std::cout << "Interrupted" << std::endl;
break;
}
fmt.show();
}
fmt.show();
} catch (const std::exception &e) {
fmt.show();
std::cout << "The following error occurred: " << std::endl;
std::cout << e.what();
}
break;
case normal::REFRESH:
fmt.show();
break;
}
}
}