Second Commit

This commit is contained in:
ahaas25 2022-10-26 19:07:34 -04:00
parent a62192e5f4
commit 64fff3feb0

View File

@ -2,29 +2,112 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <ncurses.h>
#include "typing_test.h"
struct winsize w; /* Prints Centered Text To Console Screen */
int print_centered_text(WINDOW *win, int row, char str[]) {
int text_spacing = strlen(str);
int center_col = (win->_maxx - text_spacing) / 2;
int draw_ui() { mvwprintw(win, row, center_col, str);
system("clear");
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
} }
int print_center_text(char str[], int width) { /* Prints centered text for menu items */
int i, x = strlen(str); int print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_STRING],
int highlight, int elements) {
int text_length = 0, center_col, i, k, cursor;
width - x; for (i = 0; i < elements; i++) {
width /= 2; /* Padding Area */ for (k = 0; k < strlen(str[i]); k++) {
text_length++;
}
if (i < elements - 1) {
text_length += 2; /* Account for ", " */
}
}
for (i = 0; i < width; i++) { cursor = (win->_maxx - text_length) / 2;
move(row, cursor);
for (i = 0; i < elements; i++) {
if (i == target && highlight) {
attron(A_STANDOUT);
}
printw(str[i]);
attroff(A_STANDOUT);
if (i < elements - 1) {
printw(", ");
}
} }
} }
int main (int argc, char **argv) { /* Main function. Creates main menu */
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); int main() {
draw_ui(); int cursor_x = 0, cursor_y = 0, run = 1;
int ch, key;
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
while (run) {
print_centered_text(stdscr, 0, "Typing Test");
print_centered_text(stdscr, 1, "By Aidan Haas");
print_centered_text(stdscr, 3, "Modes");
print_centered_text(stdscr, 5, "Timed");
print_centered_text_menu(stdscr, 6, cursor_x, TIMED_MODES_STRING, cursor_y == 0, NUM_MODES);
print_centered_text(stdscr, 8, "Words");
print_centered_text_menu(stdscr, 9, cursor_x, WORD_MODES_STRING, cursor_y == 1, NUM_MODES);
print_centered_text_menu(stdscr, 11, cursor_x, MISC_STRING, cursor_y == 2, NUM_MISC);
printw("\n");
ch = getch();
switch (ch) {
case KEY_UP:
if (cursor_y > 0) {
cursor_y--;
} else {
cursor_y = MAX_Y - 1;
}
break;
case KEY_DOWN:
if (cursor_y < MAX_Y - 1) {
cursor_y++;
} else {
cursor_y = 0;
}
break;
case KEY_RIGHT:
if (cursor_x < MAX_X - 1) {
cursor_x++;
} else {
cursor_x = 0;
}
break;
case KEY_LEFT:
if (cursor_x > 0) {
cursor_x--;
} else {
cursor_x = MAX_X - 1;
}
break;
default:
break;
}
printw("%d %d", cursor_x, cursor_y);
if (ch == '\n') {
if (cursor_x == 0 && cursor_y == 2) {
run = 0;
}
}
}
refresh();
endwin();
return 0; return 0;
} }