added color, made resizable, put text in subwindow

This commit is contained in:
Noah Swerhun 2021-07-02 19:08:12 -05:00
parent 54139ec204
commit c465e85d67
6 changed files with 67 additions and 26 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.o
cards

BIN
cards

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -16,7 +16,7 @@ CARD* card_parse_csv(char *f, int *ct) {
FILE *fp; FILE *fp;
char *l, *o, *r; char *l, *o, *r;
const char *delims = " \0"; const char *delims = " \0";
CARD *ca; CARD *ca = NULL;
if ((fp = fopen(f, "r")) == NULL) if ((fp = fopen(f, "r")) == NULL)
die("failed to open file (null pointer)"); die("failed to open file (null pointer)");

View file

@ -37,8 +37,8 @@ char *strdup_(char *s) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
CARD *cards; CARD *cards;
WINDOW *main_card; WINDOW *main_card, *outer_text_card, *inner_text_container;
int cards_ct, cc = 0, cf = 0, term_y, term_x; int cards_ct, cc = 0, cf = 0, max_y, max_x;
char k = '\0'; char k = '\0';
if (argc < 2) if (argc < 2)
@ -50,15 +50,47 @@ int main(int argc, char **argv) {
initscr(); initscr();
noecho(); noecho();
curs_set(0); curs_set(0);
getmaxyx(stdscr, term_y, term_x); if (!has_colors()) {
main_card = newwin(term_y * .75, endwin();
term_x * .75, die("terminal with color support required");
(term_y * .5) - ((term_y * .75) * .5), }
(term_x * .5) - ((term_x * .75) * .5)); start_color();
refresh(); init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
// main loop // main loop
do { do {
// (re)init cards
getmaxyx(stdscr, max_y, max_x);
main_card = newwin(max_y * .75,
max_x * .75,
(max_y * .5) - ((max_y * .75) * .5),
(max_x * .5) - ((max_x * .75) * .5));
getmaxyx(main_card, max_y, max_x);
outer_text_card = derwin(main_card,
max_y * .5,
max_x * .9,
(max_y * .5) - ((max_y * .5) * .5),
(max_x * .5) - ((max_x * .9) * .5));
getmaxyx(outer_text_card, max_y, max_x);
inner_text_container = derwin(outer_text_card,
max_y - 2,
max_x - 2,
1,
1);
// (re)draw cards
clear();
/* wattron(outer_text_card, A_BOLD); */
wattron(inner_text_container, COLOR_PAIR(2));
wattron(main_card, A_BOLD);
box(outer_text_card, 0, 0);
box(main_card, 0, 0);
refresh();
// get user input
switch (k) { switch (k) {
case 'j': case 'j':
if (++cc > cards_ct - 1) if (++cc > cards_ct - 1)
@ -77,31 +109,38 @@ int main(int argc, char **argv) {
} }
break; break;
case 'q': case 'q':
delwin(inner_text_container);
delwin(outer_text_card);
delwin(main_card);
endwin(); endwin();
exit(0); exit(0);
break; break;
} }
// clear old card // print
wclear(main_card); if (cf == 0) { // obverse
wrefresh(main_card);
// draw box
box(main_card, 0, 0);
// print title + text
if (cf == 0) {
mvwprintw(main_card, 0, 10, " Card #%d/%d [front] \r", cc + 1, cards_ct); mvwprintw(main_card, 0, 10, " Card #%d/%d [front] \r", cc + 1, cards_ct);
mvwprintw(main_card, (term_y * .75) * .5, (term_x * .75) * .5, "%s\r", cards[cc].obverse); wprintw(inner_text_container, "%s\r", cards[cc].obverse);
} else { } else { // reverse
mvwprintw(main_card, 0, 10, " Card #%d/%d [back] \r", cc + 1, cards_ct); mvwprintw(main_card, 0, 10, " Card #%d/%d [back] \r", cc + 1, cards_ct);
mvwprintw(main_card, (term_y * .75) * .5, (term_x * .75) * .5, "%s\r", cards[cc].reverse); wprintw(inner_text_container, "%s\r", cards[cc].reverse);
} }
wrefresh(main_card);
} while ((k = getch()) != EOF);
/* for (size_t i = 0; i < cards_ct; i++) */ /// colors
/* printf("o: `%s`, r: `%s`\n", cards[i].obverse, cards[i].reverse); */ wbkgd(outer_text_card, COLOR_PAIR(2));
wbkgd(main_card, COLOR_PAIR(1));
// refresh
touchwin(outer_text_card);
wrefresh(inner_text_container);
touchwin(main_card);
wrefresh(outer_text_card);
wrefresh(main_card);
delwin(inner_text_container);
delwin(outer_text_card);
delwin(main_card);
} while ((k = getch()) != EOF);
return 0; return 0;
} }