Show
Ignore:
Timestamp:
03/01/09 11:43:05 (3 years ago)
Author:
mwhitworth
Message:

Tidy up and comment tab completion code.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Whitix/trunk/user/sdk/console/tab.c

    r1668 r1962  
    2222} 
    2323 
     24int stringCompare(const void* p1, const void* p2) 
     25{ 
     26        return strcasecmp(*(char**)p1, *(char**)p2); 
     27} 
     28 
    2429void ConsDoTabComplete(struct ConsReadContext* context, int i, char* prompt, int promptColor) 
    2530{ 
     
    2833        char* readBuffer=context->readBuffer; 
    2934        char* start, *end; 
     35        char* tabInput; 
    3036 
    3137        ConsTokenGetStr(readBuffer, i, &start, &end); 
     
    4046        memset(results, 0, TABS_TOTAL*sizeof(char*)); 
    4147         
    42         char* tabInput; 
    43          
    44         tabInput=strndup(start, end-start); 
     48        /* Keep a copy for manipulation. */ 
     49        tabInput = strndup(start, end-start); 
    4550 
    4651        /* Copy the output from tabCompleter, which consists of strdup'ed strings, 
    4752         * until it returns NULL or we run out of results. */ 
    48         while (total < TABS_TOTAL && (results[total]=context->tabCompleter(context, tabInput, total))) 
     53        while (total < TABS_TOTAL && (results[total] = context->tabCompleter(context, tabInput, total))) 
    4954                total++; 
    5055                 
    5156        if (total > 1) 
    5257        { 
     58                int start = 0; 
     59                 
    5360                /* If there's more than one possible completion, print the results to 
    5461                 * the console. */ 
    5562                fputc('\n', stdout); 
     63 
     64                /* Sort into alphabetical order. */ 
     65                qsort(results, total, sizeof(char*), stringCompare); 
    5666 
    5767                for (j=0; j<total; j++) 
     
    6272                                puts(results[j]); 
    6373                } 
    64  
    65 #if 0            
    66                 int dist; 
    6774                 
    68                 /* TODO: MCSS of string. compare to previous string. */ 
    69                  
    70                 int lCd=dist; 
    71                  
    72                 for (j=1; j<total; j++) 
    73                 { 
    74                         int dist=0; 
    75                         char* str=start; 
    76                          
    77                         while (results[j][dist++] == *str++); 
    78                          
    79                         if (dist < lCd) 
    80                                 lCd=dist; 
    81                 } 
    82                  
    83                 printf("lCd = %d\n", lCd); 
    84                  
    85                 if (lCd > 0) 
    86                         /* TODO: Insert string. */ 
    87                         strcat(readBuffer, results[0]+strlen(start)); 
    88 #endif 
     75                /* Find the longest common prefix of the string, so we can update the 
     76                 * prompt and help the user out. 
     77                 */ 
    8978 
    9079                /* Render the prompt and readBuffer again. TODO: Call bufferUpdate? */ 
    91                  ConsSetForeColor(promptColor); 
     80                ConsSetForeColor(promptColor); 
    9281                fputs(prompt, stdout); 
    9382                ConsSetForeColor(CONS_COLOR_WHITE); 
    94                 int start=0; 
    9583         
     84                /* Go through and render the prefix line of text? TODO: Bug here. 
     85                 * Type 'm' and the line won't render again properly. 
     86                 */ 
    9687                for (j=0; j < strlen(readBuffer); j++) 
    9788                { 
     
    9990                        { 
    10091                                context->tabSetup(context, readBuffer, start, j-1); 
     92                                 
    10193                                ConsBufferUpdate(context, j-1, -1); 
    10294                                putchar(' '); 
     
    115107                /* Complete the only result, and render it to the console. */ 
    116108                char* begin=start+strlen(start); 
    117 //              int numChars=strlen(results[0])-(end-start); 
    118                  
    119                 /* Insert the results[0]+strlen(start) string at start+strlen(start) */ 
    120 //              memmove( 
    121  
    122 //              printf("start = %s, %d, %s\n", start, strlen(start), end); 
    123  
    124                 /* Move the rest of the string to the right by the number of new characters 
    125                  * from results[0] */ 
    126 #if 0 
    127                 memmove(end+numChars, end, numChars); 
    128                  
    129                 printf("numChars = %d, readBuffer = '%s'\n", numChars, readBuffer); 
    130                 while (1); 
    131 #endif 
    132109 
    133110                strcpy(end-1, results[0]+(end-start-1)); 
    134111                 
    135112                ConsBufferUpdate(context, begin-readBuffer, 1); 
    136  
    137113                ConsCursorMoveRel(strlen(readBuffer)-(begin-readBuffer), 0); 
    138114