Repository navigation
Expand file tree
/
Copy patheditor.cpp
More file actions
237 lines (215 loc) · 6.63 KB
/
Copy patheditor.cpp
File metadata and controls
237 lines (215 loc) · 6.63 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
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <cstdio>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <sys/ioctl.h>
#define CTRL_KEY(k) ((k) & 0x1f)
struct EditorConfig {
int cx, cy;
int rows, cols;
int rowoff;
std::string filename;
std::vector<std::string> lines;
std::string last_query; // last search term
int match_line = -1; // line where match was found (-1 = no match)
int match_col = -1; // column where match starts
};
EditorConfig E;
class Terminal {
public:
Terminal() {
tcgetattr(STDIN_FILENO, &orig_termios);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON | ISIG);
raw.c_iflag &= ~(IXON); // disable Ctrl-S/Ctrl-Q flow control
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
~Terminal() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
private:
struct termios orig_termios;
};
void search() {
std::string query;
while (true) {
char c;
read(STDIN_FILENO, &c, 1);
if (c == 27) return; // Escape = cancel
else if (c == 10) break; // Enter = search
else if (c == 127) { // Backspace
if (!query.empty()) query.pop_back();
} else {
query += c; // add character
}
}
// search through lines
for (int i = 0; i < (int)E.lines.size(); i++) {
size_t pos = E.lines[i].find(query);
if (pos != std::string::npos) {
E.cy = i;
E.cx = pos;
E.match_line = i;
E.match_col = pos;
E.last_query = query;
break;
}
}
}
void drawStatusBar() {
write(STDOUT_FILENO, "\x1b[7m", 4); // inverted colors on
char status[80];
int len = snprintf(status, sizeof(status), " %s | %d lines | L%d C%d",
E.filename.empty() ? "[No Name]" : E.filename.c_str(),
(int)E.lines.size(),
E.cy + 1,
E.cx + 1);
if (len > E.cols) len = E.cols;
write(STDOUT_FILENO, status, len);
// pad the rest of the line with spaces so the bar spans full width
for (int i = len; i < E.cols; i++)
write(STDOUT_FILENO, " ", 1);
write(STDOUT_FILENO, "\x1b[m", 3); // inverted colors off
write(STDOUT_FILENO, "\r\n", 2);
}
void saveFile() {
if (E.filename.empty()) return;
std::ofstream file(E.filename);
for (int i = 0; i < (int)E.lines.size(); i++) {
file << E.lines[i] << "\n";
}
}
void loadFile(const char* filename) {
E.filename = filename;
std::ifstream file(filename);
std::string line;
while (std::getline(file, line)) {
E.lines.push_back(line);
}
}
void initEditor() {
struct winsize ws;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
E.rows = ws.ws_row - 1;
E.cols = ws.ws_col;
E.cx = 0;
E.cy = 0;
E.rowoff = 0;
}
void scroll() {
if (E.cy < E.rowoff)
E.rowoff = E.cy;
if (E.cy >= E.rowoff + E.rows)
E.rowoff = E.cy - E.rows + 1;
}
void drawRows() {
for (int i = 0; i < E.rows; i++) {
int filerow = i + E.rowoff;
if (filerow < (int)E.lines.size()) {
int len = (int)E.lines[filerow].size();
if (len > E.cols) len = E.cols;
if (filerow == E.match_line && E.match_col < len) {
// write before match
int before = E.match_col;
write(STDOUT_FILENO, E.lines[filerow].c_str(), before);
// write match highlighted
write(STDOUT_FILENO, "\x1b[7m", 4);
int mlen = E.last_query.size();
if ((size_t)(E.match_col + mlen) > (size_t)len) mlen = len - E.match_col;
write(STDOUT_FILENO, E.lines[filerow].c_str() + E.match_col, mlen);
write(STDOUT_FILENO, "\x1b[m", 3);
// write after match
write(STDOUT_FILENO, E.lines[filerow].c_str() + E.match_col + mlen,
len - E.match_col - mlen);
} else {
write(STDOUT_FILENO, E.lines[filerow].c_str(), len);
}
}
else {
write(STDOUT_FILENO, "~", 1);
}
write(STDOUT_FILENO, "\r\n", 2);
}
}
void refreshScreen() {
scroll();
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[1;1H", 6); // explicit row 1, col 1
drawRows();
drawStatusBar();
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) +1, E.cx + 1);
write(STDOUT_FILENO, buf, strlen(buf));
}
void readInput() {
while (true) {
refreshScreen();
char c;
read(STDIN_FILENO, &c, 1);
if (c == CTRL_KEY('q')) {
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
break;
}
else if (c == CTRL_KEY('s')) {
saveFile();
}
else if (c == CTRL_KEY('f')) {
search();
}
else if (c == '\x1b') {
char seq[2];
read(STDIN_FILENO, &seq[0], 1);
read(STDIN_FILENO, &seq[1], 1);
if (seq[0] == '[') {
switch (seq[1]) {
case 'A': // up
if (E.cy > 0) E.cy--;
if (E.cx > (int)E.lines[E.cy].size())
E.cx = E.lines[E.cy].size();
break;
case 'B': // down
if (E.cy < (int)E.lines.size() - 1) E.cy++;
if (E.cx > (int)E.lines[E.cy].size())
E.cx = E.lines[E.cy].size();
break;
case 'C': // right
if (E.cx < (int)E.lines[E.cy].size()) E.cx++;
break;
case 'D': // left
if (E.cx > 0) E.cx--;
break;
}
}
}
else if (c == 10 || c == '\r') {
std::string rest = E.lines[E.cy].substr(E.cx);
E.lines[E.cy] = E.lines[E.cy].substr(0, E.cx);
E.lines.insert(E.lines.begin() + E.cy + 1, rest);
E.cy++;
E.cx = 0;
}
else if (c == 127) {
if (E.cx > 0) {
E.lines[E.cy].erase(E.cx - 1, 1);
E.cx--;
}
}
else if (c >= 32 && c < 127) {
E.lines[E.cy].insert(E.cx, 1, c);
E.cx++;
}
}
}
int main(int argc, char* argv[]) {
Terminal term;
initEditor();
if (argc >= 2) loadFile(argv[1]);
if (E.lines.empty()) E.lines.push_back(""); // always have at least one line
readInput();
return 0;
}