Changeset 1965 for Whitix/trunk

Show
Ignore:
Timestamp:
03/09/09 17:44:32 (3 years ago)
Author:
mwhitworth
Message:

Patch from Kostadin: complete the line using the longest common substring.

Files:
1 modified

Legend:

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

    r1962 r1965  
    2929void ConsDoTabComplete(struct ConsReadContext* context, int i, char* prompt, int promptColor) 
    3030{ 
    31         int total=0, j; 
     31        int total=0, min, j, k; 
    3232        char* results[TABS_TOTAL]; 
    3333        char* readBuffer=context->readBuffer; 
    3434        char* start, *end; 
    3535        char* tabInput; 
     36        char buffer; 
    3637 
    3738        ConsTokenGetStr(readBuffer, i, &start, &end); 
     
    7677                 * prompt and help the user out. 
    7778                 */ 
     79                  
     80                /* Get the length of the shortest string. */ 
     81                min = strlen(results[0]); 
     82                for(j=1; j<total; j++) 
     83                { 
     84                        if(strlen(results[j]) < min) 
     85                                min = strlen(results[j]); 
     86                } 
     87                 
     88                /* The result. */ 
     89                char res[min+1]; 
     90                 
     91                /* Get the longest common prefix. */ 
     92                for(j=0; j<min; j++) 
     93                { 
     94                        buffer = results[0][j]; 
     95                        for(k=1; k<total; k++) 
     96                        { 
     97                                if(buffer != results[k][j]) 
     98                                { 
     99                                        /* Exit the loops. */ 
     100                                        goto out; 
     101                                } 
     102                        } 
     103                        res[j] = buffer; 
     104                } 
    78105 
    79                 /* Render the prompt and readBuffer again. TODO: Call bufferUpdate? */ 
     106out: 
     107                res[j] = '\0'; 
     108                /* Update the buffer. */ 
     109                strcpy(context->readBuffer,res); 
     110 
     111                /* Render the prompt and readBuffer again. */ 
    80112                ConsSetForeColor(promptColor); 
    81113                fputs(prompt, stdout); 
     
    97129                } 
    98130 
    99                 if (j > 0 && j > start+1) 
     131                if (j > 0 && j >= start+1) 
    100132                { 
    101                         context->tabSetup(context, readBuffer, start, j);                
     133                        context->tabSetup(context, readBuffer, start, j); 
    102134                        ConsBufferUpdate(context, j-1, -1); 
    103135                }