-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
433 lines (381 loc) · 11.4 KB
/
main.c
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <ncurses.h>
#include <math.h>
int countDigits(int num)
{
if (num < 0)
{
num = -num;
}
if (num == 0)
{
return 1;
}
return (int)log10(num) + 1;
}
#define CTRL_KEY(k) ((k) & 0x1f)
#define BACKSPACE 127
#define ENTER '\r'
#define ARROW_LEFT 1000
#define ARROW_RIGHT 1001
#define ARROW_UP 1002
#define ARROW_DOWN 1003
struct textEditorConfig
{
int cursorX, cursorY; // Cursor X and Y position
int rowOffset; // Row offset for scrolling
int colOffset; // Column offset for scrolling
int terminalRows; // Number of rows in the terminal
int terminalCols; // Number of columns in the terminal
int totalRows; // Number of rows in the buffer
char **textRows; // Buffer holding lines of text
char *saveFilename; // The filename to save to
struct termios originalTermios; // Original terminal attributes
};
struct textEditorConfig editorConfig;
/* Disable raw mode */
void disableRawInput()
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &editorConfig.originalTermios);
printf("\x1b[?25h"); // Show cursor
}
void handleSigint(int sig)
{
endwin(); // End ncurses mode
exit(0);
}
/* Enable raw mode */
void enableRawInput()
{
tcgetattr(STDIN_FILENO, &editorConfig.originalTermios);
atexit(disableRawInput);
struct termios raw = editorConfig.originalTermios;
raw.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP | IXON | ICRNL);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
/* Get window size */
int getTerminalSize(int *rows, int *cols)
{
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
{
return -1;
}
else
{
*cols = ws.ws_col;
*rows = ws.ws_row;
return 0;
}
}
/* Read key press with support for arrow keys */
int readKeyInput()
{
int nread;
char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1)
;
if (c == '\x1b')
{ // Escape sequence
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1)
return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1)
return '\x1b';
if (seq[0] == '[')
{
switch (seq[1])
{
case 'A':
return ARROW_UP;
case 'B':
return ARROW_DOWN;
case 'C':
return ARROW_RIGHT;
case 'D':
return ARROW_LEFT;
}
}
return '\x1b';
}
else
{
return c;
}
}
void moveCursor(int key)
{
switch (key)
{
case ARROW_LEFT:
if (editorConfig.cursorX > 0)
{
editorConfig.cursorX--;
}
else if (editorConfig.cursorY > 0)
{ // Move to end of previous line if possible
editorConfig.cursorY--;
editorConfig.cursorX = strlen(editorConfig.textRows[editorConfig.cursorY]);
}
break;
case ARROW_RIGHT:
if (editorConfig.cursorX < strlen(editorConfig.textRows[editorConfig.cursorY]))
{
editorConfig.cursorX++;
}
else if (editorConfig.cursorY < editorConfig.totalRows - 1)
{ // Move to start of next line
editorConfig.cursorY++;
editorConfig.cursorX = 0;
}
break;
case ARROW_UP:
if (editorConfig.cursorY > 0)
{
editorConfig.cursorY--;
if (editorConfig.cursorX > strlen(editorConfig.textRows[editorConfig.cursorY]))
editorConfig.cursorX = strlen(editorConfig.textRows[editorConfig.cursorY]);
}
break;
case ARROW_DOWN:
if (editorConfig.cursorY < editorConfig.totalRows - 1)
{
editorConfig.cursorY++;
if (editorConfig.cursorX > strlen(editorConfig.textRows[editorConfig.cursorY]))
editorConfig.cursorX = strlen(editorConfig.textRows[editorConfig.cursorY]);
}
break;
}
if (editorConfig.cursorY < editorConfig.rowOffset)
{
editorConfig.rowOffset = editorConfig.cursorY;
}
else if (editorConfig.cursorY >= editorConfig.rowOffset + editorConfig.terminalRows)
{
editorConfig.rowOffset = editorConfig.cursorY - editorConfig.terminalRows + 1;
}
if (editorConfig.cursorX < editorConfig.colOffset)
{
editorConfig.colOffset = editorConfig.cursorX;
}
else if (editorConfig.cursorX >= editorConfig.colOffset + editorConfig.terminalCols)
{
editorConfig.colOffset = editorConfig.cursorX - editorConfig.terminalCols + 1;
}
}
void refreshScreen()
{
clear();
for (int y = 0; y < editorConfig.terminalRows; y++)
{
int fileRow = y + editorConfig.rowOffset;
if (fileRow >= editorConfig.totalRows)
{
mvprintw(y, 0, "~");
}
else
{
attron(COLOR_PAIR(1)); // Activate color pair 1
mvprintw(y, 0, "%d ", fileRow + 1);
attroff(COLOR_PAIR(1));
// attron(COLOR_PAIR(1)); // Activate color pair 1
mvprintw(y, 2, "%s", editorConfig.textRows[fileRow]);
}
}
int charCount = 0;
for (int i = 0; i < editorConfig.totalRows; i++)
{
charCount += strlen(editorConfig.textRows[i]);
}
int digit_offset = countDigits(editorConfig.totalRows + 1) + 1;
mvprintw(LINES - 2, 0, "Characters: %d | Rows: %d | Position: %d:%d", charCount, editorConfig.totalRows, editorConfig.cursorY + 1, editorConfig.cursorX + 1);
move(editorConfig.cursorY - editorConfig.rowOffset, editorConfig.cursorX + digit_offset); // Move cursor to its position
refresh();
}
void insertRow(int at, char *s, size_t len)
{
if (at < 0 || at > editorConfig.totalRows)
return;
editorConfig.textRows = realloc(editorConfig.textRows, sizeof(char *) * (editorConfig.totalRows + 1));
memmove(&editorConfig.textRows[at + 1], &editorConfig.textRows[at], sizeof(char *) * (editorConfig.totalRows - at));
editorConfig.textRows[at] = malloc(len + 1);
memcpy(editorConfig.textRows[at], s, len);
editorConfig.textRows[at][len] = '\0';
editorConfig.totalRows++;
}
void insertChar(int c)
{
if (editorConfig.cursorY >= editorConfig.totalRows)
{
insertRow(editorConfig.totalRows, "", 0);
}
char *row = editorConfig.textRows[editorConfig.cursorY];
size_t linelen = strlen(row);
row = realloc(row, linelen + 2); // +2 for new char and null-terminator
memmove(&row[editorConfig.cursorX + 1], &row[editorConfig.cursorX], linelen - editorConfig.cursorX + 1);
row[editorConfig.cursorX] = c;
editorConfig.textRows[editorConfig.cursorY] = row;
editorConfig.cursorX++;
// Wrap to the next line if we reach the end of the current line
if (editorConfig.cursorX >= editorConfig.terminalCols)
{
editorConfig.cursorX = 0;
if (editorConfig.cursorY < editorConfig.totalRows - 1)
{
editorConfig.cursorY++;
}
else
{
insertRow(editorConfig.totalRows, "", 0);
editorConfig.cursorY++;
}
}
}
/* Insert a new line at the cursor position */
void insertNewLine()
{
if (editorConfig.cursorX == 0)
{
insertRow(editorConfig.cursorY, "", 0);
}
else
{
char *row = editorConfig.textRows[editorConfig.cursorY];
insertRow(editorConfig.cursorY + 1, &row[editorConfig.cursorX], strlen(row) - editorConfig.cursorX);
editorConfig.textRows[editorConfig.cursorY][editorConfig.cursorX] = '\0';
}
editorConfig.cursorY++;
editorConfig.cursorX = 0;
}
/* Delete a character at the cursor position */
void deleteChar()
{
if (editorConfig.cursorY >= editorConfig.totalRows || (editorConfig.cursorX == 0 && editorConfig.cursorY == 0))
return;
char *row = editorConfig.textRows[editorConfig.cursorY];
if (editorConfig.cursorX > 0)
{
memmove(&row[editorConfig.cursorX - 1], &row[editorConfig.cursorX], strlen(row) - editorConfig.cursorX + 1);
editorConfig.cursorX--;
}
else
{
// Merge with previous row
size_t prev_len = strlen(editorConfig.textRows[editorConfig.cursorY - 1]);
size_t curr_len = strlen(row);
editorConfig.textRows[editorConfig.cursorY - 1] = realloc(editorConfig.textRows[editorConfig.cursorY - 1], prev_len + curr_len + 1);
memcpy(&editorConfig.textRows[editorConfig.cursorY - 1][prev_len], row, curr_len + 1);
free(editorConfig.textRows[editorConfig.cursorY]);
memmove(&editorConfig.textRows[editorConfig.cursorY], &editorConfig.textRows[editorConfig.cursorY + 1], sizeof(char *) * (editorConfig.totalRows - editorConfig.cursorY - 1));
editorConfig.totalRows--;
editorConfig.cursorY--;
editorConfig.cursorX = prev_len; // Set cursorX to the end of the previous line
}
}
/* Save the editor content to a file */
void saveFile()
{
if (!editorConfig.saveFilename)
{
char filename[256];
mvprintw(LINES - 1, 0, "Save as: ");
echo();
getnstr(filename, sizeof(filename) - 1);
noecho();
editorConfig.saveFilename = strdup(filename);
}
FILE *fp = fopen(editorConfig.saveFilename, "w");
if (!fp)
{
perror("Unable to save file");
return;
}
for (int i = 0; i < editorConfig.totalRows; i++)
{
fprintf(fp, "%s\n", editorConfig.textRows[i]);
}
fclose(fp);
mvprintw(LINES - 1, 0, "File saved as %s ", editorConfig.saveFilename);
}
void processKeypress()
{
int c = readKeyInput();
switch (c)
{
case CTRL_KEY('q'): // Quit
disableRawInput();
printf("\x1b[2J\x1b[H"); // Clear the screen
exit(0);
break;
case CTRL_KEY('s'): // Save
saveFile();
break;
case CTRL('c'): // CTRL+C to quit
endwin(); // End ncurses mode
exit(0);
case BACKSPACE:
deleteChar();
break;
case ENTER:
insertNewLine();
break;
case ARROW_LEFT:
case ARROW_RIGHT:
case ARROW_UP:
case ARROW_DOWN:
moveCursor(c);
break;
default:
if (!iscntrl(c))
{
insertChar(c);
}
break;
}
}
void initializeEditor()
{
editorConfig.cursorX = 0;
editorConfig.cursorY = 0;
editorConfig.rowOffset = 0;
editorConfig.colOffset = 0;
editorConfig.totalRows = 0;
editorConfig.textRows = NULL;
editorConfig.saveFilename = NULL;
if (getTerminalSize(&editorConfig.terminalRows, &editorConfig.terminalCols) == -1)
{
perror("getTerminalSize");
exit(1);
}
}
int main()
{
signal(SIGINT, handleSigint); // Handle CTRL+C
initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
// start_color();
init_color(COLOR_CYAN, 0, 255, 255);
init_pair(1, COLOR_CYAN, COLOR_BLACK);
initializeEditor();
enableRawInput();
initializeEditor();
while (1)
{
refreshScreen();
processKeypress();
}
return 0;
}