-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.c
More file actions
851 lines (723 loc) · 25.6 KB
/
Copy pathdraw.c
File metadata and controls
851 lines (723 loc) · 25.6 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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#pragma once
#include "color.c"
#include "complete.c"
#include "preview.c"
#include "status.c"
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
static int g_msg_clear_top = -1;
static int g_prev_msg_line_count = 0;
static void clear_line(const int y) {
for (int i = 0; i < tb_width(); i++) {
tb_set_cell(i, y, ' ', 0, 0);
}
}
static void clear_range(const int offset_x, const int width, const int start_y, const int end_y) {
for (int y = start_y; y < end_y; y++) {
for (int x = offset_x; x < offset_x + width; x++) {
tb_set_cell(x, y, ' ', 0, 0);
}
}
}
static int compare_dirs_first(const struct dirent **a, const struct dirent **b) {
if ((*a)->d_type == DT_DIR && (*b)->d_type != DT_DIR) {
return -1;
}
if ((*b)->d_type == DT_DIR && (*a)->d_type != DT_DIR) {
return 1;
}
return strcasecmp((*a)->d_name, (*b)->d_name);
}
static int filter_dir(const struct dirent *d) {
return (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) && (g_show_hidden || d->d_name[0] != '.');
}
static void reload_dir_middle(void) {
if (!g_update.dir_middle) {
return;
}
if (g_namelist_middle != NULL) {
for (int i = 0; i < g_items_in_middle_dir; i++) {
free(g_namelist_middle[i]);
}
free(g_namelist_middle);
}
const int n = scandir(g_cwd, &g_namelist_middle, filter_dir, compare_dirs_first);
if (n == -1) {
g_namelist_middle = NULL;
snprintf(g_msg, sizeof g_msg, "(middle scandir) %s", strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
g_items_in_middle_dir = 0;
return;
} else if (n == 0) {
g_cursor.name[0] = '\0';
}
g_items_in_middle_dir = n;
g_update.dir_middle = false;
if (OPT_POLL_DIR_CHANGES_MTIME) {
struct stat _st;
if (stat(g_cwd, &_st) == 0)
g_last_mtim_middle = _st.st_mtim;
}
// When items are added/removed with commands the cursor may land on the wrong item
if (g_cursor.idx >= n) {
g_cursor.idx = MAX(0, g_items_in_middle_dir - 1);
} else if (g_cursor.name[0] != '\0' && strcmp(g_namelist_middle[g_cursor.idx]->d_name, g_cursor.name) != 0) {
for (int i = 0; i < n; i++) {
if (strcmp(g_namelist_middle[i]->d_name, g_cursor.name) == 0) {
g_cursor.idx = i;
break;
}
}
}
}
void set_cursor_idx_to_name(const char *name) {
reload_dir_middle();
for (int i = 0; i < g_items_in_middle_dir; i++) {
if (strcmp(g_namelist_middle[i]->d_name, name) == 0) {
g_cursor.idx = i;
return;
}
}
}
static void cursor_free_snapshot(void) {
for (int i = 0; i < g_items_in_snapshot; i++) {
free(g_namelist_snapshot[i]);
}
free(g_namelist_snapshot);
g_namelist_snapshot = NULL;
g_items_in_snapshot = 0;
}
void cursor_snapshot_dir(void) {
if (!OPT_CURSOR_TO_NEW_FILE)
return;
cursor_free_snapshot();
if (g_namelist_middle == NULL)
return;
g_namelist_snapshot = calloc((size_t)g_items_in_middle_dir, sizeof(*g_namelist_snapshot));
if (g_namelist_snapshot == NULL)
return;
for (int i = 0; i < g_items_in_middle_dir; i++) {
g_namelist_snapshot[i] = strdup(g_namelist_middle[i]->d_name);
if (g_namelist_snapshot[i] == NULL) {
for (int j = 0; j < i; j++) {
free(g_namelist_snapshot[j]);
}
free(g_namelist_snapshot);
g_namelist_snapshot = NULL;
return;
}
}
g_items_in_snapshot = g_items_in_middle_dir;
}
void cursor_jump_to_new(void) {
if (!OPT_CURSOR_TO_NEW_FILE || g_namelist_snapshot == NULL)
return;
reload_dir_middle();
for (int i = 0; i < g_items_in_middle_dir; i++) {
bool found = false;
for (int j = 0; j < g_items_in_snapshot; j++) {
if (strcmp(g_namelist_middle[i]->d_name, g_namelist_snapshot[j]) == 0) {
found = true;
break;
}
}
if (!found) {
g_cursor.idx = i;
break;
}
}
cursor_free_snapshot();
}
// Since strcasestr is nonstandard, we always define our own
char *strcasestr(const char *haystack, const char *needle) {
if (!*needle)
return (char *)haystack;
for (; *haystack; haystack++) {
const char *h = haystack;
const char *n = needle;
while (*n && *h && tolower((unsigned char)*h) == tolower((unsigned char)*n)) {
h++;
n++;
}
if (*n == '\0')
return (char *)haystack;
}
return NULL;
}
static bool search_name_matches(const char *file_name) {
if (OPT_IGNORE_CASE)
return strcasestr(file_name, g_current_command.chars) != NULL;
return strstr(file_name, g_current_command.chars) != NULL;
}
void set_cursor_idx_to_search(const bool forward, const int start_idx) {
assert(start_idx >= -1 && start_idx <= g_items_in_middle_dir);
if (forward) {
for (int i = start_idx; i < g_items_in_middle_dir; i++) {
if (search_name_matches(g_namelist_middle[i]->d_name)) {
g_cursor.idx = i;
return;
}
}
if (OPT_WRAP_SCAN) {
for (int i = 0; i < start_idx; i++) {
if (search_name_matches(g_namelist_middle[i]->d_name)) {
g_cursor.idx = i;
return;
}
}
}
} else {
for (int i = start_idx; i >= 0; i--) {
if (search_name_matches(g_namelist_middle[i]->d_name)) {
g_cursor.idx = i;
return;
}
}
if (OPT_WRAP_SCAN) {
for (int i = g_items_in_middle_dir - 1; i > start_idx; i--) {
if (search_name_matches(g_namelist_middle[i]->d_name)) {
g_cursor.idx = i;
return;
}
}
}
}
}
bool set_cursor_idx_to_locate(const bool forward, const int start_idx, const char ch) {
assert(start_idx >= -1 && start_idx <= g_items_in_middle_dir);
if (forward) {
for (int i = start_idx; i < g_items_in_middle_dir; i++) {
if (g_namelist_middle[i]->d_name[0] == ch) {
g_cursor.idx = i;
return true;
}
}
for (int i = 0; i < start_idx; i++) {
if (g_namelist_middle[i]->d_name[0] == ch) {
g_cursor.idx = i;
return true;
}
}
} else {
for (int i = start_idx; i >= 0; i--) {
if (g_namelist_middle[i]->d_name[0] == ch) {
g_cursor.idx = i;
return true;
}
}
for (int i = g_items_in_middle_dir - 1; i > start_idx; i--) {
if (g_namelist_middle[i]->d_name[0] == ch) {
g_cursor.idx = i;
return true;
}
}
}
return false;
}
static int int_digits(const int n) {
if (n < 10)
return 1;
if (n < 100)
return 2;
if (n < 1000)
return 3;
assert(n < 100000);
return 4;
}
static bool is_yanked_entry(const char *base_dir, const char *entry_name) {
char entry_path[PATH_MAX];
const int needed = snprintf(entry_path, sizeof entry_path, "%s/%s", base_dir, entry_name);
if (needed < 0 || needed >= (int)sizeof entry_path)
return false;
for (int i = 0; i < g_yanked_count; i++) {
if (strcmp(entry_path, g_yanked_paths[i]) == 0)
return true;
}
return false;
}
static uintattr_t yank_bg(void) {
return g_yanked_is_move ? COLOR_YANK_MOVE : COLOR_YANK_COPY;
}
static int selected_index(const char *path) {
for (int i = 0; i < g_selected_count; i++) {
if (strcmp(g_selected_paths[i], path) == 0)
return i;
}
return -1;
}
static bool is_selected_in_dir(const char *dir, const char *name) {
if (!OPT_MULTISELECT)
return false;
char path[PATH_MAX];
const int needed = snprintf(path, sizeof path, "%s/%s", dir, name);
return needed >= 0 && needed < (int)sizeof path && selected_index(path) >= 0;
}
static const char *exec_check_path(char *buf, const size_t bufsz, const char *dir, const char *name) {
if (!OPT_COLOR_EXEC)
return NULL;
const int n = snprintf(buf, bufsz, "%s/%s", dir, name);
if (n < 0 || n >= (int)bufsz)
return NULL;
return buf;
}
static void draw_left_column(const int offset_x, const int width) {
if (strcmp(g_cwd, "/") == 0) {
clear_range(offset_x, width + 1, 1, tb_height() - 1);
return;
}
if (g_update.dir_left) {
if (g_namelist_left != NULL) {
for (int i = 0; i < g_items_in_left_dir; i++) {
free(g_namelist_left[i]);
}
free(g_namelist_left);
}
const int n = scandir("../", &g_namelist_left, filter_dir, compare_dirs_first);
if (n == -1) {
g_namelist_left = NULL;
snprintf(g_msg, sizeof g_msg, "(%s scandir) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
g_items_in_left_dir = 0;
clear_range(offset_x, width + 1, 1, tb_height() - 1);
return;
}
const char *current_dir = strrchr(g_cwd, '/') + 1;
for (int i = 0; i < n; i++) {
if (strcmp(g_namelist_left[i]->d_name, current_dir) == 0) {
g_left_column_idx = i;
break;
}
}
g_items_in_left_dir = n;
if (g_left_column_idx >= g_items_in_left_dir)
g_left_column_idx = MAX(0, g_items_in_left_dir - 1);
g_update.dir_left = false;
if (OPT_POLL_DIR_CHANGES_MTIME) {
struct stat _st;
if (stat("../", &_st) == 0)
g_last_mtim_left = _st.st_mtim;
}
}
if (g_items_in_left_dir == 0) {
clear_range(offset_x, width + 1, 1, tb_height() - 1);
return;
}
int start_idx = 0;
if (g_left_column_idx >= tb_height() - 2) {
start_idx = g_left_column_idx - (tb_height() - 3);
assert(start_idx >= 0);
}
const int stop_idx = MIN(start_idx + tb_height() - 2, g_items_in_left_dir);
char left_parent[PATH_MAX];
strlcpy(left_parent, g_cwd, sizeof left_parent);
char *slash = strrchr(left_parent, '/');
if (slash == left_parent) {
left_parent[1] = '\0';
} else {
*slash = '\0';
}
const bool has_yanked = g_yanked_count > 0;
int y = 1;
for (int i = start_idx; i < stop_idx; i++) {
char lpath[PATH_MAX];
const char *lpath_ptr = exec_check_path(lpath, sizeof lpath, "..", g_namelist_left[i]->d_name);
const uintattr_t fg = color_file(g_namelist_left[i]->d_type, lpath_ptr);
uintattr_t bg = (i == g_left_column_idx) ? COLOR_REVERSE : COLOR_DEFAULT;
const bool selected = is_selected_in_dir(left_parent, g_namelist_left[i]->d_name);
const bool yanked = !selected && has_yanked && is_yanked_entry(left_parent, g_namelist_left[i]->d_name);
tb_set_cell(offset_x, y, ' ', fg, selected ? COLOR_SELECT : yanked ? yank_bg() : COLOR_DEFAULT);
tb_printf(offset_x + 1, y, fg, bg, " %-*.*s", width - 1, width - 1, g_namelist_left[i]->d_name);
if (i == g_left_column_idx) {
tb_set_cell(offset_x + width, y, ' ', fg, bg);
} else {
tb_set_cell(offset_x + width, y, ' ', 0, 0);
}
y++;
}
clear_range(offset_x, width + 1, y, tb_height() - 1);
}
static int g_middle_start_idx = 0;
static int mouse_left_column_idx(const int y) {
if (y < 1 || y >= tb_height() - 1 || g_items_in_left_dir == 0)
return -1;
const int start = (g_left_column_idx >= tb_height() - 2) ? g_left_column_idx - (tb_height() - 3) : 0;
const int idx = start + (y - 1);
return idx < g_items_in_left_dir ? idx : -1;
}
static int mouse_middle_column_idx(const int y) {
if (y < 1 || y >= tb_height() - 1 || g_items_in_middle_dir == 0)
return -1;
const int idx = g_middle_start_idx + (y - 1);
return idx < g_items_in_middle_dir ? idx : -1;
}
static int mouse_right_column_idx(const int y) {
if (y < 1 || y >= tb_height() - 1 || g_items_in_right_dir == 0)
return -1;
const int start = (g_right_column_idx >= tb_height() - 2) ? g_right_column_idx - (tb_height() - 3) : 0;
const int idx = start + (y - 1);
return idx < g_items_in_right_dir ? idx : -1;
}
static void draw_middle_column(const int offset_x, const int width) {
reload_dir_middle();
int y = 1;
if (g_items_in_middle_dir == 0) {
clear_range(offset_x, width + 1, 1, tb_height() - 1);
tb_print(offset_x + 1, y, COLOR_REVERSE, COLOR_DEFAULT, "empty");
return;
}
const int view_height = MAX(tb_height() - 2, 1);
const int max_start = MAX(0, g_items_in_middle_dir - view_height);
if (g_middle_start_idx > max_start) {
g_middle_start_idx = max_start;
}
if (g_cursor.idx < g_middle_start_idx) {
g_middle_start_idx = g_cursor.idx;
} else if (g_cursor.idx >= g_middle_start_idx + view_height) {
g_middle_start_idx = g_cursor.idx - (view_height - 1);
if (g_middle_start_idx < 0) {
g_middle_start_idx = 0;
} else if (g_middle_start_idx > max_start) {
g_middle_start_idx = max_start;
}
}
const int stop_idx = MIN(g_middle_start_idx + view_height, g_items_in_middle_dir);
const int number_width = OPT_NUMBER ? int_digits(g_items_in_middle_dir) : 0;
for (int i = g_middle_start_idx; i < stop_idx; i++) {
if (i == g_cursor.idx) {
strlcpy(g_cursor.name, g_namelist_middle[i]->d_name, sizeof(g_cursor.name));
}
if (OPT_NUMBER) {
const int num = (g_cursor.idx == i || !OPT_RELATIVE_NUMBER) ? i + 1 : abs(g_cursor.idx - i);
const char *fmt = (g_cursor.idx == i && num < 10) ? "%-*d" : "%*d";
tb_printf(offset_x, y, COLOR_LINENUMBER, COLOR_DEFAULT, fmt, number_width, num);
}
char mpath[PATH_MAX];
const char *mpath_ptr = exec_check_path(mpath, sizeof mpath, g_cwd, g_namelist_middle[i]->d_name);
const uintattr_t fg = color_file(g_namelist_middle[i]->d_type, mpath_ptr);
uintattr_t bg = (i == g_cursor.idx) ? COLOR_REVERSE : COLOR_DEFAULT;
const bool selected = is_selected_in_dir(g_cwd, g_namelist_middle[i]->d_name);
const bool yanked = !selected && is_yanked_entry(g_cwd, g_namelist_middle[i]->d_name);
tb_set_cell(offset_x + number_width, y, ' ', fg, selected ? COLOR_SELECT : yanked ? yank_bg() : COLOR_DEFAULT);
tb_printf(offset_x + number_width + 1, y, fg, bg, " %-*.*s", width - 2 - number_width, width - 2 - number_width,
g_namelist_middle[i]->d_name);
if (i == g_cursor.idx) {
tb_set_cell(offset_x + width, y, ' ', fg, bg);
} else {
tb_set_cell(offset_x + width, y, ' ', 0, 0);
}
y++;
}
clear_range(offset_x, width + 1, y, tb_height() - 1);
}
// Rescan the right column's directory. Returns 0 on success, or the scandir errno on failure.
static unsigned int reload_dir_right(void) {
static char prev_path[PATH_MAX * 2] = "";
char new_path[PATH_MAX * 2];
const int needed = snprintf(new_path, sizeof new_path, "%s/%s", g_cwd, g_cursor.name);
const bool path_ok = needed >= 0 && needed < (int)sizeof new_path;
const bool same_dir = path_ok && strcmp(new_path, prev_path) == 0;
char prev_name[sizeof g_cursor.name] = "";
if (same_dir && g_namelist_right != NULL && g_right_column_idx < g_items_in_right_dir)
strlcpy(prev_name, g_namelist_right[g_right_column_idx]->d_name, sizeof prev_name);
if (g_namelist_right != NULL) {
for (int i = 0; i < g_items_in_right_dir; i++)
free(g_namelist_right[i]);
free(g_namelist_right);
}
const int n = scandir(g_cursor.name, &g_namelist_right, filter_dir, compare_dirs_first);
g_update.dir_right = false;
if (n == -1) {
g_namelist_right = NULL;
g_items_in_right_dir = 0;
prev_path[0] = '\0';
return errno;
}
g_items_in_right_dir = n;
if (path_ok)
strlcpy(prev_path, new_path, sizeof prev_path);
else
prev_path[0] = '\0';
if (OPT_POLL_DIR_CHANGES_MTIME) {
struct stat _st;
if (path_ok && stat(new_path, &_st) == 0)
g_last_mtim_right = _st.st_mtim;
}
// When items are added/removed with commands the cursor may land on the wrong item
if (g_right_column_idx >= n) {
g_right_column_idx = MAX(0, g_items_in_right_dir - 1);
} else if (same_dir && prev_name[0] != '\0' && strcmp(g_namelist_right[g_right_column_idx]->d_name, prev_name) != 0) {
for (int i = 0; i < n; i++) {
if (strcmp(g_namelist_right[i]->d_name, prev_name) == 0) {
g_right_column_idx = i;
break;
}
}
}
return 0;
}
static void draw_right_column(const int offset_x, const int width) {
int y = 1;
const int prev_msg_line_count = g_prev_msg_line_count;
const bool msg_changed = (g_prev_msg_line_count != g_msg_line_count);
g_prev_msg_line_count = g_msg_line_count;
const int full_right_width = tb_width() - offset_x;
if (OPT_IMAGE_SIXEL && g_update.dir_right)
sixel_clear_active(offset_x);
if (g_update.dir_right) {
const int status = reload_dir_right();
switch (status) {
case 0:
clear_range(offset_x, full_right_width, 1, tb_height() - 1);
break;
case ENOTDIR:
if (g_current_mode != MODE_COMMAND || g_search_idx_before >= 0) {
if (!g_preview_needs_redraw && preview_is_current(g_cursor.name))
return;
g_preview_needs_redraw = false;
clear_range(offset_x, full_right_width, 1, tb_height() - (g_msg_line_count ? g_msg_line_count : 1));
preview_start(offset_x, g_cursor.name);
}
return;
case ENOENT:
clear_range(offset_x, full_right_width, 1, tb_height() - 1);
return;
default:
clear_range(offset_x, full_right_width, 1, tb_height() - 1);
tb_print(offset_x + 2, y, COLOR_REVERSE, COLOR_DEFAULT, strerror(status));
return;
}
}
if (g_items_in_right_dir == 0) {
struct stat st;
if (lstat(g_cursor.name, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
tb_print(offset_x + 3, y, COLOR_REVERSE, COLOR_DEFAULT, "empty");
} else if (g_current_mode != MODE_COMMAND && msg_changed) {
if (prev_msg_line_count > 0)
clear_range(offset_x, full_right_width, MAX(1, tb_height() - prev_msg_line_count), tb_height());
preview_start(offset_x, g_cursor.name);
}
}
return;
}
preview_invalidate();
int start_idx = 0;
if (g_right_column_idx >= tb_height() - 2) {
start_idx = g_right_column_idx - (tb_height() - 3);
assert(start_idx >= 0);
}
const int stop_idx = MIN(start_idx + tb_height() - 2, g_items_in_right_dir);
char right_base[PATH_MAX];
const int needed = snprintf(right_base, sizeof right_base, "%s/%s", g_cwd, g_cursor.name);
const bool has_yanked = g_yanked_count > 0 && needed >= 0 && needed < (int)sizeof right_base;
for (int i = start_idx; i < stop_idx; i++) {
char rpath[PATH_MAX];
const char *rpath_ptr = exec_check_path(rpath, sizeof rpath, right_base, g_namelist_right[i]->d_name);
const uintattr_t fg = color_file(g_namelist_right[i]->d_type, rpath_ptr);
uintattr_t sel_fg = fg;
if (i == g_right_column_idx) {
sel_fg |= COLOR_UNDERLINE;
}
const bool selected = is_selected_in_dir(right_base, g_namelist_right[i]->d_name);
const bool yanked = !selected && has_yanked && is_yanked_entry(right_base, g_namelist_right[i]->d_name);
tb_set_cell(offset_x, y, ' ', fg, selected ? COLOR_SELECT : yanked ? yank_bg() : COLOR_DEFAULT);
tb_set_cell(offset_x + 1, y, ' ', sel_fg, COLOR_DEFAULT);
tb_printf(offset_x + 2, y, sel_fg, COLOR_DEFAULT, "%-*.*s", width - 1, width - 1, g_namelist_right[i]->d_name);
y++;
}
clear_range(offset_x, full_right_width, y, tb_height() - 1);
}
static void draw_path(void) {
clear_line(0);
tb_printf(0, 0, COLOR_PATH_USER_HOST, COLOR_DEFAULT, "%s@%s", g_username, g_hostname);
const int offset = g_username_len + g_hostname_len + 2;
tb_print(offset, 0, COLOR_DEFAULT, COLOR_DEFAULT, ":");
if (strcmp(g_cwd, "/") == 0) {
tb_print(offset, 0, COLOR_PATH_DIR, COLOR_DEFAULT, "/");
tb_print(offset + 1, 0, COLOR_PATH_CURSOR, COLOR_DEFAULT, g_cursor.name);
} else {
if (strncmp(g_homepath, g_cwd, g_homepath_len) == 0) {
tb_printf(offset, 0, COLOR_PATH_DIR, COLOR_DEFAULT, "~%s/", g_cwd + g_homepath_len);
tb_print(offset + strlen(g_cwd) - g_homepath_len + 2, 0, COLOR_PATH_CURSOR, COLOR_DEFAULT, g_cursor.name);
} else {
tb_printf(offset, 0, COLOR_PATH_DIR, COLOR_DEFAULT, "%s/", g_cwd);
tb_print(offset + strlen(g_cwd) + 1, 0, COLOR_PATH_CURSOR, COLOR_DEFAULT, g_cursor.name);
}
}
}
static int count_msg_lines(void) {
if (g_msg[0] == '\0')
return 0;
int msg_len = strlen(g_msg);
while (msg_len > 0 && g_msg[msg_len - 1] == '\n')
msg_len--;
if (msg_len == 0)
return 0;
int count = 1;
for (int i = 0; i < msg_len; i++) {
if (g_msg[i] == '\n')
count++;
}
return count;
}
// Renders g_msg as multiline text at the bottom of the screen,
// with the last line at bottom_y. line_count must be the result of count_msg_lines().
// Returns number of lines rendered (0 if empty).
static int render_multiline_msg(const int bottom_y, const int line_count) {
if (line_count == 0)
return 0;
const int display_lines = MIN(line_count, bottom_y);
for (int i = 0; i < display_lines; i++) {
clear_line(bottom_y - display_lines + 1 + i);
}
const char *start = g_msg;
ansi_state_t state;
for (int i = 0; i < display_lines; i++) {
const char *end = strchr(start, '\n');
const int len = end ? (int)(end - start) : (int)strlen(start);
int y = bottom_y - display_lines + 1 + i;
int x = 0;
// Reset state at start of message; persist across lines within it.
if (i == 0) {
ansi_state_reset(&state);
if (g_msg_type == MSG_TYPE_SUCCESS)
state.fg = COLOR_SUCCESS_FG;
else if (g_msg_type == MSG_TYPE_ERROR)
state.bg = COLOR_ERROR_BG;
}
// MSG_TYPE_ERROR: render "Error: " prefix with error background
if (g_msg_type == MSG_TYPE_ERROR && i == 0) {
tb_printf(0, y, COLOR_DEFAULT, COLOR_ERROR_BG, "Error: ");
x = 7;
}
const unsigned int tab_width = 8; // Using hardcoded 8 tab spaces so output from commands like ls looks correct
x = render_ansi_str(start, len, x, &y, tb_width(), false, tab_width, 0, NULL, &state);
start = end ? end + 1 : start + len;
}
return display_lines;
}
bool draw_streaming_msg(void) {
char *cwd_marker = strstr(g_msg, "--CWD_MARKER--");
char saved = '\0';
if (cwd_marker) {
saved = *cwd_marker;
*cwd_marker = '\0';
}
pthread_mutex_lock(&g_tb_mutex);
const int visible_lines = count_msg_lines();
const int rendered = render_multiline_msg(tb_height() - 1, visible_lines);
g_msg_line_count = MIN(visible_lines, tb_height() - 1);
tb_present();
pthread_mutex_unlock(&g_tb_mutex);
if (cwd_marker)
*cwd_marker = saved;
return rendered < visible_lines;
}
static void draw_message(void) {
const int line_count = count_msg_lines();
const int rendered = render_multiline_msg(tb_height() - 1, line_count);
if (rendered == 0)
return;
g_msg_clear_top = tb_height() - rendered;
}
static void draw_status_normal(const int repeat) {
clear_line(tb_height() - 1);
if (!g_items_in_middle_dir) {
return;
}
char path[sizeof(g_cwd) + 1 /* slash */ + sizeof(g_cursor.name)];
const ssize_t needed = snprintf(path, sizeof(path), "%s/%s", g_cwd, g_cursor.name);
if (needed < 0 || (size_t)needed >= sizeof(path)) {
snprintf(g_msg, sizeof g_msg, "Path too long (needed %zd bytes)", needed + 1);
g_msg_type = MSG_TYPE_ERROR;
return;
}
struct stat st;
if (lstat(path, &st) < 0) {
snprintf(g_msg, sizeof g_msg, "(%s lstat) %s", __func__, strerror(errno));
g_msg_type = MSG_TYPE_ERROR;
return;
}
const char *status_str = status_normal_string(&st);
tb_printf(0, tb_height() - 1, COLOR_STATUS_PERM, COLOR_DEFAULT, "%.10s", status_str);
tb_print(11, tb_height() - 1, COLOR_DEFAULT, COLOR_DEFAULT, status_str + 11);
const int end_offset = tb_width() - int_digits(g_cursor.idx + 1) - int_digits(g_items_in_middle_dir) - 1;
tb_printf(end_offset, tb_height() - 1, COLOR_DEFAULT, COLOR_DEFAULT, "%d/%d", g_cursor.idx + 1,
g_items_in_middle_dir);
if (repeat) {
tb_printf(end_offset - int_digits(repeat) - 1, tb_height() - 1, COLOR_DEFAULT, COLOR_DEFAULT, "%d", repeat);
}
}
static void draw_status_command(void) {
const int y = tb_height() - 1;
clear_line(y);
tb_print(0, y, COLOR_DEFAULT, COLOR_DEFAULT, g_search_idx_before >= 0 ? "/" : ":");
tb_print(1, y, COLOR_DEFAULT, COLOR_DEFAULT, g_current_command.chars);
if (OPT_CMD_COMPLETE && g_search_idx_before < 0 && complete_is_active()) {
char info[64];
const int count = complete_match_count();
const int idx = complete_match_idx() + 1;
if (count >= COMPLETION_MAX_MATCHES) {
snprintf(info, sizeof info, " [%d/99+]", idx);
} else {
snprintf(info, sizeof info, " [%d/%d]", idx, count);
}
tb_print(1 + g_current_command.len, y, COLOR_STATUS_PERM, COLOR_DEFAULT, info);
}
tb_set_cursor(g_current_command.cursor + 1,
y); // offset by one because we prefix with :
}
static void draw_status_prompt(const char *prompt, const char *buf, const int cursor) {
const int y = tb_height() - 1;
clear_line(y);
tb_print(0, y, COLOR_DEFAULT, COLOR_DEFAULT, prompt);
tb_print(strlen(prompt), y, COLOR_DEFAULT, COLOR_DEFAULT, buf);
tb_set_cursor(strlen(prompt) + cursor, y);
tb_present();
}
void draw_status_running_command(void) {
const int y = tb_height() - 1;
pthread_mutex_lock(&g_tb_mutex);
clear_line(y);
tb_print(0, y, COLOR_DEFAULT, COLOR_DEFAULT, ">");
tb_hide_cursor();
tb_present();
pthread_mutex_unlock(&g_tb_mutex);
}
static void draw_status_confirmation(const char *msg1, const char *msg2) {
clear_line(tb_height() - 1);
tb_printf(0, tb_height() - 1, COLOR_DEFAULT, COLOR_DEFAULT, "%s%s? [y/n]", msg1, msg2);
tb_present();
}
void draw_screen(const int repeat) {
pthread_mutex_lock(&g_tb_mutex);
if (g_msg_clear_top >= 0 && g_msg[0] == '\0') {
for (int i = g_msg_clear_top; i < tb_height(); i++)
clear_line(i);
g_msg_clear_top = -1;
}
g_msg_line_count = MIN(count_msg_lines(), tb_height() - 1);
const int column_width = tb_width() / 6;
draw_left_column(column_width * 0, column_width * 1 - 1);
draw_middle_column(column_width * 1 + 2, column_width * 2 - 2);
draw_right_column(column_width * 3 + 1, column_width * 3 - 1);
draw_path();
if (g_msg[0] != '\0') {
draw_message();
} else {
g_msg_line_count = 0;
switch (g_current_mode) {
case MODE_COMMAND:
draw_status_command();
break;
case MODE_NORMAL:
draw_status_normal(repeat);
break;
}
}
tb_present();
pthread_mutex_unlock(&g_tb_mutex);
}