-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.cpp
More file actions
408 lines (362 loc) · 11.6 KB
/
Editor.cpp
File metadata and controls
408 lines (362 loc) · 11.6 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
#include "Editor.h"
// NON-CLASS FUNCTIONS
Editor::Editor()
{
}
Editor::Editor(std::string filename)
{
unsigned int lineCounter{1};
std::fstream read_file;
textFile = filename;
read_file.open(filename);
if (read_file.fail())
{
std::cerr << "Error opening file: " << filename << '\n';
std::exit(EXIT_FAILURE);
//throw("Error opening file");
}
while (!read_file.eof())
{
std::string _line{};
std::getline(read_file, _line);
lineNumber.insert(lineCounter, _line);
lineCounter++;
}
read_file.close();
}
Editor::Editor(std::string filename, std::string keywordFile) : Editor(filename)
{
std::fstream read_file;
// Keywords list
read_file.open(keywordFile);
if (read_file.fail())
{
std::cerr << "Error opening file: " << keywordFile << '\n';
std::exit(EXIT_FAILURE);
}
while (!read_file.eof())
{
std::string _line{};
std::getline(read_file, _line);
keywords.push_back(_line);
}
read_file.close();
std::sort(keywords.begin(), keywords.end());
}
void Editor::run()
{
unsigned int count{}; // Keeps track of case 'dd'
CommandPlus cmd;
height = 20;
width = COLS;
startx = 0;
starty = 0;
initscr(); // Start curses mode
noecho(); // No echoing to screen
raw(); // Prevents use of signals from ctl + c
win = newwin(height, width, starty, startx);
keypad(win, true);
int ch;
// Output text to window with wprintw() from filename
display();
wmove(win, 0, 0);
while ((ch = wgetch(win)) != QUIT && ch != ESCAPE)
{
switch (ch)
{
case 'j':
case KEY_DOWN:
moveDown();
count = 0;
break;
case 'k':
case KEY_UP:
moveUp();
count = 0;
break;
case 'h':
case KEY_LEFT:
moveLeft();
count = 0;
break;
case 'l':
case KEY_RIGHT:
moveRight();
count = 0;
break;
case 'x': // delete char
deleteChar();
wclear(win);
display();
wmove(win, userPosition.get_y(), userPosition.get_x());
count = 0;
break;
case 'd': // delete line 'dd'
count++;
if (count == 2)
{
deleteLine();
wclear(win);
display();
wmove(win, userPosition.get_y(), userPosition.get_x());
count = 0;
}
break;
case 'u': // undo
undo_();
wclear(win);
display();
count = 0;
break;
case 'i': // Insert
insertMode = true;
insert_();
count = 0;
break;
default:
count = 0;
break;
}
}
endwin(); // End curses mode
}
void Editor::display()
{
// Display for ncurses
unsigned int i{1};
for (; i < lineNumber.getLength() + 1; i++)
{
std::string ch{lineNumber.getEntry(i)};
wprintw(win, ch.c_str());
wprintw(win, "\n");
}
}
void Editor::displayLine()
{
// This play for normal function (non-ncurses)
unsigned int i{1};
for (; i < lineNumber.getLength() + 1; i++)
{
std::cout << lineNumber.getEntry(i);
std::cout << '\n';
}
}
void Editor::moveDown()
{
if (userPosition.get_y() < lineNumber.getLength() - 1)
{
// x-coordinate has to be within bounds
if (userPosition.get_x() >= lineNumber.getEntry(userPosition.get_y() + 2).length())
{
userPosition.set_x(lineNumber.getEntry(userPosition.get_y() + 2).length() - 1);
}
userPosition.set_y(userPosition.get_y() + 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
}
void Editor::moveUp()
{
if (userPosition.get_y() > 0)
{
// x-coordinate has to be within bounds
if (userPosition.get_x() >= lineNumber.getEntry(userPosition.get_y()).length())
{
userPosition.set_x(lineNumber.getEntry(userPosition.get_y()).length() - 1);
}
userPosition.set_y(userPosition.get_y() - 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
}
void Editor::moveLeft()
{
// If within bounds
if (userPosition.get_x() > 0)
{
userPosition.set_x(userPosition.get_x() - 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
}
void Editor::moveRight()
{
if (insertMode)
{
if (userPosition.get_x() < lineNumber.getEntry(userPosition.get_y() + 1).length())
{
userPosition.set_x(userPosition.get_x() + 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
}
else
{
// If within bounds
if (userPosition.get_x() < lineNumber.getEntry(userPosition.get_y() + 1).length() - 1)
{
userPosition.set_x(userPosition.get_x() + 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
}
}
void Editor::deleteChar()
{
if (lineNumber.getEntry(userPosition.get_y() + 1).length() > 0)
{
CommandPlus cmd;
// save value and location then push to stack
cmd.setValue(lineNumber.getEntry(userPosition.get_y() + 1).substr(userPosition.get_x(), 1));
cmd.setLocation(userPosition);
undoStack.push(cmd);
}
// Push char, then replace new string
lineNumber.replace(userPosition.get_y() + 1, lineNumber.getEntry(userPosition.get_y() + 1).erase(userPosition.get_x(), 1));
// Reset position
if (userPosition.get_x() > 0)
userPosition.set_x(userPosition.get_x() - 1);
}
void Editor::deleteLine()
{
bool removed = false;
CommandPlus cmd;
// save value and location then push to stack
cmd.setValue(lineNumber.getEntry(userPosition.get_y() + 1));
cmd.setLocation(userPosition);
undoStack.push(cmd);
// This prevents if it's the only node from being deleted
// instead replacing it with an empty string
if (lineNumber.getLength() == 1)
{
lineNumber.replace(1, "");
removed = true;
}
// When deleting a line, the Y-coord should be decremented so to prevent
// the cursor from being on an empty line
if (userPosition.get_y() > 0 && !removed)
{
lineNumber.remove(userPosition.get_y() + 1); // current line
userPosition.set_y(userPosition.get_y() - 1);
removed = true;
}
if (!removed)
lineNumber.remove(userPosition.get_y() + 1);
// If the x position is greater than the length of the new line
// then put the cursor position to the last char of the line
// If the line is empty (a "" string) then set 'x' coord to 0
if (lineNumber.getEntry(userPosition.get_y() + 1).length() > 0)
userPosition.set_x(lineNumber.getEntry(userPosition.get_y() + 1).length() - 1);
else
userPosition.set_x(0);
}
// insert should be by char, with getch(),
void Editor::insert_()
{
CommandPlus tempCommand;
tempCommand.setValue(lineNumber.getEntry(userPosition.get_y() + 1));
undoStack.push(tempCommand);
WINDOW *boxWindow = newwin(3, 15, LINES - 3, 0);
//box(boxWindow, 0, 0);
mvwprintw(boxWindow, 1, 1, "Insert Mode");
wrefresh(boxWindow);
// Reset cursor
wmove(win, userPosition.get_y(), userPosition.get_x());
while (true)
{
int _char = wgetch(win);
if (_char == ESCAPE)
{
wclear(boxWindow);
wrefresh(boxWindow);
delwin(boxWindow);
insertMode = false;
// Reset cursor if past length
if (userPosition.get_x() > lineNumber.getEntry(userPosition.get_y() + 1).length() - 1)
{
userPosition.set_x(userPosition.get_x() - 1);
wmove(win, userPosition.get_y(), userPosition.get_x());
}
break;
}
// WIP
if (_char == ENTER)
{
std::string first_half{lineNumber.getEntry(userPosition.get_y() + 1).substr(0, userPosition.get_x())};
std::string second_half{lineNumber.getEntry(userPosition.get_y() + 1).substr(userPosition.get_x())};
// Replace the node cursor is at with new string
lineNumber.replace(userPosition.get_y() + 1, first_half);
// Insert new node next to cursor node with new string
lineNumber.insert(userPosition.get_y() + 2, second_half);
userPosition.set_x(0);
userPosition.set_y(userPosition.get_y() + 1);
// Update display
wclear(win);
display();
wmove(win, userPosition.get_y(), userPosition.get_x());
continue;
}
switch (_char)
{
case KEY_DOWN:
moveDown();
break;
case KEY_UP:
moveUp();
break;
case KEY_RIGHT:
moveRight();
break;
case KEY_LEFT:
moveLeft();
break;
case '\t':
lineNumber.replace(userPosition.get_y() + 1, lineNumber.getEntry(userPosition.get_y() + 1).insert(userPosition.get_x(), 4, ' '));
userPosition.set_x(userPosition.get_x() + 4);
wclear(win);
display();
wmove(win, userPosition.get_y(), userPosition.get_x());
break;
default:
// Place user's inputed character to modify string in y coordinate
lineNumber.replace(userPosition.get_y() + 1, lineNumber.getEntry(userPosition.get_y() + 1).insert(userPosition.get_x(), 1, _char));
// Push new modified line to stack
tempCommand.setValue(lineNumber.getEntry(userPosition.get_y() + 1));
undoStack.push(tempCommand);
// Set and move cursor 1+ after modified string
userPosition.set_x(userPosition.get_x() + 1);
// Update display
wclear(win);
wrefresh(win);
display();
wmove(win, userPosition.get_y(), userPosition.get_x());
break;
}
}
}
void Editor::undo_()
{
CommandPlus tempCmd;
if (!undoStack.isEmpty())
{
tempCmd = undoStack.peek();
undoStack.pop();
// If we are undoing a string deleting then we insert it back
if (tempCmd.getValue().length() > 1 || tempCmd.getValue() == "")
{
lineNumber.insert(tempCmd.getYLocation() + 1, tempCmd.getValue());
}
// Else, we are restoring a character, which requires the string
// to be replaced with the character in the exact place it originally was
else
{
lineNumber.replace(tempCmd.getYLocation() + 1, lineNumber.getEntry(tempCmd.getYLocation() + 1).insert(tempCmd.getXLocation(), tempCmd.getValue()));
// This increments the x position as you are undoing as long as the length
// of the line is longer than the x position
// We only want to incremetn 'x' if we're undoing chars, which is why it's inside this else statement
if (userPosition.get_x() < lineNumber.getEntry(userPosition.get_y() + 1).length() - 1)
userPosition.set_x(userPosition.get_x() + 1);
}
// This stops the x cursor from going out of bounds
if (userPosition.get_x() >= lineNumber.getEntry(userPosition.get_y() + 1).length())
userPosition.set_x(lineNumber.getEntry(userPosition.get_y() + 1).length() - 1);
// This stops x-coor from going negative
if (userPosition.get_x() < 0)
userPosition.set_x(0);
}
}