-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvimktor.cpp
More file actions
416 lines (385 loc) · 10.4 KB
/
Copy pathvimktor.cpp
File metadata and controls
416 lines (385 loc) · 10.4 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "include/vimktor.h"
#include "include/common.h"
#include "include/input_manager.h"
#include "include/sequence.h"
#include "include/vimktor_debug.h"
#include "include/window.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <curses.h>
#include <cwchar>
#include <filesystem>
#include <fstream>
#include <memory>
#include <ncurses.h>
#include <string>
void Vimktor::Init() {
InitCurses();
int w, h;
getmaxyx(stdscr, h, w);
m_windows.push_back(std::make_unique<ExploreWindow>());
m_current_window = m_windows.begin();
}
void Vimktor::End() {
m_mode = EXIT;
endwin();
}
VimktorErr_t Vimktor::NewWindowHorizontal(std::unique_ptr<Window> &&win) {
// set position
auto pos_prev = m_current_window->get()->GetWindowPosition();
pos_prev.x++; // trick to assure that widow will be right next
win.get()->SetWindowPosition(pos_prev);
win->ChangeHeight(m_current_window->get()->GetWindowDimensions().y);
auto ptr = win.get();
size_t align = 0;
m_windows.push_back(std::move(win));
size_t num_of_windows = m_windows.size();
size_t new_width = getmaxx(stdscr) / num_of_windows;
if (new_width * num_of_windows < getmaxx(stdscr))
align = getmaxx(stdscr) - new_width * num_of_windows;
// sort windows by position from left to right
auto compare_windows = [](const std::unique_ptr<Window> &p1,
const std::unique_ptr<Window> &p2) -> bool {
if (p2->GetWindowPosition().x != p1->GetWindowPosition().x) {
return p2->GetWindowPosition().x < p1->GetWindowPosition().x;
}
return p2->GetWindowPosition().y < p1->GetWindowPosition().y;
};
std::sort(m_windows.begin(), m_windows.end(), compare_windows);
position_t temp(0, 0);
for (auto &el : m_windows) {
el->MoveX(temp.x);
el.get()->ChangeWidth(new_width);
el.get()->Render();
temp.x += new_width;
Debug::Log(std::format("TO SA WYMIARY: {} A TO POZYCJA: {}",
(std::string)el->GetWindowDimensions(),
(std::string)el->GetWindowPosition()));
}
for (auto start = m_windows.begin(); start != m_windows.end(); start++) {
if (start->get() == ptr)
m_current_window = start;
}
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::InitCurses() {
initscr();
keypad(stdscr, TRUE);
raw();
nonl();
set_escdelay(50);
nodelay(stdscr, false);
noecho();
curs_set(1);
init_color(COLOR, 0, 0, 0);
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::AddWindow(std::unique_ptr<Window> &&win) {
if (m_windows.empty())
m_windows.push_back(std::move(win));
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::ChangeWindowRight() {
if (m_current_window + 1 != m_windows.end()) {
m_current_window++;
} else {
return MEMORY_ERROR;
}
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::ChangeWindowLeft() {
if (m_current_window != m_windows.begin()) {
m_current_window--;
} else {
return MEMORY_ERROR;
}
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::RenderWindow() {
if (m_current_window == m_windows.end() || m_windows.empty())
return VimktorErr_t::MEMORY_ERROR;
for (auto &win : m_windows) {
if (win != *m_current_window) {
win->Render();
}
}
m_current_window->get()->Render();
m_current_window->get()->Render();
return VIMKTOR_OK;
}
// VimktorErr_t Vimktor::GetInput() {
// VimktorEvent_t event = InputManager::Get().GetEvent(m_window, m_mode);
// HandleEvents(event);
// return VIMKTOR_OK;
// }
// VimktorErr_t Vimktor::HandleEvents(VimktorEvent_t event) {
// VimktorErr_t err = VIMKTOR_OK;
// switch (event) {
//
// case EV_NONE:
//
// break;
// case EV_CURSOR_DOWN:
// err = m_sequence.CursorMove(DOWN);
// break;
// case EV_CURSOR_UP:
// err = m_sequence.CursorMove(UP);
// break;
// case EV_CURSOR_RIGHT:
// err = m_sequence.CursorMove(RIGHT);
// break;
// case EV_CURSOR_LEFT:
// err = m_sequence.CursorMove(LEFT);
// break;
// case EV_CLOSE:
// m_mode = EXIT;
// break;
// case EV_ERASE_LINE:
// m_sequence.EraseLineCursor();
// break;
// case EV_MODE_NORMAL:
// m_mode = NORMAL;
// break;
// case EV_MODE_INSERT:
// m_mode = INSERT;
// break;
// case EV_MODE_INSERT_RIGHT:
// m_mode = INSERT;
// m_sequence.m_mode = m_mode;
// m_sequence.CursorMove(RIGHT);
// break;
// case EV_BACKSPACE:
// m_sequence.EraseCharCursor();
// break;
// case EV_INSERT_TEXT: {
// glyph_t gl = glyph_t(InputManager::Get().GetChar());
// m_sequence.InsertCharCursor(gl);
// } break;
// case EV_GO_TO_SOL:
// m_sequence.CursorMoveSol();
// break;
// case EV_GO_TO_NEXT_WORD:
// m_sequence.CursorMoveWordNext();
// break;
// case EV_GO_TO_EOL:
// m_sequence.CursorMoveEol();
// break;
// case EV_SAVE_FILE:
// WriteFile();
// HelperLog("saved to " + m_filename);
// break;
// case EV_NEW_LINE:
// m_sequence.AddNewLineCursor();
// break;
// case EV_GET_COMMAND:
// HandleCommands();
// break;
// case EV_FILE_EXPLORER:
// WriteFile();
// ExplorePath();
// break;
// case EV_ENTER_CURSOR_DIRECTORY:
// OpenFileCursor();
// break;
// }
// return VIMKTOR_OK;
// }
//
// VimktorErr_t Vimktor::HandleCommands() {
// std::string cmd;
//
// char16_t ch;
//
// nodelay(m_window, 0);
// while (1) {
// HelperLog(":" + cmd);
// wrefresh(m_window);
// ch = wgetch(m_window);
// if (ch == KEY_ESCAPE || ch == 13 || ch == KEY_ENTER) {
// nodelay(m_window, 1);
// // ch = wgetch(m_window);
// break;
// wrefresh(stdscr);
// }
// if (ch == KEY_BACKSPACE) {
// if (cmd.size() > 0)
// cmd.pop_back();
//
// } else {
// cmd.push_back(ch);
// }
// }
// nodelay(m_window, 0);
//
// // nodelay(m_window, 1);
// HelperLog(" ");
// wrefresh(stdscr);
// if (commandList.contains(cmd)) {
// HandleEvents(commandList[cmd]);
// }
//
// return VIMKTOR_OK;
// }
//
// VimktorErr_t Vimktor::OpenEmpty() {
// LoadFile(".vimktor_temp");
// return VIMKTOR_OK;
// }
//
// VimktorErr_t Vimktor::LoadFile(const std::string &fileName) {
// if (fileName == ".") {
// ExplorePath();
// return VIMKTOR_OK;
// }
// std::fstream file;
// m_filename = fileName;
// file.open(fileName, std::ios::in);
// if (!file.good()) {
// file.close();
// file.open(m_filename, std::ios::out | std::ios::in |
// std::fstream::trunc); return FILE_ERROR;
// }
// Debug::Log("plik zapisu: " + m_filename);
// m_sequence.LoadFile(file);
// file.close();
// return VIMKTOR_OK;
// };
//
// VimktorErr_t Vimktor::WriteFile(const std::string &fileName) {
// std::fstream file;
// file.open(fileName, std::ios::out);
// if (!file.good()) {
// return FILE_ERROR;
// }
// m_sequence.WriteFile(file);
// file.close();
// return VIMKTOR_OK;
// };
//
// VimktorErr_t Vimktor::WriteFile() {
// std::fstream file;
// file.open(m_filename, std::ios::out);
// if (!file.good()) {
// return FILE_ERROR;
// }
// m_sequence.WriteFile(file);
// file.close();
// return VIMKTOR_OK;
// };
//
VimktorErr_t Vimktor::HandleEvents() {
auto ev = m_current_window->get()->HandleInput();
m_current_window->get()->HelperLog(VimktorEventToString(ev));
if (ev == EV_NONE)
return VIMKTOR_OK;
switch (ev) {
case EV_CHANGE_WINDOW_RIGHT:
ChangeWindowRight();
break;
case EV_CHANGE_WINDOW_LEFT:
ChangeWindowLeft();
break;
case EV_NEW_WINDOW_HORIZONTAL:
NewWindowHorizontal(std::make_unique<ExploreWindow>());
break;
case EV_ENTER_CURSOR_DIRECTORY:
break;
case EV_OPEN_EDITOR:
ChangeWindowType(EDITOR);
break;
case EV_OPEN_COLAB:
ChangeWindowType(COLLAB);
break;
case EV_CLOSE:
Debug::Log(std::format(" NIE WIEM {}", m_windows.size()));
CloseCurrentWindow();
break;
default:
return VIMKTOR_OK;
}
return VIMKTOR_OK;
}
VimktorErr_t Vimktor::CloseCurrentWindow() {
if (m_current_window == m_windows.end())
return MEMORY_ERROR;
m_current_window = m_windows.erase(m_current_window);
Debug::Log(std::format(" NIE WIEM {}", m_windows.size()));
if (m_current_window == m_windows.end() || m_windows.size() == 0) {
End();
return VimktorErr_t::EOL_ERROR;
}
return VIMKTOR_OK;
}
void Vimktor::Loop() {
while (m_mode != EXIT) {
RenderWindow();
HandleEvents();
}
}
VimktorErr_t Vimktor::ChangeWindowType(VimktorWindowType_t type) {
std::unique_ptr<Window> new_window;
if (type == COLLAB) {
new_window = std::move(std::make_unique<CollabWindow>());
} else if (type == EXPLORER) {
new_window = std::move(std::make_unique<ExploreWindow>());
} else if (type == EDITOR) {
new_window = std::move(std::make_unique<EditorWindow>());
}
Debug::Log(std::format("zmiana na {}", (int)type));
new_window->HelperLog(std::format("zmiana na {}", (int)type));
return VIMKTOR_OK;
}
// position_t Vimktor::GetEditorDimensions() {
// return position_t(getmaxx(m_window), getmaxy(m_window));
// }
//
// void Vimktor::HelperLog(const std::string &msg) {
// position_t endPoint = GetEditorDimensions();
// size_t y = endPoint.y - 1;
// size_t x = 0;
// wmove(m_window, y, x);
// clrtoeol();
// mvwprintw(m_window, y, x, "%s", msg.c_str());
// }
//
// Vimktor::CommandList_t Vimktor::commandList = {
// {"w", EV_SAVE_FILE},
// {"Explore", EV_FILE_EXPLORER},
// {"q", EV_CLOSE},
// {"Colab", EV_COLAB},
// };
//
//
// VimktorErr_t Vimktor::ExplorePath() {
// m_sequence.m_cursorPos = position_t(0, 0);
// m_mode = FILES;
// HelperLog(std::filesystem::current_path().string());
// m_sequence.LoadCurrentDirectory();
// return VIMKTOR_OK;
// }
//
// VimktorErr_t Vimktor::ExplorePath(const std::string &path_str) {
// m_sequence.m_cursorPos = position_t(0, 0);
// m_mode = FILES;
// std::filesystem::current_path(path_str);
// HelperLog(std::filesystem::current_path().string());
// m_sequence.LoadCurrentDirectory();
// return VIMKTOR_OK;
// }
//
// VimktorErr_t Vimktor::OpenFileCursor() {
// auto path = std::filesystem::current_path();
//
// std::string path_str = path.string() + '/' + m_sequence.GetStringCursor();
// if (std::filesystem::is_directory(path_str) ||
// m_sequence.GetStringCursor() == "../" ||
// m_sequence.GetStringCursor() == "./") {
// ExplorePath(path_str);
// } else {
// LoadFile(m_sequence.GetStringCursor());
// m_mode = NORMAL;
// }
// return VIMKTOR_OK;
// }