summaryrefslogtreecommitdiff
path: root/draw.c
blob: 92d5bb41db43a04768cd23856c15008947fec8cf (plain)
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
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "entry.h"
#include "group.h"
#include "read_cfg.h"
#define MAX_LEN 6
#define BUF_LEN 1024

void draw_title();
void draw_win(WINDOW *new, char *title);
void fill_groups();
void fill_entries();
char *trim_name(char *name, char *path, int max_len);
void update_col(int mode, int hl_where); //0 = last, 1 = first; 0 = GROUP, 1 = ENTRY, 2 = INFO
void switch_col();
void trav_col(int dir); //0 = down, 1 = up
void launch_entry();
char *compat_convert(char *path, int mode);

static int width;
static int height;
WINDOW *group_win;
WINDOW *entry_win;
WINDOW *info_win;
int g_hover = 0;
int e_hover = 0;
int true_hover = 0; //which column are we currently hovering on? (I think)
GROUP **g;
ENTRY **e;
int g_count;
int e_count;
int g_offset = 0;
int e_offset = 0;

//TODO consider figuring out where some refreshes are unecessary

int main(int argc, char **argv){
	char *cfg_path = "config";
	bool tall = true; //is the window a certain height (tbd what the threshold should be TODO)
	bool wide = true; //is the window a certain width (tbd what the threshold should be TODO)
	int input;

	//if a config path was given as an argument, set it accordingly
	if(argc > 2 && (!strcmp(argv[1], "-c") || !strcmp(argv[1], "--cfg_path"))) cfg_path = argv[2];

	//Fill Groups
	cfg_interp(cfg_path); //read the contents of the cfg file
	g = get_groups(); //retrieve results of previous function
	g_count = get_gcount(g); //retrieve number of groups in g

	initscr();
	cbreak();
	keypad(stdscr, true);
	start_color();

	width = getmaxx(stdscr);
	height = getmaxy(stdscr);
	init_pair(0, COLOR_WHITE, COLOR_BLACK);
	init_pair(1, COLOR_BLACK, COLOR_WHITE);
	init_pair(2, COLOR_BLACK, COLOR_YELLOW);
	attron(COLOR_PAIR(0));

	//title at the top (Terminal Media Launcher) (23 chars)
	draw_title();

	//Draw Search Bar 2 spaces (3 spaces if window is big enough) under the title
	move(3, width/4);
	printw("[ Search: ");
	move(3, (width*3)/4);
	printw("]");
	move(3, (width/4)+10);

	//Draw Columns 
	//TODO create conditionals based on size of window)
	group_win = newwin(height-4, width/3, 4, 0);
	entry_win = newwin(height-4, width/3, 4, width/3);
	info_win = newwin(height-4, width/3, 4, (2*width)/3);
	refresh();
	draw_win(group_win, "GROUP");
	draw_win(entry_win, "ENTRY");
	draw_win(info_win, "INFO");
	update_col(0, 1);

	//start with hover on the first group, draw the entries from the selected group, true_hover is over the groups (rather than the entries)
	update_col(1, 1);
	update_col(2, 0);
	move(3, (width/4)+10);

	//drawing is done, now run a while loop to receive input
	while(1){
		input = getch();

		switch(input){
			case 9: //tab key
			case KEY_LEFT:
			case KEY_RIGHT:
				//switch true_hover to look at the other column (TODO this code could use some polish)
				switch_col();
				break;

			case KEY_DOWN:
				trav_col(0);
				break;

			case KEY_UP:
				trav_col(1);
				break;

			case 10: //enter key
				launch_entry();
				break;

			default:
				endwin();
				return 0;

		}

		move(3, (width/4)+10); //reset cursor location to the search bar after any action
	}
	
	endwin();
	return 0;
}

void draw_title(){
	WINDOW *title;

	title = newwin(2, width, 0, 0);
	refresh();
	attron(A_BOLD | A_UNDERLINE);
	move(0, (width-23)/2);
	printw("Terminal Media Launcher");
	attroff(A_BOLD | A_UNDERLINE);
	box(title, 0, 0);
	wrefresh(title);

	return;
}

void draw_win(WINDOW *new, char *title){
	int title_len = strlen(title);

	box(new, 0, 0);
	attron(A_UNDERLINE);
	wmove(new, 0, (new->_maxx - title_len)/2);
	wprintw(new, "%s", title);
	attroff(A_UNDERLINE);
	wrefresh(new);

	return;
}

void fill_groups(){
	int i;
	int max_len = group_win->_maxx-1; //longest possible string length that can be displayed in the window
	int ycoord = 1;
	int max_y = height-5; //last possible slot to draw a group
	char *name;

	for(i = 0+g_offset; i < g_count; i++){
		if(ycoord >= max_y) break; //already at bottom of terminal window, stop drawing
		//TODO account for empty groups with a removal function
		name = get_gname(g[i]);

		//the name is too long, take the group to the trimming function
		if(strlen(name) > max_len) name = trim_name(name, get_gname(g[i]), max_len);
		wmove(group_win, ycoord, 1);
		wprintw(group_win, "%s", name);
		ycoord++;
	}

	wrefresh(group_win);
	return;
}

//very similar to the previous function, perhaps they can be combined... (TODO)
void fill_entries(){
	int i;
	int max_len = entry_win->_maxx-1; //longest possible string length that can be displayed in the window
	int ycoord = 1;
	int max_y = height-5;
	char *name;

	for(i = 0+e_offset; i < e_count; i++){
		if(ycoord >= max_y) break; //reached the bottom of the terminal window, stop drawing
		name = get_ename(e[i]);

		//the name is too long, take the group to the trimming function
		if(strlen(name) > max_len) name = trim_name(name, get_epath(e[i]), max_len);
		wmove(entry_win, ycoord, 1);
		wprintw(entry_win, "%s", name);
		ycoord++;
	}

	wrefresh(entry_win);
	return;
}

char *trim_name(char *name, char *path, int max_len){
	char *relative;

	//group name and path are equivalent: special procedure
	if(!(strcmp(name, path))){
		//find relative path name
		relative = strrchr(name, '/');
		name = relative+1;
		if(strlen(name) <= max_len) return name;
	}

	name[max_len] = '\0';
	return name;
}

//FIXME info printing (mode = 2) is not uniformally in the box
void update_col(int mode, int hl_where){
	//mode 0 = group
	//mode 1 = entry
	//mode 2 = info

	WINDOW *col;
	char *name;
	int name_len;
	int y_hl;

	switch(mode){
		case 0:
			col = group_win;
			name = "GROUP";
			name_len = 5;
			break;

		case 1:
			col = entry_win;
			name = "ENTRY";
			name_len = 5;
			break;

		case 2:
			col = info_win;
			name = "INFO";
			name_len = 4;
			break;

		default:
			return;
	}

	y_hl = (hl_where ? 1 : col->_maxy-1);

	//reset the column window (including reboxing and redrawing the title)
	wclear(col);
	box(col, 0, 0);
	wmove(col, 0, (group_win->_maxx - name_len)/2);
	wprintw(col, name);
	wrefresh(col);

	switch(mode){
		case 0:
			fill_groups();
			mvwchgat(group_win, y_hl, 1, group_win->_maxx-1, A_DIM, 2, NULL);
			break;

		case 1:
			e_count = get_ecount(g[g_hover]);
			e = get_entries(get_ghead(g[g_hover]), e_count);
			fill_entries(e, e_count);
			mvwchgat(entry_win, y_hl, 1, entry_win->_maxx-1, A_DIM, 1, NULL);
			break;

		default:
			mvwprintw(info_win, 1, 1, "PROGRAM: %s\n\nPATH: %s", get_gprog(g[g_hover]), get_epath(e[e_hover]));

	}

	wrefresh(col);
	return;
}

void switch_col(){
	true_hover = (true_hover+1) % 2;
	if(true_hover){
		mvwchgat(group_win, 1+g_hover, 1, group_win->_maxx-1, A_DIM, 1, NULL); //adjust group light
		mvwchgat(entry_win, 1+e_hover, 1, entry_win->_maxx-1, A_DIM, 2, NULL); //adjust entry light
	}
	else{
		mvwchgat(group_win, 1+g_hover, 1, group_win->_maxx-1, A_DIM, 2, NULL); //adjust group light
		mvwchgat(entry_win, 1+e_hover, 1, entry_win->_maxx-1, A_DIM, 1, NULL); //adjust entry light
	}
	move(3, (width/4)+10);

	wrefresh(group_win);
	wrefresh(entry_win);
	return;
}

void trav_col(int dir){
	int *focus = (true_hover ? &e_hover : &g_hover); //make it easy to know which column we are looking at
	int *offset = (true_hover ? &e_offset : &g_offset); //this variable is broken
	int count = (true_hover ? e_count : g_count);
	int max_hl = height-2+*offset; //for some reason, this works
	int min_hl = 5+*offset;
	bool oob_flag = false;

	//check if the traversal is valid (i.e. not at top/bottom), exit if not
	if((dir && !(*focus)) || (!dir && (*focus == count-1))) return;

	//reset previously highlighted entry and group, change focus
	mvwchgat(entry_win, 1+e_hover-e_offset, 1, entry_win->_maxx-1, A_NORMAL, 0, NULL);
	mvwchgat(group_win, 1+g_hover-g_offset, 1, group_win->_maxx-1, A_NORMAL, 0, NULL);
	(dir ? (*focus)-- : (*focus)++);

	//check offsets relating to new highlight, make sure highlight did not go oob
	if(*focus+5 > max_hl){
		*focus--;
		(*offset)++;
		oob_flag = true;
	}
	else if(*focus+5 < min_hl){
		*focus++;
		(*offset)--;
		oob_flag = true;
	}

	if(oob_flag) (true_hover ? update_col(1, dir) : update_col(0, dir));

	//highlight newly hovered upon entry/group
	mvwchgat(entry_win, 1+e_hover-e_offset, 1, entry_win->_maxx-1, A_DIM, (true_hover ? 2 : 1), NULL);
	mvwchgat(group_win, 1+g_hover-g_offset, 1, group_win->_maxx-1, A_DIM, (true_hover ? 1 : 2), NULL);
	if(!true_hover){ //a little extra work regarding group hover
		e_offset = 0;
		update_col(1, 1);
		e_hover = 0;
	}

	wrefresh(group_win);
	wrefresh(entry_win);
	update_col(2, 0);
	return;
}

void launch_entry(){
	char *program = get_gprog(g[g_hover]);
	char *flags = get_gflags(g[g_hover]);
	char *path = get_epath(e[e_hover]);
	int mode = get_compmode();
	char full_command[BUF_LEN];
	bool quote_flag_f = (flags[0] == '"' ? false : true);
	bool quote_flag_e = (path[0] == '"' ? false : true);

	//if the entry is an executable file (doesn't have a launcher)
	if(!(strcmp(program, "./"))) system(path);

	else{
		if(mode != 0) path = compat_convert(path, mode);
		full_command[0] = '\0';
		strcat(full_command, program);
		if(flags[0] !='\0'){
			strcat(full_command, " ");
			if(quote_flag_f) strcat(full_command, "\"");
			strcat(full_command, flags);
			if(quote_flag_f) strcat(full_command, "\"");
		}
		strcat(full_command, " ");
		if(quote_flag_e) strcat(full_command, "\"");
		strcat(full_command, path);
		if(quote_flag_e) strcat(full_command, "\"");
		mvprintw(0, 0, "DEBUG: %s", full_command);
		system(full_command);
	}

	return;
}

char *compat_convert(char *path, int mode){
	char *new = malloc(sizeof(char) * BUF_LEN);
	char *trav = new;

	//1 -> WSL: letter is in /mnt/, convert slashes to backslashes
	if(mode == 1){
		path = path+5;
		*trav = *path - 32; //point at letter, make it uppercase
		trav++;
		*trav = ':';
		path++;
		trav++;

		//convert each character
		while(*path != '\0'){
			if(*path == '/') *trav = '\\';
			else *trav = *path;
			path++;
			trav++;
		}
		*trav = '\0';
	}

	else printf("Error: mode should not be %d\n", mode);

	return new;
}