💾 Archived View for gemini.rmf-dev.com › repo › Vaati › mz › files › db5abe1639bc65220570bf6c058ba40… captured on 2023-01-29 at 17:36:10. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
2 *
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15 #ifdef __linux__
16 #define _GNU_SOURCE
17 #else
18 #define _BSD_SOURCE
19 #endif
20 #include <dirent.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include "termbox.h"
27 #include "client.h"
28 #include "view.h"
29 #include "file.h"
30 #include "util.h"
31 #include "strlcpy.h"
32
33 struct view *view_init() {
34 struct view *view = malloc(sizeof(struct view));
35 file_init(view);
36 return view;
37 }
38
39 void view_open(struct view *view) {
40 char buf[2048];
41 client.error = 0;
42 switch (view->entries[view->selected].type) {
43 case DT_REG:
44
45 if ((size_t)snprintf(V(buf), "xdg-open \"%s\" 2>/dev/null &",
46 view->entries[view->selected].name) >= sizeof(buf)) {
47 sstrcpy(client.info, "path too long");
48 client.error = 1;
49 break;
50 }
51 system(buf);
52 break;
53 case DT_DIR:
54 if (file_cd(view, view->entries[view->selected].name)) {
55 sstrcpy(client.info, strerror(errno));
56 client.error = 1;
57 break;
58 }
59 file_ls(view);
60 break;
61 }
62 }
63
64 void view_draw(struct view *view) {
65
66 size_t i = 0, start = TABS;
67
68 if (view->selected + 1 > view->scroll + HEIGHT)
69 view->scroll = view->selected - HEIGHT;
70 if (view->selected < view->scroll)
71 view->scroll = view->selected;
72
73 while (i + view->scroll < view->length) {
74 int selected;
75 struct entry *e;
76 uintattr_t fg, bg;
77
78 if (i > HEIGHT)
79 break;
80
81 fg = bg = TB_DEFAULT;
82
83 selected = view->selected == i + view->scroll;
84 e = &view->entries[i + view->scroll];
85 if (selected) {
86 fg = TB_WHITE;
87 bg = TB_BLUE;
88 }
89 if (e->type == DT_DIR)
90 fg = TB_CYAN;
91 else if (e->type != DT_REG)
92 fg = TB_MAGENTA;
93
94 tb_print(0, i + start, fg, bg, e->name);
95 if (e->type == DT_DIR) {
96 tb_set_cell(strlen(e->name), i + start, '/', fg, bg);
97 }
98 i++;
99 }
100 }
101
102 void view_select(struct view *view, const char *name) {
103 file_select(view, name);
104 if (view->length < HEIGHT) return;
105 if (view->selected >= view->length - HEIGHT / 2) {
106 view->scroll = view->length - HEIGHT - 1;
107 } else if (view->selected > HEIGHT / 2) {
108 view->scroll = view->selected - HEIGHT / 2;
109 }
110 }
111