-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirview.c
429 lines (322 loc) · 9.6 KB
/
dirview.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <string.h>
#include <strings.h>
#include <memory.h> // Microsoft safe memory movement
#include <tickit.h>
#include <math.h>
// Display names of all files in current directory
// https://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program
#include <dirent.h>
// local headers
#include "dirReader.h"
#define streq(a,b) (!strcmp(a,b))
/**
* @brief Everything in bundle is the stuff that should be freed or unref'd
* at end of program. Dereference or close in reverse order.
*/
static struct bundle
{
//
Tickit *t;
//
TickitWindow *root;
//
TickitWindow *typer;
//
TickitWindow *dirDisplay;
// Buffer for the text we store in the typer. Size determined by 2*cols.
// Declared first in typerOnExpose.
// Size is cols for flexibility on window resize (not implemented currently)
char *typerBuffer;
} bundle = {.t = NULL, .root = NULL, .typer = NULL, .dirDisplay = NULL, .typerBuffer = NULL};
static enum extraKey
{
Normal,
Backspace,
Tab,
PageUp,
PageDown,
Up,
Down,
Left,
Right,
Home,
End,
Insert,
Delete,
Function
};
static struct extraKeyInfo
{
enum extraKey ex;
// Optional for function key number
uint8_t num;
} extraKeyInfo = {.ex = Normal};
static TickitPenRGB8 blue = {.r = 10, .g = 100, .b = 200};
static TickitRect termRect;
static uint16_t numTyped = 0;
static uint16_t typerWidth = 0;
static TickitKeyEventInfo lastKey;
// Function Declarations
static int rootOnExpose (TickitWindow *root, TickitEventFlags flags, void *_info, void *data);
static int rootOnKey (TickitWindow *root, TickitEventFlags flags, void *_info, void *data);
static int typerOnExpose(TickitWindow *typer, TickitEventFlags flags, void *_info, void *data);
static void empty_bundle(void);
/**
* If I want to do fuzzy searching on local files, CLI tool fzf
*
*/
// TODO: Typer window at bottom, for user to type text in
// TODO: Find a way to make the program structure not fucked
// (headers? .o's?) (before it gets too late) (make a graph?)
// TODO:
// Low priority or unimportant right now (focus on something else)
// TODO: Implement window resize considerations (low priority)
/**
* @brief Expose event function called for root on spawn and any subsequent times expose is called.
*
* @param root
* @param flags
* @param _info
* @param data
* @return int
*/
static int rootOnExpose (TickitWindow *root, TickitEventFlags flags, void *_info, void *data)
{
TickitExposeEventInfo *info = _info;
TickitRenderBuffer *rb = info->rb;
TickitRect rect = info->rect;
int cols = rect.cols,
lines = rect.lines;
int midx = cols / 2,
midy = lines / 2;
return 1;
}
/**
* @brief Event called when the root window experiences a key.
* Modifies `lastKey` global variable.
*
* @details
* Bindings:
* CTRL + Z: pause, restore with fg.
* ALT + Q : call exit function, quit. Safer than CTRL + C.
* aka Meta + Q if that's your poison
*
*
* @param root root
* @param flags
* @param _info TickitKeyEventInfo
* @param data custom data
* @return int
*/
static int rootOnKey(TickitWindow *root, TickitEventFlags flags, void *_info, void *data)
{
TickitKeyEventInfo *info = _info;
if(lastKey.str) free((void *)lastKey.str);
lastKey = *info;
lastKey.str = strdup(info->str);
if (info->mod == TICKIT_MOD_CTRL && streq(info->str, "C-z"))
{
TickitTerm *term = tickit_window_get_term(root);
tickit_term_pause(term);
raise(SIGSTOP);
tickit_term_resume(term);
tickit_window_expose(root, NULL);
return 1;
}
// if user types Meta-q or Meta-Q, quit the prorgram
if (info->mod == TICKIT_MOD_ALT && (streq(info->str, "M-q") || streq(info->str, "M-Q")))
{
empty_bundle();
}
// If key isn't in combo with ALT or CTRL, send it to the typer
if (!(info->mod & TICKIT_MOD_ALT) && !(info->mod & TICKIT_MOD_CTRL))
{
// Backspace
if (streq(lastKey.str, "Backspace")) extraKeyInfo.ex = Backspace;
// Enter
// Tab
// PageUp
// PageDown
// Delete
// Home
// Insert
// Up
// Down
// Left
// Right
// Function 1-12
else extraKeyInfo.ex = Normal;
tickit_window_expose(bundle.typer, (TickitRect *) NULL);
}
return 1;
}
/**
* @brief Any time the typer is exposed, this function will run. Typer will be
* auto exposed at launch and any subsequent times from rootOnKey.
*
* Works with bundle.typerBuffer and automatically mallocs it.
*
* @param typer
* @param flags
* @param _info
* @param data
* @return int
*/
static int typerOnExpose(TickitWindow *typer, TickitEventFlags flags, void *_info, void *data)
{
TickitExposeEventInfo *info = _info;
TickitRenderBuffer *rb = info->rb; // does not need to be unrefed
TickitRect rect = info->rect;
if (bundle.typerBuffer == NULL)
{
typerWidth = rect.cols - 2; // -2 because of extra gap from "> "
bundle.typerBuffer = malloc(typerWidth * sizeof(char));
memset(bundle.typerBuffer, 0, sizeof(char) * typerWidth);
// make empty
for (int i = 0; i < typerWidth; ++i)
{
bundle.typerBuffer[i] = '\0';
}
}
// Clear the window anew each time
tickit_renderbuffer_goto(rb, 0, 0);
tickit_renderbuffer_clear(rb);
// should be unrefed
TickitPen *p = tickit_pen_new();
tickit_pen_set_colour_attr_desc(p, TICKIT_PEN_FG, "white");
tickit_pen_set_bool_attr(p, TICKIT_PEN_BLINK, true);
tickit_renderbuffer_setpen(rb, p);
tickit_renderbuffer_text(rb, "> ");
tickit_pen_set_bool_attr(p, TICKIT_PEN_BLINK, false);
tickit_renderbuffer_setpen(rb, p);
// Am I really using goto statements? Yes.
// --- Know thy enemy ---
//
// These could be extracted to functions in a typer header file
// but ew I have so many global variables (that's bad enough itself)
switch(extraKeyInfo.ex)
{
case Backspace:
goto backspace;
default:
goto normal;
};
backspace:
{
// avoids underflow
if (numTyped != 0) numTyped--;
bundle.typerBuffer[numTyped] = '\0';
tickit_renderbuffer_textf(rb, "%s", bundle.typerBuffer);
goto exit;
}
normal:
{
// First time running will auto expose typer and this avoids segfault
if (lastKey.str == NULL) goto exit;
/// @warning Issues abound if the thing you're writing to buffer is more than 1 char
if (numTyped < typerWidth)
{
strncat(bundle.typerBuffer, lastKey.str, 10);
numTyped++;
}
tickit_renderbuffer_textf(rb, "%s", bundle.typerBuffer);
goto exit;
}
exit:
{
tickit_window_expose(bundle.dirDisplay, NULL);
tickit_pen_unref(p);
return 1;
}
}
static int dirDisplayOnExpose(TickitWindow *disDisplay, TickitEventFlags flags, void *_info, void *data)
{
TickitExposeEventInfo *info = _info;
TickitRenderBuffer *rb = info->rb; // does not need to be unrefed
TickitRect rect = info->rect;
tickit_renderbuffer_goto(rb, 0, 0);
tickit_renderbuffer_clear(rb);
dirview_readdir();
char *out = NULL;
dirview_filterdir("dir");
tickit_renderbuffer_text(rb, "Hi I'm writing in the dir display");
// if (bundle.typerBuffer != NULL)
// {
// dirview_filterdir("dir");
// out = dirview_readfilter();
// }
// if (out != NULL)
// {
// tickit_renderbuffer_text(rb, "hello!");
// free(out);
// }
// char *out = dirview_readfilter();
// tickit_renderbuffer_text(rb, out);
// free(out);
return 0;
}
/**
* @brief Dereference or close items in bundle in reverse order
*
*/
static void empty_bundle(void)
{
free(bundle.typerBuffer);
tickit_window_close(bundle.dirDisplay);
tickit_window_close(bundle.typer);
tickit_window_close(bundle.root);
tickit_stop(bundle.t);
tickit_unref(bundle.t);
// should I reset all the values to NULL?
// I'm exiting right away anyway
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
tickit_debug_enabled = true;
printf("Welcome to dirview. It's probably not complete yet.\n");
bundle.t = (Tickit *) tickit_new_stdtty();
if(bundle.t == NULL)
{
fprintf(stderr, "Cannot create Tickit - %s\n", strerror(errno));
return 1;
}
bundle.root = (TickitWindow *) tickit_get_rootwin(bundle.t);
if(bundle.root == NULL)
{
fprintf(stderr, "Cannot create root window - %s\n", strerror(errno));
return 1;
}
termRect = tickit_window_get_geometry(bundle.root);
tickit_window_bind_event(bundle.root, TICKIT_WINDOW_ON_EXPOSE, (TickitBindFlags) 0, &rootOnExpose, NULL);
bundle.typer = (TickitWindow *) tickit_window_new(bundle.root, (TickitRect){.top = termRect.lines-1, .left = 0, .lines = 2, .cols = termRect.cols}, (TickitWindowFlags) 0);
if (bundle.typer == NULL)
{
fprintf(stderr, "Cannot create typer window - %s\n", strerror(errno));
return 1;
}
// not currently working
tickit_window_set_cursor_visible(bundle.typer, true);
tickit_window_set_cursor_shape(bundle.typer, TICKIT_CURSORSHAPE_BLOCK);
bundle.dirDisplay = (TickitWindow *) tickit_window_new(bundle.root, (TickitRect){.top=0, .cols=termRect.cols, .left=0, .lines = termRect.lines-2}, (TickitWindowFlags) 0);
if (bundle.dirDisplay == NULL)
{
fprintf(stderr, "Cannot create dir display window - %s\n", strerror(errno));
return 1;
}
tickit_window_bind_event(bundle.root, (TickitWindowEvent) TICKIT_WINDOW_ON_KEY, (TickitBindFlags) 0, &rootOnKey, NULL);
tickit_window_bind_event(bundle.typer, (TickitWindowEvent) TICKIT_WINDOW_ON_EXPOSE, (TickitBindFlags) 0, &typerOnExpose, NULL);
tickit_window_bind_event(bundle.dirDisplay, (TickitWindowEvent) TICKIT_WINDOW_ON_EXPOSE, (TickitBindFlags) 0, &dirDisplayOnExpose, NULL);
tickit_window_expose(bundle.typer, NULL);
tickit_window_expose(bundle.dirDisplay, NULL);
tickit_run(bundle.t);
// I don't think we could reach here?
// Seems like only proper way to quit right now is Meta-Q
empty_bundle();
return EXIT_SUCCESS;
}