-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
343 lines (303 loc) · 12.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <nana/gui.hpp>
#include <nana/gui/place.hpp>
#include <nana/gui/widgets/button.hpp>
#include <nana/gui/widgets/label.hpp>
#include <nana/gui/widgets/textbox.hpp>
#include <nana/gui/widgets/listbox.hpp>
#include <nana/gui/widgets/panel.hpp>
#include <nana/gui/widgets/tabbar.hpp>
#include <nana/gui/widgets/toolbar.hpp>
#include <nana/gui/filebox.hpp>
#include <nana/threads/pool.hpp>
//#include <nana/filesystem/filesystem.hpp>
#include <nana/filesystem/filesystem_ext.hpp>
#define TOML11_COLORIZE_ERROR_MESSAGE
#include <toml.hpp>
#include <iostream>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <optional>
#include <string_view>
static bool endsWith(std::string_view str, std::string_view suffix)
{
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}
static bool startsWith(std::string_view str, std::string_view prefix)
{
return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
}
struct tab_page_editor : public nana::panel<false>
{
nana::place my_place{*this};
nana::textbox editor{*this};
std::string file_name;
tab_page_editor(nana::window wd): nana::panel<false>(wd)
{
my_place.div("<editor>");
my_place["editor"]<< editor;
editor.borderless(true);
}
void load_file(const std::filesystem::path &file_path)
{
file_name = file_path.filename();
if (endsWith(file_name, "cpp") || endsWith(file_name, "c")) {
editor.set_highlight("C++ keywords", nana::colors::blue, nana::colors::white);
editor.set_keywords("C++ keywords", false, true, {"structs", "class", "return", "void", "for", "while", "break", "if", "else"});
editor.set_highlight("C++ types", nana::colors::burly_wood, nana::colors::white);
editor.set_keywords("C++ types", false, true, { "int", "float", "char", "NULL"});
}
#if 1
editor.load(file_path);
#else
std::ifstream myfile(file_path.value());
if (myfile.is_open()) {
std::string line;
while (std::getline(myfile,line)) {
editor.append(line, false);
}
myfile.close();
}
editor.edited_reset();
// ???
// editor._m_saved()
#endif
}
};
namespace std {
namespace filesystem {
std::filesystem::path config_unix_file(std::string name)
{
const char *xdg_home = getenv("XDG_CONFIG_HOME");
if (xdg_home != nullptr) {
path file_name = path(xdg_home);
file_name.replace_filename(name);
return file_name;
}
const char *unix_home = getenv("HOME");
if (unix_home != nullptr) {
path file_name = path(unix_home);
file_name.append(name);
return file_name;
}
path file_name = std::filesystem::temp_directory_path();
file_name.append(name);
return file_name;
}
std::filesystem::path config_windows_file(std::string name)
{
// TODO: take ideas from https://github.com/radekp/qt/blob/master/src/corelib/io/qfsfileengine_win.cpp
return "c:\\users\\defualt\\";
}
std::filesystem::path config_file(std::string name)
{
// TODO - windows?
return std::filesystem::config_unix_file(name);
}
}
}
class project_model {
std::vector<std::filesystem::path> files;
std::string base_dir;
public:
void clear() {
base_dir.clear();
files.clear();
}
void load_dir(const std::string &dir_name) {
base_dir = dir_name;
try {
for (const auto& dir : std::filesystem::recursive_directory_iterator{ dir_name }) {
files.push_back(dir.path());
}
} catch (...) {
}
}
void setup_listbox(nana::listbox &lsbox)
{
lsbox.clear();
lsbox.auto_draw(false);
for (auto &s: files) {
auto relative = std::filesystem::relative(s.parent_path(), base_dir);
lsbox.at(0).append({ s.filename(), relative != "." ? relative : ""});
}
lsbox.auto_draw(true);
}
std::optional<std::filesystem::path> get_file(size_t index)
{
if (index > files.size()){
return {};
}
return files.at(index);
}
};
#ifdef __linux__
int exec(const char* cmd, std::function<void(const char*)> on_line_read)
{
char line[1024];
FILE* pipe = popen(cmd, "r");
if (!pipe) {
return -1;
}
while (fgets(line, sizeof line, pipe) != NULL) {
on_line_read(line);
}
pclose(pipe);
return 0;
}
#endif
#ifdef __WIN32__
// https://stackoverflow.com/a/46348112/78712
int exec(const char* cmd, std::function<void(const char*)> on_line_read)
{
// TODO
}
#endif
auto nana_ide_config_file_name = ".nanaiderc";
std::filesystem::path nana_ide_config_file;
int main() {
nana::threads::pool thread_pool;
nana::form fm{};
nana::place plc{fm};
nana::tabbar<std::string> tabs(fm);
nana::listbox lsbox(fm);
nana::textbox lbl(fm);
nana::button buttonOpenFile{fm, "File"};
nana::button buttonOpenProject{fm, "Project"};
nana::button buttonCompile{fm, "Compile"};
nana::button buttonRun{fm, "Run"};
// https://www.reddit.com/r/cpp/comments/f70io2/toml_a_toml_parser_and_serializer_for_c17/
toml::value config;
project_model project;
std::vector<std::shared_ptr<tab_page_editor>> pages;
auto display_file = [&fm, &pages, &tabs, &plc](const std::filesystem::path &file) {
for(int i=0; i< tabs.length(); i++) {
std::string s1 = file.filename();
std::string s2 = pages.at(i).get()->file_name;
if (s1 == s2) {
tabs.activated(i);
return;
}
}
pages.push_back(std::make_shared<tab_page_editor>(fm));
tab_page_editor &editor {*pages.back()};
tabs.append(file.filename(), editor);
editor.load_file(file);
plc["tab_frame"].fasten(editor);
editor.editor.focus();
plc.collocate();
};
nana_ide_config_file = std::filesystem::config_file(nana_ide_config_file_name);
lsbox.append_header("filename");
lsbox.append_header("size");
lsbox.events().selected([&display_file, &project](const nana::arg_listbox &msg){
if (!msg.item.selected()) {
return;
}
std::optional<std::filesystem::path> file = project.get_file(msg.item.pos().item);
if (file == std::nullopt) {
return;
}
if (!std::filesystem::is_regular_file(file.value())) {
return;
}
display_file(file.value());
});
tabs.events().removed([&pages](const nana::arg_tabbar_removed<std::string> &arg) {
pages.erase(pages.begin() + arg.item_pos);
});
lbl.editable(false);
buttonOpenFile.events().click([&fm, &display_file]{
nana::filebox fb(fm, true);
fb.add_filter("C++ files", "*.c;*.cxx;*.cpp;*.h;*.hpp");
fb.add_filter("Build system files", "Makefile*;CMakeLists.txt;conanfile.txt");
fb.add_filter("Text files", "*.txt;*.md");
fb.add_filter("All Files", "*.*");
fb.allow_multi_select(true);
auto files = fb.show();
if (!files.empty()) {
for (auto f: files) {
display_file(f);
}
}
});
buttonOpenProject.events().click(nana::threads::pool_push(thread_pool, [&lsbox, &project, &config]{
// Seems like nana 1.7.4 does not support changing the folderbox title
nana::folderbox picker{nullptr, {}, "Choose project directory"};
picker.title("Choose project directory");
auto path = picker.show();
if (path.empty()) {
return;
}
project.clear();
project.load_dir(path.front().string());
project.setup_listbox(lsbox);
try {
config["environment"]["loaded_dir"] = path.front().string();
std::ofstream config_file (nana_ide_config_file);
config_file << config;
config_file.close();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}));
buttonCompile.events().click(nana::threads::pool_push(thread_pool, [&lbl, &buttonCompile, &fm]{
buttonCompile.enabled(false);
buttonCompile.caption("Compiling...");
lbl.caption("");
exec("ninja -C build", [&lbl, &fm](const char* line){
lbl.append(line, false);
});
buttonCompile.caption("Compile");
buttonCompile.enabled(true);
}));
buttonRun.events().click([&fm](){
nana::msgbox mb(fm, "Run");
mb.icon(nana::msgbox::icon_information) << "unimplemented yet";
mb.show();
});
plc.div("vert"
"<toolbar weight=30"
"<left_side min=205 gap=5 arrange=[100,repeated]>"
"<spacer min=80>"
"<right_side weight=205 gap=5 arrange=[100,repeated]>"
">"
"<main_window"
"<list weight=20%>|<vert <tabs weight=32><tab_frame>|<panel weight=20%>"
">"
);
plc["left_side"] << buttonOpenFile;
plc["left_side"] << buttonOpenProject;
plc["right_side"] << buttonRun;
plc["right_side"] << buttonCompile;
plc["list"] << lsbox;
plc["tabs"] << tabs;
plc["panel"] << lbl;
plc.collocate();
try {
config = toml::parse<toml::preserve_comments>(nana_ide_config_file);
const std::string loaded_dir = toml::find<std::string>(config, "environment", "loaded_dir");
if (!loaded_dir.empty()) {
project.clear();
project.load_dir(loaded_dir);
project.setup_listbox(lsbox);
} else {
// TODO: hacks to see something on screen
tab_page_editor editor1(fm);
tab_page_editor editor2(fm);
tabs.append("main.cpp", editor1);
tabs.append("CMakeLists.txt", editor2);
plc["tab_frame"].fasten(editor1).fasten(editor2);
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
fm.show();
// TODO - we should ave geometry of window on config
nana::API::zoom_window(fm, true);
nana::exec();
return 0;
}