-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclimenu.c
659 lines (474 loc) · 14 KB
/
climenu.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <time.h>
#include <termios.h>
#include <stdint.h>
#define COLOR_DEFAULT ""
#define COLOR_RESET "\033[0m"
#define COLOR_FG_BLACK "\033[30m"
#define COLOR_FG_RED "\033[31m"
#define COLOR_FG_GREEN "\033[32m"
#define COLOR_FG_YELLOW "\033[33m"
#define COLOR_FG_BLUE "\033[34m"
#define COLOR_FG_MAGENTA "\033[35m"
#define COLOR_FG_CYAN "\033[36m"
#define COLOR_FG_WHITE "\033[37m"
#define COLOR_BG_BLACK "\033[40m"
#define COLOR_BG_RED "\033[41m"
#define COLOR_BG_GREEN "\033[42m"
#define COLOR_BG_YELLOW "\033[43m"
#define COLOR_BG_BLUE "\033[44m"
#define COLOR_BG_MAGENTA "\033[45m"
#define COLOR_BG_CYAN "\033[46m"
#define COLOR_BG_WHITE "\033[47m"
#define ALLOCSIZ 64
/* Key codes */
#define KEY_J 0x6A
#define KEY_K 0x6B
#define KEY_DOWN 0x425B1B
#define KEY_UP 0x415B1B
#define KEY_ENTER 0xD
#define KEY_SPACE 0x20
#define KEY_Q 0x71
#define KEY_CTRLC 0x03
#define PROPERTY(key_str) {.key = key_str, .val = NULL }
#define COUNT_OF(arr) (sizeof arr / sizeof *arr)
void select_next(void);
void select_prev(void);
void clean_exit(void);
enum ColorMode {
CM_NONE = 0,
CM_SELECTED
};
enum EntryType {
ET_NONE = 0,
ET_REG,
ET_HEADER
};
struct EntryKey {
char* key;
char* val;
int type;
};
struct Entry {
struct Entry* next;
struct Entry* prev;
struct EntryKey* keys;
size_t keys_count;
size_t index;
char type;
};
struct EntryKey g_parameter_keys[] = {
PROPERTY("str"),
PROPERTY("fgcolor"),
PROPERTY("bgcolor"),
PROPERTY("exec"),
PROPERTY("colormode"),
PROPERTY("wait"),
PROPERTY("exit"),
PROPERTY("runstr")
};
struct Entry* g_selected = NULL;
struct Entry* g_head = NULL;
size_t g_entry_count = 0;
struct termios g_termios_original;
struct winsize g_window;
void error_exit(char* msg) {
printf("%s\n", msg);
exit(EXIT_FAILURE);
}
void* xrealloc(void* buf, size_t size) {
buf = realloc(buf, size);
if (buf == NULL)
error_exit("realloc failed");
return buf;
}
void* xmalloc(size_t size) {
void* ptr = malloc(size);
if (ptr == NULL)
error_exit("malloc failed");
return ptr;
}
void* xcalloc(size_t n, size_t size) {
void* ptr = calloc(n, size);
if (ptr == NULL)
error_exit("calloc failed");
return ptr;
}
int sleep_ms(long ms) {
struct timespec ts;
int res;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res);
return res;
}
void clear_screen(void) {
system("clear");
}
size_t get_filesize(FILE* file_ptr) {
size_t size;
fseek(file_ptr, 0, SEEK_END);
size = ftell(file_ptr);
rewind(file_ptr);
return size;
}
char* read_file(char* path) {
FILE* file_ptr;
size_t file_size;
char* buf;
file_ptr = fopen(path, "r");
if (file_ptr == NULL)
error_exit("failed to open file");
file_size = get_filesize(file_ptr);
buf = xcalloc(1, file_size + 1);
fread(buf, 1, file_size, file_ptr);
fclose(file_ptr);
return buf;
}
char* read_stdin(void) {
char* buf = NULL;
char ch;
size_t i, allocated_size;
i = allocated_size = 0;
while ((ch = getchar()) != EOF) {
if (i >= allocated_size) {
allocated_size += ALLOCSIZ;
buf = xrealloc(buf, allocated_size);
}
buf[i++] = ch;
}
if (i >= allocated_size)
buf = xrealloc(buf, allocated_size + 1);
buf[i] = '\0';
/* Set stdin to tty input */
freopen("/dev/tty", "r", stdin);
return buf;
}
int string_startswith(char* a, char* b) {
while (*a) {
if (*b == '\0')
return 1;
if (*(a++) != *(b++))
return 0;
}
return 1;
}
char* string_dup(char* str) {
char* new_str = xcalloc(1, strlen(str) + 1);
strcpy(new_str, str);
return new_str;
}
char* next_line(char* ptr) {
while (*ptr && *(ptr++) != '\n')
;
return (*ptr) ? ptr : NULL;
}
char* get_value(char* ptr) {
char buf[BUFSIZ] = {0};
while (*(ptr++) && *(ptr - 1) != '=')
;
for (int i = 0; i < BUFSIZ - 1 && *ptr && *ptr != '\n'; ++i)
buf[i] = *(ptr++);
return string_dup(buf);
}
struct Entry* entry_create(enum EntryType type) {
struct Entry* new_entry = xcalloc(1, sizeof *new_entry);;
new_entry->type = type;
new_entry->index = g_entry_count++;
return new_entry;
}
void entry_append(struct Entry* head, struct Entry* node) {
while (head->next)
head = head->next;
head->next = node;
node->prev = head;
}
void entry_destroy(struct Entry* entry) {
if (entry->keys)
for (size_t i = 0; i < entry->keys_count; ++i)
free(entry->keys[i].val);
free(entry->keys);
free(entry);
}
char* parse_color(char* color, int fg) {
if (color == NULL)
return NULL;
if (strcmp(color, "black") == 0) return fg ? COLOR_FG_BLACK : COLOR_BG_BLACK;
if (strcmp(color, "blue") == 0) return fg ? COLOR_FG_BLUE : COLOR_BG_BLUE;
if (strcmp(color, "cyan") == 0) return fg ? COLOR_FG_CYAN : COLOR_BG_CYAN;
if (strcmp(color, "green") == 0) return fg ? COLOR_FG_GREEN : COLOR_BG_GREEN;
if (strcmp(color, "magenta") == 0) return fg ? COLOR_FG_MAGENTA : COLOR_BG_MAGENTA;
if (strcmp(color, "red") == 0) return fg ? COLOR_FG_RED : COLOR_BG_RED;
if (strcmp(color, "white") == 0) return fg ? COLOR_FG_WHITE : COLOR_BG_WHITE;
if (strcmp(color, "yellow") == 0) return fg ? COLOR_FG_YELLOW : COLOR_BG_YELLOW;
return "";
}
void entry_copy_keys(struct Entry* entry) {
int j = 0;
entry->keys = xmalloc(entry->keys_count * sizeof *entry->keys);
for (size_t i = 0; i < COUNT_OF(g_parameter_keys); ++i) {
if (g_parameter_keys[i].val) {
entry->keys[j++] = g_parameter_keys[i];
g_parameter_keys[i].val = NULL;
}
}
}
struct Entry* parse_entry(char* ptr) {
struct Entry* entry;
if (string_startswith(ptr, "[Entry]")) entry = entry_create(ET_REG);
else if (string_startswith(ptr, "[Header]")) entry = entry_create(ET_HEADER);
else return NULL;
while ( (ptr = next_line(ptr)) ) {
if (string_startswith(ptr, "["))
break;
for (size_t i = 0; i < COUNT_OF(g_parameter_keys); ++i) {
if (string_startswith(ptr, g_parameter_keys[i].key)) {
g_parameter_keys[i].val = get_value(ptr);
++entry->keys_count;
}
}
}
entry_copy_keys(entry);
return entry;
}
struct Entry* parse_entries(char* file_content) {
struct Entry* entry_head = NULL;
struct Entry* entry_curr = NULL;
char* ptr = file_content;
do {
entry_curr = string_startswith(ptr, "[") ? parse_entry(ptr) : NULL;
if (entry_curr)
if (entry_head)
entry_append(entry_head, entry_curr);
else
entry_head = entry_curr;
} while ( (ptr = next_line(ptr)) );
free(file_content);
return entry_head;
}
void select_entry(struct Entry* entry) {
if (entry->type != ET_REG)
select_entry(entry->next);
else
g_selected = entry;
}
void select_next(void) {
if (g_selected->next)
g_selected = g_selected->next;
else if (g_selected->type != ET_REG)
select_prev();
if (g_selected->type != ET_REG)
select_next();
}
void select_prev(void) {
if (g_selected->prev)
g_selected = g_selected->prev;
else if (g_selected->type != ET_REG)
select_next();
if (g_selected->type != ET_REG)
select_prev();
}
void cursor_pos(int x, int y) {
printf("\033[%d;%df", y, x);
}
void cursor_visible(int vis) {
printf(vis ? "\033[?25h" : "\033[?25l");
}
char* get_key(struct Entry* entry, char* key) {
for (size_t i = 0; i < entry->keys_count; ++i)
if (strcmp(key, entry->keys[i].key) == 0)
return entry->keys[i].val;
return "";
}
char* cmd_output(char* cmd) {
FILE *file_ptr;
char buf[BUFSIZ];
file_ptr = popen(cmd, "r");
if (file_ptr == NULL) {
perror("popen");
exit(1);
}
while (fgets(buf, sizeof(buf) - 1, file_ptr) != NULL) {
char* ptr = buf;
while (*ptr && *ptr != '\n')
++ptr;
if (*ptr == '\n')
*ptr = '\0';
}
pclose(file_ptr);
return string_dup(buf);
}
void entry_print(struct Entry* entry, int selected) {
char* entry_str = NULL;
int allow_color = 1;
if (entry->type == ET_REG && strcmp(get_key(entry, "colormode"), "selected") == 0)
allow_color = selected;
printf("%s%s%s%s%s%-*s",
entry->type == ET_HEADER ? "\033[4m" : "",
allow_color ? parse_color(get_key(entry, "fgcolor"), 1) : "",
allow_color ? parse_color(get_key(entry, "bgcolor"), 0) : "",
*get_key(entry, "runstr") ? entry_str = cmd_output(get_key(entry, "str")) : get_key(entry, "str"),
selected ? " (*)" : "",
g_window.ws_col,
COLOR_RESET);
free(entry_str);
}
void draw(struct Entry* head) {
struct Entry* cursor = head;
int x, y;
ioctl(0, TIOCGWINSZ, &g_window);
/* Scrolling */
for (size_t i = 0; (g_window.ws_row + i) <= g_selected->index; ++i)
if (cursor->next)
cursor = cursor->next;
x = y = 0;
while (cursor) {
if (++y <= g_window.ws_row)
cursor_pos(x, y);
else
break;
if (cursor->type == ET_HEADER)
entry_print(cursor, 0);
if (cursor->type == ET_REG)
entry_print(cursor, (cursor == g_selected));
cursor = cursor->next;
}
fflush(stdout);
}
void restore_terminal_mode(void) {
tcsetattr(0, TCSANOW, &g_termios_original);
cursor_pos(0, 0);
cursor_visible(1);
}
void set_terminal_mode(void) {
struct termios termios_new;
tcgetattr(0, &g_termios_original);
memcpy(&termios_new, &g_termios_original, sizeof termios_new);
cfmakeraw(&termios_new);
tcsetattr(0, TCSANOW, &termios_new);
cursor_visible(0);
}
int key_pressed(void) {
fd_set fds;
struct timeval tv = { 0L, 0L };
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv) > 0;
}
int32_t getch(void) {
char buf[4] = {0};
return (read(0, buf, 4) != -1) ? *(int32_t*) buf : -1;
}
/* entry_execute is not the correct name for this, since it does not
actually do anything to the entry. */
void execute_entry(struct Entry* entry) {
/* Restore so the application we run looks as it should */
restore_terminal_mode();
cursor_visible(1);
clear_screen();
system(get_key(entry, "exec"));
cursor_visible(0);
set_terminal_mode();
if (*get_key(entry, "wait"))
getch();
if (*get_key(entry, "exit"))
clean_exit();
clear_screen();
}
void clean_exit(void) {
while (g_head->next && (g_head = g_head->next))
entry_destroy(g_head->prev);
entry_destroy(g_head);
restore_terminal_mode();
clear_screen();
exit(EXIT_SUCCESS);
}
void print_usage(void) {
printf
(
"Usage:\n"
" climenu [ENTRIES PATH] [UPDATE INTERVAL]\n\n"
" If no entries file is provided, climenu\n"
" will attempt to read entries from stdin.\n\n"
"Keys:\n"
" Navigate: JK, UP/DOWN arrows.\n"
" Select: Space, Enter\n"
" Exit: CTRL+C, Q\n\n"
"Entry types:\n"
" [Header]: Defines a menu header with a text description.\n"
" [Entry]: Defines a button with a label and a shell command to execute.\n\n"
"Entry properties:\n"
" [Header] and [Entry]:\n"
" <str>: Specifies the text to display for the header or button.\n"
" fgcolor: foreground color. [black,blue,cyan,green,magenta,red,white,yellow]\n"
" bgcolor: background color. [black,blue,cyan,green,magenta,red,white,yellow]\n"
" runstr: Execute the entry string and display the result [true]\n\n"
" [Entry]:\n"
" <exec>: Specifies the shell command to execute when the button is pressed.\n"
" exit: Exit after executing an entry command. [true]\n"
" wait: Waits for a key press after executing the command. [true]\n"
" colormode: When the entry should be displayed in color. [selected]\n"
);
}
int main(int argc, char** argv) {
int refresh = 0;
int time_passed = 0;
int32_t ch;
if (argc == 1) {
g_head = parse_entries(read_stdin());
}
if (argc == 2) {
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
print_usage();
return 0;
}
if ((refresh = atoi(argv[1])) != 0)
g_head = parse_entries(read_stdin());
else
g_head = parse_entries(read_file(argv[1]));
}
if (argc == 3) {
g_head = parse_entries(read_file(argv[1]));
refresh = atoi(argv[2]);
}
if (g_head == NULL) {
print_usage();
return 1;
}
select_entry(g_head);
set_terminal_mode();
clear_screen();
while (1) {
if (refresh) {
draw(g_head);
while (time_passed < refresh * 1000) {
sleep_ms(10);
time_passed += 10;
if (key_pressed()) {
draw(g_head);
ch = getch();
goto handle_input;
} else {
continue;
}
}
time_passed = 0;
continue;
} else {
draw(g_head);
ch = getch();
}
handle_input:
if (ch == KEY_K || ch == KEY_UP) select_prev();
if (ch == KEY_J || ch == KEY_DOWN) select_next();
if (ch == KEY_SPACE || ch == KEY_ENTER) execute_entry(g_selected);
if (ch == KEY_Q || ch == KEY_CTRLC) clean_exit();
}
return EXIT_SUCCESS;
}