-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotWorking_main.c
169 lines (144 loc) · 3.85 KB
/
notWorking_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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include "errorHandler.h"
#include "modeChanger.h"
#include "keyProcessing.h"
#include "editorSpecs.h"
#include "cursorPosition.h"
#define ctrl_key(k) ((k) & 0x1f)
// STEPS:
// 1. Enter Raw Mode -- DONE!
// 2. Handle Raw I/O -- DONE!
// 3. Create Text Viewer Window/Screen -- No scrolling and escape seqs, else DONE!
// 4. Create the editor (main features)
char keyRead() {
int bytes_read;
char c;
while ((bytes_read=read(STDIN_FILENO, &c, 1))!=1) {
if (bytes_read==-1 && errno!=EAGAIN){
throwErrorMsg("read");
}
}
return c;
}
void moveCursorEditor(char key) {
if(key=='a' && editor.curs_x > 0) {
editor.curs_x--;
}
else if(key=='d' && editor.curs_x < editor.number_of_cols - 1) {
editor.curs_x++;
}
else if(key=='w' && editor.curs_y > 0) {
editor.curs_y--;
}
else if(key=='s' && editor.curs_y < editor.number_of_rows - 1) {
editor.curs_y++;
}
}
if (E.cx != E.screencols - 1)
void keypressEditor() {
char c = keyRead();
if(c==ctrl_key('q')){
exit(0);
}
else if(c=='w' || c=='a' || c=='s' || c=='d'){
moveCursorEditor(c);
}
}
typedef struct editor_row {
int len;
char *chars;
} editor_row;
struct EditorData{
int curs_x, curs_y;
int number_of_rows, number_of_cols;
struct termios orig;
editor_row *erow;
};
struct EditorData editor;
struct appendBuffer{
char *ptrBuf;
int len;
};
void appendBuffer_concat(struct appendBuffer *ab2, const char *s, int len) {
char *newBuf = realloc(ab2->ptrBuf, ab2->len + len); // increase buf size
if (newBuf == NULL) return;
memcpy(&newBuf[ab2->len], s, len);
ab2->ptrBuf = newBuf;
ab2->len += len;
}
// Destructor: appendBuffer
void appendBuffer_free(struct appendBuffer *ab2) {
free(ab2->ptrBuf);
}
void init(){
editor.curs_x = 0;
editor.curs_y = 0;
editor.erow = NULL;
int status = findWinSz(&editor.number_of_rows,&editor.number_of_cols);
if(status==-1){
throwErrorMsg("findWinSz");
}
}
void drawEditor(struct appendBuffer *appBuff) {
for (int i = 0; i < editor.number_of_rows; i++) {
appendBuffer_concat(appBuff, "~", 1);
appendBuffer_concat(appBuff, "\x1b[K", 3); // clear each line before redrawing
if (i < editor.number_of_rows - 1) appendBuffer_concat(appBuff, "\r\n", 2);
}
}
void refreshEditor() {
struct appendBuffer AB = {NULL, 0}; // constructor
// Anukool: Check VT100 manual...
appendBuffer_concat(&AB, "\x1b[?25l", 6); // hide cursor: VT100
appendBuffer_concat(&AB, "\x1b[H", 3);
drawEditor(&AB);
appendBuffer_concat(&AB, "\x1b[H", 3);
appendBuffer_concat(&AB, "\x1b[?25h", 6); // show/reset cursor: VT100
write(STDOUT_FILENO, AB.ptrBuf, AB.len);
appendBuffer_free(&AB);
}
void setTerminal() {
struct appendBuffer AB = {NULL, 0}; // constructor
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
drawEditor(&AB);
write(STDOUT_FILENO, "\x1b[H", 3);
}
void appendToEditor(char *s, size_t len) {
editor.erow = realloc(E.row, sizeof(erow) * (editor.typedrows + 1));
int app_index = editor.typedrows;
editor.erow[app_index].size = len;
editor.erow[app_index].chars = malloc(len + 1);
memcpy(editor.erow[app_index].chars, s, len);
editor.erow[app_index].chars[len] = '\0';
editor.typedrows++;
}
void openfile(char *fname) {
FILE *file = fopen(fname, "r");
if (!file) throwErrorMsg("fopen");
char *line = NULL;
size_t line_capacity = 0;
ssize_t length;
while ((length = getline(&line, &line_capacity, fp)) != -1) {
while (length > 0 && (line[length - 1] == '\n' || line[length - 1] == '\r')) length--;
appendToEditor(line, length);
}
free(line);
fclose(file);
}
int main(int argc, char *argv[]){
enterRawMode();
init();
if (argc>1) openfile(argv[1]);
while (1) {
setTerminal();
keypressEditor();
}
return 0;
}