Update makefile, fix generate_words(), work on UI
This commit is contained in:
parent
0ac050a96d
commit
c5d57e9beb
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
||||
# Macros
|
||||
CC = gcc
|
||||
CFLAGS = -ansi -Wall -g -O0 -Wshadow -Wwrite-strings \
|
||||
CFLAGS = -ansi -Wall -g -O0 -Wshadow \
|
||||
-fstack-protector-all
|
||||
LDLIBS = -lncurses
|
||||
|
||||
|
@ -8,17 +8,16 @@
|
||||
#include "utilities.h"
|
||||
|
||||
/* Prints Centered Text To Console Screen */
|
||||
void print_centered_text(WINDOW *win, int row, char str[]) {
|
||||
void print_centered_text(WINDOW *win, int row, char *str) {
|
||||
int text_spacing = strlen(str);
|
||||
int center_col = (win->_maxx - text_spacing) / 2;
|
||||
|
||||
mvwprintw(win, row, center_col, str);
|
||||
}
|
||||
|
||||
/* Prints centered text for menu items */
|
||||
void 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;
|
||||
int text_length = 0, i, k, cursor;
|
||||
|
||||
for (i = 0; i < elements; i++) {
|
||||
for (k = 0; k < strlen(str[i]); k++) {
|
||||
@ -45,28 +44,30 @@ void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_S
|
||||
}
|
||||
}
|
||||
|
||||
void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) {
|
||||
int run = 1, ch, i, words, win_x = win->_maxx, new_test = 1;
|
||||
char str[1024];
|
||||
Word_array *prompt = NULL;
|
||||
/* Prints the typing prompt onto the terminal.
|
||||
Automatically centers, wraps, and scrolls through text */
|
||||
void print_typing_prompt(WINDOW *win, Word_array *prompt, char *user_input) {
|
||||
int i, window_width = win->_maxx;
|
||||
|
||||
printw("\nPrompt: \n");
|
||||
/* Split prompt into Strings that fit terminal width */
|
||||
|
||||
for (i = 0; i < words; i++) {
|
||||
/* Will print the words centered in the screen */
|
||||
/* Will scroll down when user finishes middle line */
|
||||
/* Will automatically wrap text to next line based on
|
||||
how big the console size is */
|
||||
for (i = 0; i < prompt->number_of_words; i++) {
|
||||
mvwprintw(win, 3, 0, "%d: %s ", i, prompt->words[i].text);
|
||||
}
|
||||
|
||||
/* For now prints placeholder text for the purposes of testing */
|
||||
printw("User input %s ", strlen(user_input), user_input);
|
||||
}
|
||||
|
||||
/* Make "Print words function for simplicity" */
|
||||
void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) {
|
||||
int run = 1, ch, i, new_test = 1, user_input_length;
|
||||
char str[1024], *user_input = NULL;
|
||||
Word_array *prompt = NULL;
|
||||
|
||||
while (run) {
|
||||
if (new_test == 1) {
|
||||
clear();
|
||||
|
||||
/* Reset str */
|
||||
str[0] = '\0';
|
||||
|
||||
if (mode == 0) {
|
||||
@ -87,23 +88,44 @@ void typing_ui(WINDOW *win, int level, int mode, Word_array *word_array) {
|
||||
clear_word_array(prompt);
|
||||
}
|
||||
|
||||
/* 10 is a placeholder */
|
||||
generate_words(10, word_array, prompt);
|
||||
|
||||
new_test = 0;
|
||||
for (i = 0; i < prompt->number_of_words; i++) {
|
||||
printw("%d: %s ", i, prompt->words[i].text);
|
||||
if (user_input != NULL) {
|
||||
free(user_input);
|
||||
}
|
||||
|
||||
user_input = malloc(prompt->num_characters + 1);
|
||||
user_input[0] = '\0';
|
||||
user_input_length = 0;
|
||||
|
||||
new_test = 0;
|
||||
}
|
||||
|
||||
ch = getch();
|
||||
if (isalpha(ch)) {
|
||||
printw("%c", ch);
|
||||
}
|
||||
|
||||
if (ch == ' ') {
|
||||
/* Ensures user does not type outside of character limit */
|
||||
if (user_input_length < prompt->num_characters) {
|
||||
user_input[user_input_length] = ch;
|
||||
user_input_length++;
|
||||
user_input[user_input_length] = '\0';
|
||||
move(0, 0);
|
||||
printw("Size: %d", strlen(user_input));
|
||||
}
|
||||
} else if (ch == KEY_BACKSPACE) {
|
||||
if (user_input_length > 0) {
|
||||
user_input_length--;
|
||||
user_input[user_input_length] = '\0';
|
||||
move(0, 0);
|
||||
printw("Size: %d", strlen(user_input));
|
||||
}
|
||||
} else if (ch == ' ') {
|
||||
printw("RESET TEST");
|
||||
new_test = 1;
|
||||
}
|
||||
|
||||
/* Prints typing prompt after all input is done processing */
|
||||
/* print_typing_prompt(win, prompt, "Hello"); */
|
||||
}
|
||||
|
||||
free(prompt);
|
||||
@ -124,6 +146,8 @@ int main() {
|
||||
|
||||
words_file = fopen("words.txt", "r");
|
||||
|
||||
print_centered_text(stdscr, 4, "Loading...");
|
||||
|
||||
if (has_colors() == FALSE) {
|
||||
print_centered_text(stdscr, 4, "Your terminal does not support color");
|
||||
print_centered_text(stdscr, 5, "This may result in unexpected behavior\n");
|
||||
@ -147,6 +171,8 @@ int main() {
|
||||
parse_words_file(words_file, word_array);
|
||||
}
|
||||
|
||||
clear();
|
||||
|
||||
while (run) {
|
||||
init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
||||
attron(COLOR_PAIR(2));
|
||||
|
10
utilities.c
10
utilities.c
@ -7,9 +7,12 @@ void clear_word_array(Word_array *array) {
|
||||
for (i = 0; i < array->number_of_words; i++) {
|
||||
free(array->words[i].text);
|
||||
}
|
||||
free(array->words);
|
||||
array->number_of_words = 0;
|
||||
}
|
||||
|
||||
/* Generates a pseudo random number using current millisecond + iteration since
|
||||
code runs too fast */
|
||||
int generate_random(int lower, int upper, int c) {
|
||||
struct timeval te;
|
||||
gettimeofday(&te, NULL);
|
||||
@ -21,7 +24,7 @@ int generate_random(int lower, int upper, int c) {
|
||||
/* This will go through the word_array structure and find the requested
|
||||
amount of words */
|
||||
int generate_words(int number_of_words, Word_array *words, Word_array *to_return) {
|
||||
int i, k;
|
||||
int i, k, len = 0;
|
||||
|
||||
/* Remember to free me after the test ends! */
|
||||
|
||||
@ -29,12 +32,15 @@ int generate_words(int number_of_words, Word_array *words, Word_array *to_return
|
||||
to_return->words = malloc(sizeof(Word) * number_of_words);
|
||||
|
||||
for (i = 0; i < number_of_words; i++) {
|
||||
k = generate_random(0, words->number_of_words, i);
|
||||
k = generate_random(0, words->number_of_words - 1, i);
|
||||
len += words->words[k].length + 1;
|
||||
to_return->words[i].length = words->words[k].length;
|
||||
to_return->words[i].text = malloc(words->words[k].length);
|
||||
strcpy(to_return->words[i].text, words->words[k].text);
|
||||
}
|
||||
|
||||
to_return->num_characters = len;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -5,13 +5,20 @@
|
||||
#define SUCCESS 1
|
||||
#define FAILURE 0
|
||||
|
||||
/* Word Structure
|
||||
length - length of word
|
||||
text - word String */
|
||||
typedef struct {
|
||||
int length;
|
||||
char *text;
|
||||
} Word;
|
||||
|
||||
/* Word Array Structure
|
||||
number_of_words - stores number of words
|
||||
num_characters - is total character count of array (including spaces)
|
||||
words - Array of 'Word' structures */
|
||||
typedef struct {
|
||||
int number_of_words;
|
||||
int number_of_words, num_characters;
|
||||
Word *words;
|
||||
} Word_array;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user