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
|
# Macros
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -ansi -Wall -g -O0 -Wshadow -Wwrite-strings \
|
CFLAGS = -ansi -Wall -g -O0 -Wshadow \
|
||||||
-fstack-protector-all
|
-fstack-protector-all
|
||||||
LDLIBS = -lncurses
|
LDLIBS = -lncurses
|
||||||
|
|
||||||
|
@ -8,17 +8,16 @@
|
|||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
|
|
||||||
/* Prints Centered Text To Console Screen */
|
/* 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 text_spacing = strlen(str);
|
||||||
int center_col = (win->_maxx - text_spacing) / 2;
|
int center_col = (win->_maxx - text_spacing) / 2;
|
||||||
|
|
||||||
mvwprintw(win, row, center_col, str);
|
mvwprintw(win, row, center_col, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prints centered text for menu items */
|
/* Prints centered text for menu items */
|
||||||
void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_STRING],
|
void print_centered_text_menu(WINDOW *win, int row, int target, char str[][MAX_STRING],
|
||||||
int highlight, int elements) {
|
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 (i = 0; i < elements; i++) {
|
||||||
for (k = 0; k < strlen(str[i]); k++) {
|
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) {
|
/* Prints the typing prompt onto the terminal.
|
||||||
int run = 1, ch, i, words, win_x = win->_maxx, new_test = 1;
|
Automatically centers, wraps, and scrolls through text */
|
||||||
char str[1024];
|
void print_typing_prompt(WINDOW *win, Word_array *prompt, char *user_input) {
|
||||||
Word_array *prompt = NULL;
|
int i, window_width = win->_maxx;
|
||||||
|
|
||||||
printw("\nPrompt: \n");
|
/* Split prompt into Strings that fit terminal width */
|
||||||
|
|
||||||
for (i = 0; i < words; i++) {
|
for (i = 0; i < prompt->number_of_words; i++) {
|
||||||
/* Will print the words centered in the screen */
|
mvwprintw(win, 3, 0, "%d: %s ", i, prompt->words[i].text);
|
||||||
/* Will scroll down when user finishes middle line */
|
|
||||||
/* Will automatically wrap text to next line based on
|
|
||||||
how big the console size is */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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) {
|
while (run) {
|
||||||
if (new_test == 1) {
|
if (new_test == 1) {
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
|
/* Reset str */
|
||||||
str[0] = '\0';
|
str[0] = '\0';
|
||||||
|
|
||||||
if (mode == 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);
|
clear_word_array(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 10 is a placeholder */
|
||||||
generate_words(10, word_array, prompt);
|
generate_words(10, word_array, prompt);
|
||||||
|
|
||||||
new_test = 0;
|
if (user_input != NULL) {
|
||||||
for (i = 0; i < prompt->number_of_words; i++) {
|
free(user_input);
|
||||||
printw("%d: %s ", i, prompt->words[i].text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user_input = malloc(prompt->num_characters + 1);
|
||||||
|
user_input[0] = '\0';
|
||||||
|
user_input_length = 0;
|
||||||
|
|
||||||
|
new_test = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = getch();
|
ch = getch();
|
||||||
if (isalpha(ch)) {
|
if (isalpha(ch)) {
|
||||||
printw("%c", ch);
|
/* Ensures user does not type outside of character limit */
|
||||||
}
|
if (user_input_length < prompt->num_characters) {
|
||||||
|
user_input[user_input_length] = ch;
|
||||||
if (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");
|
printw("RESET TEST");
|
||||||
new_test = 1;
|
new_test = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Prints typing prompt after all input is done processing */
|
||||||
|
/* print_typing_prompt(win, prompt, "Hello"); */
|
||||||
}
|
}
|
||||||
|
|
||||||
free(prompt);
|
free(prompt);
|
||||||
@ -124,6 +146,8 @@ int main() {
|
|||||||
|
|
||||||
words_file = fopen("words.txt", "r");
|
words_file = fopen("words.txt", "r");
|
||||||
|
|
||||||
|
print_centered_text(stdscr, 4, "Loading...");
|
||||||
|
|
||||||
if (has_colors() == FALSE) {
|
if (has_colors() == FALSE) {
|
||||||
print_centered_text(stdscr, 4, "Your terminal does not support color");
|
print_centered_text(stdscr, 4, "Your terminal does not support color");
|
||||||
print_centered_text(stdscr, 5, "This may result in unexpected behavior\n");
|
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);
|
parse_words_file(words_file, word_array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clear();
|
||||||
|
|
||||||
while (run) {
|
while (run) {
|
||||||
init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
||||||
attron(COLOR_PAIR(2));
|
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++) {
|
for (i = 0; i < array->number_of_words; i++) {
|
||||||
free(array->words[i].text);
|
free(array->words[i].text);
|
||||||
}
|
}
|
||||||
|
free(array->words);
|
||||||
array->number_of_words = 0;
|
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) {
|
int generate_random(int lower, int upper, int c) {
|
||||||
struct timeval te;
|
struct timeval te;
|
||||||
gettimeofday(&te, NULL);
|
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
|
/* This will go through the word_array structure and find the requested
|
||||||
amount of words */
|
amount of words */
|
||||||
int generate_words(int number_of_words, Word_array *words, Word_array *to_return) {
|
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! */
|
/* 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);
|
to_return->words = malloc(sizeof(Word) * number_of_words);
|
||||||
|
|
||||||
for (i = 0; i < number_of_words; i++) {
|
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].length = words->words[k].length;
|
||||||
to_return->words[i].text = malloc(words->words[k].length);
|
to_return->words[i].text = malloc(words->words[k].length);
|
||||||
strcpy(to_return->words[i].text, words->words[k].text);
|
strcpy(to_return->words[i].text, words->words[k].text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
to_return->num_characters = len;
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,13 +5,20 @@
|
|||||||
#define SUCCESS 1
|
#define SUCCESS 1
|
||||||
#define FAILURE 0
|
#define FAILURE 0
|
||||||
|
|
||||||
|
/* Word Structure
|
||||||
|
length - length of word
|
||||||
|
text - word String */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int length;
|
int length;
|
||||||
char *text;
|
char *text;
|
||||||
} Word;
|
} 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 {
|
typedef struct {
|
||||||
int number_of_words;
|
int number_of_words, num_characters;
|
||||||
Word *words;
|
Word *words;
|
||||||
} Word_array;
|
} Word_array;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user