Mercurial > louis > kiibohd-controller
comparison Debug/cli/cli.c @ 319:3994a5a68793
Cleanup CLI history
Add all the spaces :)
author | Rowan Decker <Smasher816@gmail.com> |
---|---|
date | Thu, 02 Apr 2015 23:53:19 -0700 |
parents | 217e7bd8a0a4 |
children | 18c3c4924f20 |
comparison
equal
deleted
inserted
replaced
318:217e7bd8a0a4 | 319:3994a5a68793 |
---|---|
158 | 158 |
159 // Process the current line buffer | 159 // Process the current line buffer |
160 CLI_commandLookup(); | 160 CLI_commandLookup(); |
161 | 161 |
162 // Add the command to the history | 162 // Add the command to the history |
163 cli_saveHistory(CLILineBuffer); | 163 cli_saveHistory( CLILineBuffer ); |
164 | 164 |
165 // Keep the array circular, discarding the older entries | 165 // Keep the array circular, discarding the older entries |
166 if (CLIHistoryTail < CLIHistoryHead) | 166 if ( CLIHistoryTail < CLIHistoryHead ) |
167 CLIHistoryHead = (CLIHistoryHead+1)%CLIMaxHistorySize; | 167 CLIHistoryHead = ( CLIHistoryHead + 1 ) % CLIMaxHistorySize; |
168 CLIHistoryTail++; | 168 CLIHistoryTail++; |
169 if (CLIHistoryTail==CLIMaxHistorySize) | 169 if ( CLIHistoryTail == CLIMaxHistorySize ) |
170 { | 170 { |
171 CLIHistoryTail = 0; | 171 CLIHistoryTail = 0; |
172 CLIHistoryHead = 1; | 172 CLIHistoryHead = 1; |
173 } | 173 } |
174 | 174 |
175 CLIHistoryCurrent = CLIHistoryTail; // 'Up' starts at the last item | 175 CLIHistoryCurrent = CLIHistoryTail; // 'Up' starts at the last item |
176 cli_saveHistory(NULL); // delete the old temp buffer | 176 cli_saveHistory( NULL ); // delete the old temp buffer |
177 | 177 |
178 // Reset the buffer | 178 // Reset the buffer |
179 CLILineBufferCurrent = 0; | 179 CLILineBufferCurrent = 0; |
180 | 180 |
181 // Reset the prompt after processing has finished | 181 // Reset the prompt after processing has finished |
198 | 198 |
199 case 0x1B: // Esc / Escape codes | 199 case 0x1B: // Esc / Escape codes |
200 // Check for other escape sequence | 200 // Check for other escape sequence |
201 | 201 |
202 // \e[ is an escape code in vt100 compatable terminals | 202 // \e[ is an escape code in vt100 compatable terminals |
203 if (CLILineBufferCurrent>=prev_buf_pos+3 | 203 if ( CLILineBufferCurrent >= prev_buf_pos + 3 |
204 && CLILineBuffer[prev_buf_pos]==0x1B | 204 && CLILineBuffer[ prev_buf_pos ] == 0x1B |
205 && CLILineBuffer[prev_buf_pos+1]==0x5B) | 205 && CLILineBuffer[ prev_buf_pos + 1] == 0x5B ) |
206 { | 206 { |
207 // Arrow Keys: A (0x41) = Up, B (0x42) = Down, C (0x43) = Right, D (0x44) = Left | 207 // Arrow Keys: A (0x41) = Up, B (0x42) = Down, C (0x43) = Right, D (0x44) = Left |
208 | 208 |
209 if (CLILineBuffer[prev_buf_pos+2]==0x41) // Hist prev | 209 if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x41 ) // Hist prev |
210 { | 210 { |
211 if (CLIHistoryCurrent==CLIHistoryTail) | 211 if ( CLIHistoryCurrent == CLIHistoryTail ) |
212 { | 212 { |
213 // Is first time pressing arrow. Save the current buffer | 213 // Is first time pressing arrow. Save the current buffer |
214 CLILineBuffer[prev_buf_pos] = '\0'; | 214 CLILineBuffer[ prev_buf_pos ] = '\0'; |
215 cli_saveHistory(CLILineBuffer); | 215 cli_saveHistory( CLILineBuffer ); |
216 } | 216 } |
217 | 217 |
218 // Grab the previus item from the history if there is one | 218 // Grab the previus item from the history if there is one |
219 if (RING_PREV(CLIHistoryCurrent)!=RING_PREV(CLIHistoryHead)) | 219 if ( RING_PREV( CLIHistoryCurrent ) != RING_PREV( CLIHistoryHead ) ) |
220 CLIHistoryCurrent = RING_PREV(CLIHistoryCurrent); | 220 CLIHistoryCurrent = RING_PREV( CLIHistoryCurrent ); |
221 cli_retreiveHistory(CLIHistoryCurrent); | 221 cli_retreiveHistory( CLIHistoryCurrent ); |
222 } | 222 } |
223 if (CLILineBuffer[prev_buf_pos+2]==0x42) // Hist next | 223 if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x42 ) // Hist next |
224 { | 224 { |
225 // Grab the next item from the history if it exists | 225 // Grab the next item from the history if it exists |
226 if (RING_NEXT(CLIHistoryCurrent)!=RING_NEXT(CLIHistoryTail)) | 226 if ( RING_NEXT( CLIHistoryCurrent ) != RING_NEXT( CLIHistoryTail ) ) |
227 CLIHistoryCurrent = RING_NEXT(CLIHistoryCurrent); | 227 CLIHistoryCurrent = RING_NEXT( CLIHistoryCurrent ); |
228 cli_retreiveHistory(CLIHistoryCurrent); | 228 cli_retreiveHistory( CLIHistoryCurrent ); |
229 } | 229 } |
230 } | 230 } |
231 return; | 231 return; |
232 | 232 |
233 case 0x08: | 233 case 0x08: |
396 CLILineBuffer[CLILineBufferCurrent++] = *tabMatch++; | 396 CLILineBuffer[CLILineBufferCurrent++] = *tabMatch++; |
397 } | 397 } |
398 } | 398 } |
399 } | 399 } |
400 | 400 |
401 inline int wrap(int kX, int const kLowerBound, int const kUpperBound) | 401 inline int CLI_wrap( int kX, int const kLowerBound, int const kUpperBound ) |
402 { | 402 { |
403 int range_size = kUpperBound - kLowerBound + 1; | 403 int range_size = kUpperBound - kLowerBound + 1; |
404 | 404 |
405 if (kX < kLowerBound) | 405 if ( kX < kLowerBound ) |
406 kX += range_size * ((kLowerBound - kX) / range_size + 1); | 406 kX += range_size * ((kLowerBound - kX) / range_size + 1); |
407 | 407 |
408 return kLowerBound + (kX - kLowerBound) % range_size; | 408 return kLowerBound + (kX - kLowerBound) % range_size; |
409 } | 409 } |
410 | 410 |
411 inline void cli_saveHistory(char *buff) { | 411 inline void CLI_saveHistory( char *buff ) |
412 if (buff==NULL) { | 412 { |
413 if ( buff == NULL ) | |
414 { | |
413 //clear the item | 415 //clear the item |
414 CLIHistoryBuffer[CLIHistoryTail][0] = '\0'; | 416 CLIHistoryBuffer[ CLIHistoryTail ][ 0 ] = '\0'; |
415 return; | 417 return; |
416 } | 418 } |
417 | 419 |
418 // Copy the line to the history | 420 // Copy the line to the history |
419 int i; | 421 int i; |
420 for (i=0; i<CLILineBufferCurrent; i++) | 422 for (i = 0; i < CLILineBufferCurrent; i++) |
421 { | 423 { |
422 CLIHistoryBuffer[CLIHistoryTail][i] = CLILineBuffer[i]; | 424 CLIHistoryBuffer[ CLIHistoryTail ][ i ] = CLILineBuffer[ i ]; |
423 } | 425 } |
424 } | 426 } |
425 | 427 |
426 void cli_retreiveHistory(int index) { | 428 void CLI_retreiveHistory( int index ) |
427 char *histMatch = CLIHistoryBuffer[index]; | 429 { |
430 char *histMatch = CLIHistoryBuffer[ index ]; | |
428 | 431 |
429 // Reset the buffer | 432 // Reset the buffer |
430 CLILineBufferCurrent = 0; | 433 CLILineBufferCurrent = 0; |
431 | 434 |
432 // Reprint the prompt (automatically clears the line) | 435 // Reprint the prompt (automatically clears the line) |
437 | 440 |
438 // There are no index counts, so just copy the whole string to the input buffe | 441 // There are no index counts, so just copy the whole string to the input buffe |
439 CLILineBufferCurrent = 0; | 442 CLILineBufferCurrent = 0; |
440 while ( *histMatch != '\0' ) | 443 while ( *histMatch != '\0' ) |
441 { | 444 { |
442 CLILineBuffer[CLILineBufferCurrent++] = *histMatch++; | 445 CLILineBuffer[ CLILineBufferCurrent++ ] = *histMatch++; |
443 } | 446 } |
444 } | 447 } |
445 | 448 |
446 | 449 |
447 | 450 |