-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscb.c
460 lines (394 loc) · 8.42 KB
/
scb.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
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
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <ncurses.h>
#ifdef KEY_ENTER
#undef KEY_ENTER
#endif
#define KEY_ENTER 0x0A
#define KEY_ESC 27
#define DEFAULT_EDITOR "vim"
#define SCROLL_UP BUTTON4_PRESSED
#define SCROLL_DOWN 0x200000
struct Lines {
char** lines;
size_t len;
size_t cap;
size_t selected;
};
static char* search_term = NULL;
struct Lines* new_lines()
{
const size_t cap = 1024;
struct Lines* l = calloc(1, sizeof (struct Lines));
assert(l);
l->lines = malloc(cap * sizeof (char*));
l->cap = cap;
l->selected = -1;
return l;
}
void add_line(struct Lines* l, char* line)
{
size_t len = strlen(line);
if (l->len == l->cap) {
l->cap *= 2;
l->lines = realloc(l->lines, l->cap * sizeof (char*));
assert(l->lines);
}
if (line[len - 1] == '\n') {
line[len - 1] = 0;
}
l->lines[l->len] = line;
l->len++;
}
void select_down(struct Lines* l, size_t n)
{
if ((l->selected + n) < l->len)
l->selected += n;
else
l->selected = l->len - 1;
}
void select_up(struct Lines* l, size_t n)
{
if (l->selected >= n)
l->selected -= n;
else
l->selected = 0;
}
void select_line(struct Lines* l, size_t n)
{
if (n < 0) {
l->selected = 0;
} else if (n >= l->len) {
l->selected = l->len - 1;
} else {
l->selected = n;
}
}
bool find(struct Lines* l, char* substr)
{
if (!substr)
return false;
for (size_t i = l->selected + 1; i < l->len; i++) {
char* tok = strstr(l->lines[i], substr);
if (tok) {
l->selected = i;
return true;
}
}
return false;
}
void reset_stds()
{
FILE* fd = fopen("/dev/tty", "r+");
close(0);
dup(fileno(fd));
close(1);
dup(fileno(fd));
fclose(fd);
}
void init_screen()
{
reset_stds();
initscr();
cbreak();
noecho();
curs_set(0);
clear();
}
void auto_select(struct Lines* l)
{
int rows, col;
(void) col; /* GCC claims col is unused. clang behaves. */
/* TODO: Separate this code from ncurses. */
getmaxyx(stdscr, rows, col);
if (l->len < rows) {
l->selected = l->len / 2;
} else {
l->selected = rows / 2;
}
}
#define TAB_STRING " "
#define TAB_LENGTH (sizeof (TAB_STRING))
void draw_line(int row, const char* line, bool highlight)
{
int rows, cols;
size_t len = strlen(line);
int char_count = 0;
(void) rows; /* Compiler is complaining that rows is unused */
if (highlight)
attrset(A_STANDOUT);
else
attrset(A_NORMAL);
getmaxyx(stdscr, rows, cols);
/*
Using mvprintw causes the x,y coords to get
out of sync when tabs are in the line.
Therefore, replace the tabs with spaces and
keep track of the char bytes.
All in the name of highlighting the entire row.
*/
/* mvprintw(row, 0, "%.*s", cols, line); */
for (int i = 0; i < len; i++) {
if (line[i] == '\t') {
mvprintw(row, char_count, TAB_STRING);
char_count += TAB_LENGTH - 1;
} else {
mvaddch(row, char_count, line[i]);
char_count++;
}
}
/* Fill the rest of the row with spaces to highlight the entire row. */
if (cols > char_count)
mvprintw(row, char_count, "%*c", cols - char_count, ' ');
}
void draw_frame(struct Lines* l, size_t offset)
{
int rows, cols;
(void) cols; /* GCC claims col is unused. clang behaves. */
getmaxyx(stdscr, rows, cols);
rows--;
for (int i = offset; (i - offset) < rows && i < l->len; i++) {
bool highlight = i == l->selected;
draw_line(i - offset, l->lines[i], highlight);
}
}
const char* status_msg(const char* msg)
{
static const char* current = NULL;
const char* old = current;
current = msg;
return old;
}
void draw_statusbar(struct Lines* l)
{
int n;
int rows, cols;
(void) cols; /* GCC claims col is unused. clang behaves. */
const char* msg = status_msg(NULL);
getmaxyx(stdscr, rows, cols);
attrset(A_DIM);
mvprintw(rows - 1, 0, ":: scb (%lu,%zu) :::: %n", l->selected + 1, l->len, &n);
if (msg) {
attrset(A_BLINK | A_DIM);
mvprintw(rows - 1, n, "%s", msg);
}
}
void draw(struct Lines* l)
{
size_t offset;
int rows, cols;
(void) cols; /* GCC claims col is unused. clang behaves. */
clear();
getmaxyx(stdscr, rows, cols);
rows--; /* Leave the bottom row for the status bar */
if (l->selected <= rows / 2 || rows >= l->len) {
offset = 0;
} else if (l->selected >= l->len - (rows / 2)){
offset = l->len - rows;
} else {
offset = l->selected - (rows / 2);
}
draw_frame(l, offset);
draw_statusbar(l);
refresh();
}
void parse_line(char* line, char** file, char** linenum)
{
char* tok = strchr(line, ':');
*file = NULL;
*linenum = NULL;
if (!tok) {
/* TODO: Why are we strduping this? */
*file = strdup(line);
return;
}
*file = strndup(line, tok - line);
tok = strchr(tok + 1, ':');
if (!tok) {
/* TODO: Why are we setting this to NULL, again? */
*linenum = NULL;
return;
}
*linenum = line + strlen(*file) + 1;
*linenum = strndup(*linenum - 1, tok - *linenum + 1);
**linenum = '+';
}
void open_line(char* line)
{
int status;
char* file;
char* linenum;
char* editor = getenv("EDITOR");
if (!editor) {
editor = DEFAULT_EDITOR;
}
/* TODO: Check if editor exists. This saves us from worrying about
execlp failing due to the command not being found. */
parse_line(line, &file, &linenum);
if (!file || !linenum) {
/* TODO: Report this error on the status bar. */
clear();
mvprintw(0, 0, "Error parsing line.");
refresh();
getch();
return;
}
/* TODO: Do error checking and reporting on the parsed line. */
def_prog_mode();
endwin();
pid_t pid = fork();
if (pid > 0) {
free(file);
free(linenum);
waitpid(pid, &status, 0);
if (WEXITSTATUS(status) != 0) {
(void) status_msg("Failed to open editor.");
}
reset_prog_mode();
refresh();
} else if (pid == 0) {
if (execlp(editor, editor, linenum, file, NULL) == -1) {
fprintf(stderr, "Failed to run %s: %s\n", editor, strerror(errno));
exit(2);
}
} else {
fprintf(stderr, "Failed to fork: %s\n", strerror(errno));
exit(1);
}
}
char* command_input(const char* prefix)
{
int c;
int last, rows, cols;
(void) cols; /* GCC claims col is unused. clang behaves. */
char* buf = malloc(512);
char* p = buf;
assert(buf);
getmaxyx(stdscr, rows, cols);
attrset(A_DIM);
last = rows - 1;
echo();
curs_set(1);
mvprintw(last, 0, "%s ", prefix);
clrtoeol();
nocbreak();
while ((c = getch()) != KEY_ENTER) {
if (buf - p == 512 - 1) {
break;
}
*p = c;
p++;
}
*p = '\0';
noecho();
curs_set(0);
cbreak();
return buf;
}
void input_loop(struct Lines* l)
{
int c;
mousemask(ALL_MOUSE_EVENTS, NULL);
keypad(stdscr, TRUE);
auto_select(l);
draw(l);
while ((c = getch()) != 'q') {
switch (c) {
case KEY_MOUSE: {
MEVENT event;
if (getmouse(&event) == OK) {
if (event.bstate & BUTTON3_CLICKED) {
open_line(l->lines[l->selected]);
} else if (event.bstate & SCROLL_UP) {
select_up(l, 1);
} else if (event.bstate & SCROLL_DOWN) {
select_down(l, 1);
}
}
} break;
case KEY_UP: {
select_up(l, 1);
} break;
case KEY_DOWN: {
select_down(l, 1);
} break;
case KEY_PPAGE: {
int rows, col;
(void) col; /* GCC claims col is unused. clang behaves. */
getmaxyx(stdscr, rows, col);
select_up(l, rows / 2);
} break;
case KEY_NPAGE: {
int rows, col;
(void) col; /* GCC claims col is unused. clang behaves. */
getmaxyx(stdscr, rows, col);
select_down(l, rows / 2);
} break;
case KEY_ENTER: {
open_line(l->lines[l->selected]);
} break;
case KEY_HOME: {
select_line(l, 0);
} break;
case KEY_END: {
select_line(l, l->len);
} break;
case '/': {
free(search_term);
search_term = command_input(" search: ");
if (search_term) {
find(l, search_term);
}
} break;
case 'n': {
if (search_term) {
find(l, search_term);
} else {
search_term = command_input(" search: ");
find(l, search_term);
}
} break;
case ':': {
char* t = command_input(" goto: ");
select_line(l, atoi(t) - 1);
free(t);
} break;
default: {
status_msg("See the manpage for help");
} break;
}
draw(l);
}
}
int main(int argc, char** argv)
{
struct Lines* l = new_lines();
{
char* line = NULL;
size_t len = 0;
while (getline(&line, &len, stdin) != -1) {
add_line(l, line);
line = NULL;
len = 0;
}
}
if (l->len == 0) {
fprintf(stderr, "Nothing to do.\n");
return 0;
}
init_screen();
input_loop(l);
endwin();
/* Clean up memory */
for (size_t i = 0; i < l->len; i++) {
free(l->lines[i]);
}
free(l->lines);
free(l);
free(search_term);
return 0;
}