1
0
Fork 0

Refactored parameter autocompleters

This commit is contained in:
James Booth 2012-10-22 00:16:22 +01:00
parent e0732ad0a5
commit 5353bb47d6
2 changed files with 57 additions and 67 deletions

View File

@ -58,12 +58,16 @@
#include "preferences.h"
#include "ui.h"
typedef char*(*autocomplete_func)(char *);
static WINDOW *inp_win;
static int pad_start = 0;
static int _handle_edit(const int ch, char *input, int *size);
static int _printable(const int ch);
static void _replace_input(char *input, const char * const new_input, int *size);
static void _parameter_autocomplete(char *input, int *size, char *command,
autocomplete_func func);
void
create_input_window(void)
@ -320,7 +324,7 @@ _handle_edit(const int ch, char *input, int *size)
case 9: // tab
// autocomplete commands
// autocomplete command
if ((strncmp(input, "/", 1) == 0) && (!str_contains(input, *size, ' '))) {
for(i = 0; i < *size; i++) {
inp_cpy[i] = input[i];
@ -334,55 +338,12 @@ _handle_edit(const int ch, char *input, int *size)
free(auto_msg);
free(found);
}
// autcomplete /msg recipient
} else if ((strncmp(input, "/msg ", 5) == 0) && (*size > 5)) {
for(i = 5; i < *size; i++) {
inp_cpy[i-5] = input[i];
}
inp_cpy[(*size) - 5] = '\0';
found = contact_list_find_contact(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((5 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/msg ");
strcat(auto_msg, found);
_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
// autocomplete /connect username
} else if ((strncmp(input, "/connect ", 9) == 0) && (*size > 9)) {
for(i = 9; i < *size; i++) {
inp_cpy[i-9] = input[i];
}
inp_cpy[(*size) - 9] = '\0';
found = prefs_find_login(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((9 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/connect ");
strcat(auto_msg, found);
_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
// autocomplete /help command
} else if ((strncmp(input, "/help ", 6) == 0) && (*size > 6)) {
for(i = 6; i < *size; i++) {
inp_cpy[i-6] = input[i];
}
inp_cpy[(*size) - 6] = '\0';
found = cmd_help_complete(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((6 + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, "/help ");
strcat(auto_msg, found);
_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
}
_parameter_autocomplete(input, size, "/msg", contact_list_find_contact);
_parameter_autocomplete(input, size, "/connect", prefs_find_login);
_parameter_autocomplete(input, size, "/help", cmd_help_complete);
return 1;
default:
@ -413,3 +374,32 @@ _replace_input(char *input, const char * const new_input, int *size)
for (i = 0; i < *size; i++)
waddch(inp_win, input[i]);
}
static void
_parameter_autocomplete(char *input, int *size, char *command,
autocomplete_func func)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = func(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
_replace_input(input, auto_msg, size);
free(auto_msg);
free(found);
}
}
free(command_cpy);
}

View File

@ -339,7 +339,7 @@ static void find_first_exists(void)
char *search = (char *) malloc(2 * sizeof(char));
strcpy(search, "B");
char *result = find_contact(search);
char *result = contact_list_find_contact(search);
assert_string_equals("Bob", result);
free(result);
free(search);
@ -351,7 +351,7 @@ static void find_second_exists(void)
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Dav");
char *result = contact_list_find_contact("Dav");
assert_string_equals("Dave", result);
free(result);
}
@ -362,7 +362,7 @@ static void find_third_exists(void)
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Ja");
char *result = contact_list_find_contact("Ja");
assert_string_equals("James", result);
free(result);
}
@ -373,13 +373,13 @@ static void find_returns_null(void)
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Mike");
char *result = contact_list_find_contact("Mike");
assert_is_null(result);
}
static void find_on_empty_returns_null(void)
{
char *result = find_contact("James");
char *result = contact_list_find_contact("James");
assert_is_null(result);
}
@ -389,8 +389,8 @@ static void find_twice_returns_second_when_two_match(void)
contact_list_add("Jamie", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result1 = find_contact("Jam");
char *result2 = find_contact(result1);
char *result1 = contact_list_find_contact("Jam");
char *result2 = contact_list_find_contact(result1);
assert_string_equals("Jamie", result2);
free(result1);
free(result2);
@ -409,11 +409,11 @@ static void find_five_times_finds_fifth(void)
contact_list_add("Jamy", NULL, NULL);
contact_list_add("Jamz", NULL, NULL);
char *result1 = find_contact("Jam");
char *result2 = find_contact(result1);
char *result3 = find_contact(result2);
char *result4 = find_contact(result3);
char *result5 = find_contact(result4);
char *result1 = contact_list_find_contact("Jam");
char *result2 = contact_list_find_contact(result1);
char *result3 = contact_list_find_contact(result2);
char *result4 = contact_list_find_contact(result3);
char *result5 = contact_list_find_contact(result4);
assert_string_equals("Jamo", result5);
free(result1);
free(result2);
@ -428,9 +428,9 @@ static void find_twice_returns_first_when_two_match_and_reset(void)
contact_list_add("Jamie", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result1 = find_contact("Jam");
reset_search_attempts();
char *result2 = find_contact(result1);
char *result1 = contact_list_find_contact("Jam");
contact_list_reset_search_attempts();
char *result2 = contact_list_find_contact(result1);
assert_string_equals("James", result2);
free(result1);
free(result2);
@ -444,10 +444,10 @@ static void removed_contact_not_in_search(void)
contact_list_add("James", NULL, NULL);
contact_list_add("Jamie", NULL, NULL);
char *result1 = find_contact("Jam"); // Jamatron
char *result2 = find_contact(result1); // Jambo
char *result1 = contact_list_find_contact("Jam"); // Jamatron
char *result2 = contact_list_find_contact(result1); // Jambo
contact_list_remove("James");
char *result3 = find_contact(result2);
char *result3 = contact_list_find_contact(result2);
assert_string_equals("Jamie", result3);
free(result1);
free(result2);