cmdedit: fix my bug, improve code a bit

This commit is contained in:
Denis Vlasenko 2006-12-19 19:30:37 +00:00
parent 28fbd69bf8
commit f58906b646

View File

@ -989,18 +989,19 @@ static void showfiles(void)
for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) { for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
str_add_chr[0] = add_char_to_match[n]; str_add_chr[0] = add_char_to_match[n];
acol = str_add_chr[0] ? column_width - 1 : column_width; acol = str_add_chr[0] ? column_width - 1 : column_width;
printf("%s%s", matches[n], str_add_chr); printf("%s%s%-*s", matches[n], str_add_chr,
l = strlen(matches[n]); acol - strlen(matches[n]), "");
while (l < acol) {
putchar(' ');
l++;
}
} }
str_add_chr[0] = add_char_to_match[n]; str_add_chr[0] = add_char_to_match[n];
printf("%s%s\n", matches[n], str_add_chr); printf("%s%s\n", matches[n], str_add_chr);
} }
} }
static int match_compare(const void *a, const void *b)
{
return strcmp(*(char**)a, *(char**)b);
}
static void input_tab(int *lastWasTab) static void input_tab(int *lastWasTab)
{ {
/* Do TAB completion */ /* Do TAB completion */
@ -1016,7 +1017,6 @@ static void input_tab(int *lastWasTab)
return; return;
} }
if (! *lastWasTab) { if (! *lastWasTab) {
char *tmp, *tmp1; char *tmp, *tmp1;
int len_found; int len_found;
char matchBuf[BUFSIZ]; char matchBuf[BUFSIZ];
@ -1046,38 +1046,27 @@ static void input_tab(int *lastWasTab)
/* Try to match any executable in our path and everything /* Try to match any executable in our path and everything
* in the current working directory that matches. */ * in the current working directory that matches. */
exe_n_cwd_tab_completion(matchBuf, find_type); exe_n_cwd_tab_completion(matchBuf, find_type);
/* Remove duplicate found and sort */ /* Sort, then remove any duplicates found */
if (matches) { if (matches) {
int i, n; int i, n = 0;
/* strcmp is int(*f)(const char*, const char*) */ qsort(matches, num_matches, sizeof(char*), match_compare);
/* qsort wants int(*f)(const void*, const void*) */ for (i = 0; i < num_matches - 1; ++i) {
/* We cheat here :) */ if (matches[i] && matches[i+1]) {
qsort(matches, num_matches, sizeof(char*), (void*)strcmp); if (strcmp(matches[i], matches[i+1]) == 0) {
i = 0; free(matches[i]);
while (i < num_matches - 1) { matches[i] = 0;
n = i + 1; } else {
if (matches[i] && matches[n]) { add_char_to_match[n] = add_char_to_match[i];
while (n < num_matches matches[n++] = matches[i];
&& !strcmp(matches[i], matches[n])) {
free(matches[n]);
matches[n] = 0;
n++;
} }
} }
i = n;
} }
n = 0; add_char_to_match[n] = add_char_to_match[num_matches-1];
for(i = 0; i < num_matches; i++) matches[n++] = matches[num_matches-1];
if (matches[i]) {
matches[n] = matches[i];
add_char_to_match[n] = add_char_to_match[i];
n++;
}
num_matches = n; num_matches = n;
} }
/* Did we find exactly one match? */ /* Did we find exactly one match? */
if (!matches || num_matches > 1) { if (!matches || num_matches > 1) {
beep(); beep();
if (!matches) if (!matches)
return; /* not found */ return; /* not found */