summaryrefslogtreecommitdiff
path: root/draw.c
blob: b415672f13df272965ad36ec14a0777db88c7ba1 (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
#include <ncurses.h>
#include <stdbool.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(GROUP **group_arr, int count);
void fill_entries(ENTRY **entry_arr, int count);
char *trim_name(char *name, char *path, int max_len);
void update_entries();
void switch_col();
void arrowkey_handler(int c);
void trav_col(int dir); //0 = down, 1 = up

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;
GROUP **g;
ENTRY **e;
int g_count;
int e_count;

//TODO consider figuring out where some refreshes are unecessary

int main(){
	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)
	char input;

	initscr();
	cbreak();
	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");

	//Fill Groups
	cfg_interp(); //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
	fill_groups(g, g_count);

	//start with hover on the first group, draw the entries from the selected group, true_hover is over the groups (rather than the entries)
	mvwchgat(group_win, 1, 1, group_win->_maxx, A_DIM, 2, NULL);
	wrefresh(group_win);
	update_entries();
	move(3, (width/4)+10);

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

		switch(input){
			case '\t':
				//switch true_hover to look at the other column (TODO this code could use some polish)
				switch_col();
				break;

			case 27: //some arrow key was pressed
				//TODO FIXME retarded issue here regarding key intake being dumped to screen (doesn't like arrow keys :()
				//This may require a greater rewrite and a better understanding of keys in ncurses
				getch();
				arrowkey_handler(getch());
				fflush(stdout);
				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;
}

//FIXME issue rendering boxes
void draw_win(WINDOW *new, char *title){
	int title_len = strlen(title);

	attron(A_UNDERLINE);
	move(4, new->_begx+(new->_maxx-title_len)/2);
	printw("%s", title);
	attroff(A_UNDERLINE);
	box(new, 0, 0);
	wrefresh(new);

	return;
}

void fill_groups(GROUP **group_arr, int count){
	int i;
	int max_len = group_win->_maxx; //longest possible string length that can be displayed in the window
	int ycoord = 1;
	char *name;

	for(i = 0; i < count; i++){
		name = get_gname(group_arr[i]);

		//the name is too long, take the group to the trimming function
		if(strlen(name) > max_len) name = trim_name(name, get_gpath(group_arr[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(ENTRY **entry_arr, int count){
	int i;
	int max_len = entry_win->_maxx; //longest possible string length that can be displayed in the window
	int ycoord = 1;
	char *name;

	for(i = 0; i < count; i++){
		name = get_ename(entry_arr[i]);

		//the name is too long, take the group to the trimming function
		if(strlen(name) > max_len) name = trim_name(name, get_epath(entry_arr[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 *tok; //for use in finding relative path name
	char *tok_ahead;
	char *delims = "/\t\n";

	//group name and path are equivalent: special procedure
	if(!(strcmp(name, path))){
		//find relative path name
		tok_ahead = strtok(name, delims);
		while(tok_ahead != NULL){
			tok = tok_ahead;
			tok_ahead = strtok(NULL, delims);
		}
		name = tok;
		if(strlen(name) <= max_len) return name;
	}

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

void update_entries(){
	//reset the entry window 
	wclear(entry_win);
	wrefresh(entry_win);

	e_count = get_ecount(g[g_hover]);
	e = get_entries(get_ghead(g[g_hover]), e_count);
	fill_entries(e, e_count);
	//FIXME weird box rendering issue after the previous function call
	mvwchgat(entry_win, 1, 1, entry_win->_maxx, A_DIM, 1, NULL);

	return;
}

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

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

void arrowkey_handler(int c){

	switch(c){
		case 66: //down arrow
			trav_col(0);
			break;

		case 65: //up arrow (TODO combine the code with that for down arrow)
			trav_col(1);
			break;

		default: //left or right arrow key
			switch_col();
			break;
	}

	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 count = (true_hover ? e_count : g_count);

	//check if the traversal is valid (i.e. not at top/bottom), exit if not
	//FIXME check that third parameter... might not be right (could cause issues)
	if((dir && !(*focus)) || (!dir && (*focus == count-1)) || (!dir && (*focus == height))) return;

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

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

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