summaryrefslogtreecommitdiff
path: root/src/draw.c
blob: b483b0c858235b53f8594e94ff026df2cd8d62b1 (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
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "draw.h"
#include "entry.h"
#include "group.h"

void draw_data(Group **groups, int g_count, Entry **entries, int e_count, WINDOW *w, int start_x, int start_y);

void tui_init(Group **groups, int g_count, Entry **entries, int e_count) {
	int input; // capture user input
	WINDOW *w_main;
	
	// set an interrupt to end cleanly
	signal(SIGINT, tui_end);

	// init and set options
	initscr();
	keypad(stdscr, TRUE); // enable arrow keys and other function keys
	cbreak();             // set for interactive curses applications
	curs_set(0);             // hide cursor

	// set main window
	w_main = newwin(0, 0, 0, 0);
	box(w_main, 0, 0);
	refresh();

	// draw groups and entries
	draw_data(groups, g_count, entries, e_count, w_main, 1, 1);
	wrefresh(w_main);

	input = getch();
	tui_end();
	free(*groups);
	free(*entries);
}

void tui_end() {
	endwin();
}



void draw_data(Group **groups, int g_count, Entry **entries, int e_count, WINDOW *w, int start_x, int start_y) {
	int i;
	int j;
	char buf[BUF_LEN];

	for(i = 0; i < g_count; ++i) {
		mvwprintw(w, start_y, start_x, "%s", group_get_name(&((*groups)[i])));
		++start_y;
		for(j = 0; j < e_count; ++j) {
			if(entry_get_group_id(&((*entries)[j])) == group_get_id(&((*groups)[i]))) {
				mvwprintw(w, start_y, start_x, "\t");
				if(entry_get_due_date(&((*entries)[j])) != NULL) {
					memset(buf, 0, BUF_LEN);
					strftime(buf, BUF_LEN, "%a, %m/%d", entry_get_due_date(&((*entries)[j])));
					wprintw(w, "%s: ", buf);
				}
				//mvprintw(start_y, start_x, "\t%s", entry_get_title(&((*entries)[i])));
				wprintw(w, "%s", entry_get_title(&((*entries)[i])));
				++start_y;
			}
		}
		++start_y;
	}
}