-
Notifications
You must be signed in to change notification settings - Fork 8
/
bd_explore.c
391 lines (298 loc) · 10.4 KB
/
bd_explore.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
#include <stdlib.h>
#include <string.h>
#include <bedd.h>
#include <io.h>
typedef struct bd_entry_t bd_entry_t;
typedef struct bd_explore_t bd_explore_t;
struct bd_entry_t {
char name[256];
int is_directory;
time_t mtime;
int selected;
};
struct bd_explore_t {
char path[256];
bd_entry_t *entries;
int count;
int cursor, scroll;
int scroll_mouse_y;
};
// Static functions
static void __bd_explore_update(bd_explore_t *explore);
static void __bd_explore_follow(bd_explore_t *explore);
static int __bd_explore_enter(bd_view_t *view, int new_tab);
static void __bd_explore_update(bd_explore_t *explore) {
if (explore->entries) {
free(explore->entries);
explore->entries = NULL;
}
explore->count = 0;
io_file_t directory = io_dopen(explore->path);
bd_entry_t entry;
while (io_dread(directory, entry.name)) {
if (!strcmp(entry.name, ".")) {
continue;
}
char merge_path[256];
strcpy(merge_path, explore->path);
int length = strlen(merge_path);
if (merge_path[length - 1] != '/') {
merge_path[length++] = '/';
}
strcpy(merge_path + length, entry.name);
io_file_t test = io_dopen(merge_path);
if ((entry.is_directory = io_dvalid(test))) {
io_dclose(test);
}
entry.mtime = io_ftime(merge_path);
entry.selected = 0;
explore->entries = realloc(explore->entries, (explore->count + 1) * sizeof(bd_entry_t));
explore->entries[explore->count++] = entry;
}
for (int i = 0; i < explore->count; i++) {
for (int j = i + 1; j < explore->count; j++) {
if (strcmp(explore->entries[i].name, explore->entries[j].name) > 0) {
bd_entry_t entry = explore->entries[i];
explore->entries[i] = explore->entries[j];
explore->entries[j] = entry;
}
}
}
}
static void __bd_explore_follow(bd_explore_t *explore) {
if (explore->cursor < 0) {
explore->cursor = 0;
}
if (explore->cursor >= explore->count) {
explore->cursor = explore->count - 1;
}
int view_height = bd_height - 2;
if (explore->scroll > explore->cursor) {
explore->scroll = explore->cursor;
} else if (explore->scroll <= explore->cursor - view_height) {
explore->scroll = (explore->cursor - view_height) + 1;
}
}
static int __bd_explore_enter(bd_view_t *view, int new_tab) {
bd_explore_t *explore = view->data;
bd_entry_t entry = explore->entries[explore->cursor];
char new_path[256];
strcpy(new_path, explore->path);
int length = strlen(new_path);
if (!strcmp(entry.name, "..")) {
if (!strcmp(new_path, ".")) {
io_dsolve(explore->path, new_path);
}
char *ptr = strrchr(new_path, '/');
if (!ptr) {
return 0;
}
if (ptr == new_path) {
ptr++;
}
length = ptr - new_path;
new_path[length] = '\0';
} else {
if (new_path[length - 1] != '/') {
new_path[length++] = '/';
}
strcpy(new_path + length, entry.name);
}
if (entry.is_directory && !new_tab) {
strcpy(explore->path, new_path);
__bd_explore_update(explore);
__bd_explore_follow(explore);
explore->cursor = 0;
return 1;
} else {
bd_open(new_path);
bd_view = bd_view_count - 1;
return 0;
}
}
// Public functions
void bd_explore_draw(bd_view_t *view) {
bd_explore_t *explore = view->data;
int view_height = bd_height - 2;
for (int i = 0; i < view_height; i++) {
int index = i + explore->scroll;
io_cursor(0, i + 2);
if (index < explore->count) {
bd_entry_t entry = explore->entries[index];
struct tm *tm_time = localtime(&entry.mtime);
char buffer[100];
strftime(buffer, 100, "%d %b %Y, %H:%M:%S", tm_time);
if (index == explore->cursor) {
io_printf(IO_INVERT " > ");
} else {
io_printf(IO_SHADOW_1 " - ");
}
if (entry.is_directory) {
io_printf(IO_NORMAL " %s(%s) " IO_BOLD_GREEN "%s/", entry.selected ? IO_SHADOW_1 : "", buffer, entry.name);
} else {
io_printf(IO_NORMAL " %s(%s) %s%s", entry.selected ? IO_SHADOW_1 : "", buffer, entry.name[0] == '.' ? IO_BOLD_BLUE : IO_BOLD_WHITE, entry.name);
}
} else {
io_printf(IO_NORMAL " :");
}
io_printf(IO_CLEAR_LINE IO_NORMAL);
io_cursor(bd_width - 1, i + 2);
int scroll_start_y = (explore->scroll * (bd_height - 2)) / (explore->count + (bd_height - 2));
int scroll_end_y = 1 + ((explore->scroll + (bd_height - 2)) * (bd_height - 2)) / (explore->count + (bd_height - 2));
if (i >= scroll_start_y && i < scroll_end_y) {
io_printf(IO_SHADOW_2 " ");
} else {
io_printf(IO_NORMAL "\u2502");
}
io_printf(IO_NORMAL);
}
strcpy(view->title, explore->path);
view->cursor = (bd_cursor_t) {
-1, -1,
};
}
int bd_explore_event(bd_view_t *view, io_event_t event) {
bd_explore_t *explore = view->data;
if (event.type == IO_EVENT_KEY_PRESS) {
if (event.key == IO_CTRL('U')) {
explore->cursor = 0;
__bd_explore_update(explore);
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_CTRL('A')) {
int select_count = 0;
for (int i = 0; i < explore->count; i++) {
if (explore->entries[i].selected) {
select_count++;
}
}
for (int i = 0; i < explore->count; i++) {
if (!strcmp(explore->entries[i].name, "..")) {
continue;
}
explore->entries[i].selected = (select_count < explore->count - 1);
}
__bd_explore_follow(explore);
return 1;
} else if (IO_UNALT(event.key) == IO_CTRL('M')) {
return __bd_explore_enter(view, event.key != IO_UNALT(event.key));
} else if (event.key == IO_ARROW_UP) {
explore->cursor--;
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_ARROW_DOWN) {
explore->cursor++;
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_CTRL(IO_ARROW_UP)) {
explore->cursor = 0;
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_CTRL(IO_ARROW_DOWN)) {
explore->cursor = explore->count - 1;
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_PAGE_UP) {
explore->cursor -= (bd_height - 2) / 2;
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_PAGE_DOWN) {
explore->cursor += (bd_height - 2) / 2;
__bd_explore_follow(explore);
return 1;
} else if (event.key == ' ') {
if (strcmp(explore->entries[explore->cursor].name, "..")) {
explore->entries[explore->cursor].selected = !explore->entries[explore->cursor].selected;
}
__bd_explore_follow(explore);
return 1;
} else if (event.key == IO_CTRL('C') || event.key == IO_CTRL('X')) {
io_file_t clip = io_copen(1);
char buffer[8192];
strcpy(buffer, (event.key == IO_CTRL('C')) ? "Copy" : "Move");
for (int i = 0; i < explore->count; i++) {
if (explore->entries[i].selected) {
char merge_path[256];
strcat(buffer, " ");
strcpy(merge_path, explore->path);
int length = strlen(merge_path);
if (merge_path[length - 1] != '/') {
merge_path[length++] = '/';
}
strcpy(merge_path + length, explore->entries[i].name);
strcat(buffer, merge_path);
}
}
io_fwrite(clip, buffer, strlen(buffer));
io_cclose(clip);
return 1;
} else if (event.key == IO_CTRL('V')) {
io_file_t clip = io_copen(0);
char buffer[8192];
int length = 0;
while (!io_feof(clip)) {
if (io_fread(clip, buffer + length, 1) <= 0) {
break;
}
length++;
}
io_cclose(clip);
// TODO: Copy and cut
return 1;
}
} else if (event.type == IO_EVENT_MOUSE_DOWN || event.type == IO_EVENT_MOUSE_MOVE) {
if (explore->scroll_mouse_y >= 0) {
explore->scroll = ((event.mouse.y - (2 + explore->scroll_mouse_y)) * (explore->count + (bd_height - 2))) / (bd_height - 2);
if (explore->scroll < 0) {
explore->scroll = 0;
} else if (explore->scroll >= explore->count) {
explore->scroll = explore->count - 1;
}
} else if (event.type == IO_EVENT_MOUSE_DOWN && event.mouse.x >= bd_width - 1) {
int scroll_start_y = 2 + (explore->scroll * (bd_height - 2)) / (explore->count + (bd_height - 2));
int scroll_end_y = 3 + ((explore->scroll + (bd_height - 2)) * (bd_height - 2)) / (explore->count + (bd_height - 2));
if (event.mouse.y >= scroll_start_y && event.mouse.y < scroll_end_y) {
explore->scroll_mouse_y = event.mouse.y - scroll_start_y;
} else {
explore->scroll_mouse_y = (scroll_end_y - scroll_start_y) / 2;
explore->scroll = ((event.mouse.y - (2 + explore->scroll_mouse_y)) * (explore->count + (bd_height - 2))) / (bd_height - 2);
if (explore->scroll < 0) {
explore->scroll = 0;
} else if (explore->scroll >= explore->count) {
explore->scroll = explore->count - 1;
}
}
} else if (event.type == IO_EVENT_MOUSE_DOWN) {
if (explore->cursor == explore->scroll + (event.mouse.y - 2)) {
return __bd_explore_enter(view, 0);
}
explore->cursor = explore->scroll + (event.mouse.y - 2);
__bd_explore_follow(explore);
}
return 1;
} else if (event.type == IO_EVENT_MOUSE_UP) {
explore->scroll_mouse_y = -1;
} else if (event.type == IO_EVENT_SCROLL) {
explore->scroll += event.scroll * bd_config.scroll_step;
if (explore->scroll < 0) {
explore->scroll = 0;
} else if (explore->scroll >= explore->count) {
explore->scroll = explore->count - 1;
}
return 1;
}
return 0;
}
void bd_explore_load(bd_view_t *view, const char *path) {
bd_explore_t *explore = malloc(sizeof(bd_explore_t));
view->data = explore;
strcpy(explore->path, path);
explore->entries = NULL;
int length = strlen(explore->path);
if (explore->path[length - 1] == '/') {
explore->path[length - 1] = '\0';
}
explore->cursor = explore->scroll = 0;
explore->scroll_mouse_y = -1;
__bd_explore_update(explore);
}