-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequence.cpp
More file actions
471 lines (392 loc) · 10.9 KB
/
Copy pathsequence.cpp
File metadata and controls
471 lines (392 loc) · 10.9 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "include/sequence.h"
#include "include/common.h"
#include "include/vimktor_debug.h"
#include "vimktor_debug.cpp"
#include <assert.h>
#include <cstdio>
#include <expected>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
Sequence::Sequence(std::fstream &file) { LoadFile(file); }
std::vector<glyph_t> &Sequence::operator[](size_t line) {
assert(line < data.size());
return (data[line]);
}
std::vector<glyph_t> &Sequence::GetLineAt(size_t line) {
assert(line < data.size());
return (data[line]);
}
std::vector<glyph_t> &Sequence::GetLineAtCursor() {
size_t line = m_cursorPos.y;
assert(line < data.size());
return (data[line]);
}
std::expected<glyph_t *, VimktorErr_t> Sequence::GetGlyphAt(size_t col,
size_t line) {
if (line >= data.size() || col >= data[line].size()) {
return std::unexpected(INVALID_ARRGUMENT);
}
return &data[line][col];
}
std::expected<glyph_t *, VimktorErr_t> Sequence::GetGlyphAtRel(size_t col,
size_t line) {
col = col + m_pagePos.x;
line = line + m_pagePos.y;
if (line >= data.size() || col >= data[line].size()) {
return std::unexpected(INVALID_ARRGUMENT);
}
return &data[line][col];
}
void Sequence::SetLineTo(size_t line, const std::string &str) {
assert(data.size() < line);
if (str.size() > data[line].size()) {
data[line].clear();
}
data[line].resize(0);
for (auto el : str) {
data[line].push_back(glyph_t(el));
}
}
std::string Sequence::GetStringCursor() {
size_t line = m_cursorPos.y;
assert(data.size() > line);
std::string ans;
ans.resize(data[line].size());
size_t num = 0;
for (auto glyph : data[line]) {
ans[num++] = glyph;
}
return ans;
}
std::string Sequence::GetStringAt(size_t line) {
assert(data.size() > line);
std::string ans;
ans.resize(data[line].size());
size_t num = 0;
for (auto glyph : data[line]) {
ans[num++] = glyph;
}
return ans;
}
void Sequence::AddLine(const std::string &str) {
auto &line_vec = data.emplace_back();
for (auto ch : str) {
line_vec.push_back(ch);
}
}
VimktorErr_t Sequence::CursorMove(CursorDirection dir) {
position_t backUp = m_cursorPos;
if (dir == UP) {
m_cursorPos.y--;
// CursorChangeLine(UP);
} else if (dir == DOWN) {
m_cursorPos.y++;
// CursorChangeLine(DOWN);
} else if (dir == LEFT) {
m_cursorPos.x--;
} else if (dir == RIGHT) {
m_cursorPos.x++;
}
ManageLastPos(backUp);
CursorManagePagePos();
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorMovePos(const position_t &pos) {
position_t backUp = m_cursorPos;
m_cursorPos.x = pos.x;
m_cursorPos.y = pos.y;
ManageLastPos(backUp);
CursorManagePagePos();
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorMovePos(const position_t &&pos) {
position_t backUp = m_cursorPos;
m_cursorPos.x = pos.x;
m_cursorPos.y = pos.y;
ManageLastPos(backUp);
CursorManagePagePos();
return VIMKTOR_OK;
}
void Sequence::ManageLastPos(position_t &backUp) {
// this method fixes cursor positon after moving event
//
// make sure line exist
//
//
Debug::Log(std::format("cursor before{}", (std::string)backUp));
if (m_cursorPos.x < 0) {
m_cursorPos.x = 0;
backUp.x = m_cursorPos.x;
}
if (m_cursorPos.y < 0) {
m_cursorPos.y = 0;
backUp.y = m_cursorPos.y;
}
if (m_cursorPos.y >= Size() && m_cursorPos.y != 0) {
m_cursorPos.y = Size() - 1;
}
if (m_cursorPos.x != backUp.x) {
// jezeli jestesmy w tej samej lini
if (m_cursorPos.x >= LineSize(m_cursorPos.y) && m_cursorPos.x != 0) {
size_t currentLineLen = LineSize(m_cursorPos.y);
m_cursorPos.x = (currentLineLen > 0) ? currentLineLen - 1 : 0;
// m_cursorPos.x = LineSize(m_cursorPos.y) - 1;
}
m_cursorPosPrev.x = m_cursorPos.x;
} else {
m_cursorPos.x = m_cursorPosPrev.x;
}
if (m_cursorPos.x >= LineSize(m_cursorPos.y) && m_cursorPos.x != 0) {
m_cursorPos.x = LineSize(m_cursorPos.y) - 1;
if (m_cursorPos.x < 0)
m_cursorPos.x = 0;
}
Debug::Log(std::format("cursor after{}", (std::string)m_cursorPos));
return;
}
VimktorErr_t Sequence::CursorChangeLine(CursorDirection dir) {
if (dir != UP && dir != DOWN)
return VimktorErr_t::INVALID_ARRGUMENT;
if (dir == UP)
m_cursorPos.y--;
if (dir == DOWN)
m_cursorPos.y++;
if (CursorPosValid() != VIMKTOR_OK)
return VimktorErr_t::EOL_ERROR;
if (m_cursorPosPrev.x >= LineSize(m_cursorPos.y)) {
if (LineSize(m_cursorPos.y) == 0)
m_cursorPos.x = 0;
m_cursorPos.x = LineSize(m_cursorPos.y) - 1;
} else {
m_cursorPos.x = m_cursorPosPrev.x;
}
return VIMKTOR_OK;
}
void Sequence::AddGlyphAt(size_t col, size_t line, glyph_t glyph) {
auto &buffer = GetLineAt(line);
buffer.insert(buffer.begin() + col, glyph);
}
VimktorErr_t Sequence::LoadFile(std::fstream &file) {
data.clear();
data.reserve(DEFAULT_SEQUENCE_LINE_NUM);
std::string line;
while (!file.eof()) {
getline(file, line);
auto &element = data.emplace_back(std::vector<glyph_t>());
element.reserve(line.size());
for (char ch : line) {
element.emplace_back(glyph_t(ch));
}
}
return VIMKTOR_OK;
}
VimktorErr_t Sequence::WriteFile(std::fstream &file) {
file.clear();
for (const auto &line_itr : data) {
for (const auto &glyph_itr : line_itr) {
file << (char)glyph_itr.ch;
}
file << '\n';
}
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorPosValid() {
if (m_cursorPos.x < 0 || m_cursorPos.y < 0)
return MEMORY_ERROR;
if (m_cursorPos.y > Size()) {
return MEMORY_ERROR;
}
if (m_cursorPos.x == 0)
return VIMKTOR_OK;
if (m_cursorPos.x >= LineSize(m_cursorPos.y)) {
return MEMORY_ERROR;
}
return VIMKTOR_OK;
}
void Sequence::InsertCharCursor(const glyph_t &gl) {
if (Size() == 0)
data.emplace_back();
const auto itr = data[m_cursorPos.y].begin() + m_cursorPos.x;
data[m_cursorPos.y].insert(itr, gl);
CursorMove(RIGHT);
}
void Sequence::EraseCharCursor() {
if (m_cursorPos.x == 0) {
if (m_cursorPos.y == 0)
return;
EraseLineCursor();
CursorMove(UP);
CursorMoveEol();
return;
}
CursorMove(LEFT);
const auto itr = data[m_cursorPos.y].begin() + m_cursorPos.x;
data[m_cursorPos.y].erase(itr);
}
void Sequence::AddNewLineCursor() {
auto itr = data.begin() + m_cursorPos.y + 1;
std::vector<glyph_t> new_line;
new_line.reserve(DEFAULT_LINE_LENGTH);
data.insert(itr, std::move(new_line));
CursorMove(DOWN);
}
void Sequence::ReplaceCharCursor(const glyph_t & gl){}
void Sequence::EraseLineCursor() {
auto itr = data.begin() + m_cursorPos.y;
if (data.size() == 1) {
itr->clear();
CursorMoveSol();
return;
}
if (itr == data.end())
itr--;
data.erase(itr);
ManageLastPos(m_cursorPos);
};
VimktorErr_t Sequence::CursorMoveEol() {
auto backUp = m_cursorPos;
m_cursorPos.x = LineSize(m_cursorPos.y) - 1;
ManageLastPos(backUp);
// TODO: fix page positioning
Debug::Log(std::format("x{} , line size {}", m_cursorPos.x,
LineSize(m_cursorPos.y)));
CursorManagePagePos();
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorMoveSol() {
auto backUp = m_cursorPos;
m_cursorPos.x = 0;
ManageLastPos(backUp);
CursorManagePagePos();
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorMoveWordNext() {
auto itr =
GetLineAtCursor().begin() + m_cursorPos.x; // current letter nder cursor
bool afterSpace = false;
while (itr != GetLineAtCursor().end()) {
if (itr->ch == ' ') {
afterSpace = true;
} else {
if (afterSpace) {
m_cursorPos.x = itr - GetLineAtCursor().begin();
return VIMKTOR_OK;
}
}
itr++;
}
if (m_cursorPos.y < Size()) {
CursorMove(DOWN);
CursorMoveSol();
return VIMKTOR_OK;
}
return VIMKTOR_OK;
}
VimktorErr_t Sequence::CursorManagePagePos() {
if (m_cursorPos.x < m_pagePos.x) {
m_pagePos.x = m_cursorPos.x;
} else if (m_cursorPos.x >= m_pagePos.x + m_pageWidth) {
m_pagePos.x = m_cursorPos.x - m_pageWidth + 1;
}
if (m_cursorPos.y < m_pagePos.y) {
m_pagePos.y = m_cursorPos.y;
} else if (m_cursorPos.y >= m_pagePos.y + m_pageHeight) {
m_pagePos.y = m_cursorPos.y - m_pageHeight + 1;
}
if (m_pagePos.x < 0)
m_pagePos.x = 0;
if (m_pagePos.y < 0)
m_pagePos.y = 0;
Debug::Log(std::format("page x{}, page y{}", m_pagePos.x, m_pagePos.y));
return VIMKTOR_OK;
}
VimktorErr_t Sequence::LoadCurrentDirectory() {
data.clear();
auto path = std::filesystem::current_path();
// load special directories
AddLine("../");
AddLine("./");
for (auto const &dir : std::filesystem::directory_iterator{path}) {
AddLine(dir.path().filename());
}
return VIMKTOR_OK;
}
#include "include/colab.h"
#include <sys/socket.h>
void SequenceCollab::AddNewLineCursor() {
int32_t y = m_cursorPos.y; // Zapisujemy z której linii robimy Enter
Sequence::AddNewLineCursor(); // Lokalna zmiana[cite: 1]
if (m_socket != -1) {
CollabMessage msg;
msg.type = OP_NEWLINE;
msg.x = 0;
msg.y = y;
msg.ch = 0;
send(m_socket, &msg, sizeof(msg), MSG_NOSIGNAL);
}
}
void SequenceCollab::EraseLineCursor() {
int32_t y = m_cursorPos.y;
Sequence::EraseLineCursor();
if (m_socket != -1) {
CollabMessage msg;
msg.type = OP_ERASE_LINE;
msg.x = 0;
msg.y = y;
msg.ch = 0;
send(m_socket, &msg, sizeof(msg), MSG_NOSIGNAL);
}
}
void SequenceCollab::ReplaceCharCursor(const glyph_t &gl) {
int32_t x = m_cursorPos.x;
int32_t y = m_cursorPos.y;
// Sequence::ReplaceCharCursor(gl); // Lokalna zmiana
if (m_socket != -1) {
CollabMessage msg;
msg.type = OP_REPLACE;
msg.x = x;
msg.y = y;
msg.ch = (uint32_t)gl.ch;
send(m_socket, &msg, sizeof(msg), MSG_NOSIGNAL);
}
}
void SequenceCollab::ApplyRemoteNewLine(int32_t y) {
if (y < 0 || y >= data.size())
return;
auto itr = data.begin() + y + 1;
std::vector<glyph_t> new_line;
new_line.reserve(DEFAULT_LINE_LENGTH);
data.insert(itr, std::move(new_line));
if (m_cursorPos.y > y) {
m_cursorPos.y++;
}
}
void SequenceCollab::ApplyRemoteEraseLine(int32_t y) {
if (y < 0 || y >= data.size())
return;
auto itr = data.begin() + y;
if (data.size() == 1) {
itr->clear(); // Nie usuwamy ostatniej linii, tylko ją czyścimy[cite: 1]
} else {
data.erase(itr);
}
// Fix: Zarządzanie kursorem po usunięciu linii przez kogoś innego
if (m_cursorPos.y > y) {
m_cursorPos.y--; // Zsuwamy nasz kursor do góry
} else if (m_cursorPos.y == y) {
m_cursorPos.x =
0; // Jeśli ktoś skasował linię w której byliśmy, lądujemy na początku
if (m_cursorPos.y >= data.size()) {
m_cursorPos.y = data.size() - 1; // Zabezpieczenie przed EOF
}
}
}
void SequenceCollab::ApplyRemoteReplace(int32_t x, int32_t y, uint32_t ch) {
if (y < 0 || y >= data.size())
return;
if (x < 0 || x >= data[y].size())
return;
data[y][x] = glyph_t(ch);
}