Merge pull request #179 from grissiom/finsh-ex

Finsh ex
This commit is contained in:
Bernard Xiong 2013-10-18 17:06:36 -07:00
commit a0ffbc1fb8
3 changed files with 176 additions and 100 deletions

View File

@ -156,6 +156,18 @@ static void * ThreadforKeyGet(void * lpParam)
SAVEKEY(0x5b); SAVEKEY(0x5b);
SAVEKEY(0x42); SAVEKEY(0x42);
} }
else if (key == 0x4b)//<- 0x1b 0x5b 0x44
{
SAVEKEY(0x1b);
SAVEKEY(0x5b);
SAVEKEY(0x44);
}
else if (key == 0x4d)//<- 0x1b 0x5b 0x43
{
SAVEKEY(0x1b);
SAVEKEY(0x5b);
SAVEKEY(0x43);
}
continue; continue;
} }

View File

@ -244,96 +244,26 @@ void finsh_run_line(struct finsh_parser* parser, const char *line)
} }
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
rt_bool_t finsh_handle_history(struct finsh_shell* shell, char ch) rt_bool_t finsh_handle_history(struct finsh_shell* shell)
{ {
/*
* handle up and down key
* up key : 0x1b 0x5b 0x41
* down key: 0x1b 0x5b 0x42
*/
if (ch == 0x1b)
{
shell->stat = WAIT_SPEC_KEY;
return RT_TRUE;
}
if ((shell->stat == WAIT_SPEC_KEY))
{
if (ch == 0x5b)
{
shell->stat = WAIT_FUNC_KEY;
return RT_TRUE;
}
shell->stat = WAIT_NORMAL;
return RT_FALSE;
}
if (shell->stat == WAIT_FUNC_KEY)
{
shell->stat = WAIT_NORMAL;
if (ch == 0x41) /* up key */
{
/* prev history */
if (shell->current_history > 0)shell->current_history --;
else
{
shell->current_history = 0;
return RT_TRUE;
}
/* copy the history command */
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_position = strlen(shell->line);
shell->use_history = 1;
}
else if (ch == 0x42) /* down key */
{
/* next history */
if (shell->current_history < shell->history_count - 1)
shell->current_history ++;
else
{
/* set to the end of history */
if (shell->history_count != 0)
{
shell->current_history = shell->history_count - 1;
}
else return RT_TRUE;
}
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_position = strlen(shell->line);
shell->use_history = 1;
}
if (shell->use_history)
{
#if defined(_WIN32) #if defined(_WIN32)
int i; int i;
rt_kprintf("\r"); rt_kprintf("\r");
for(i=0; i<= 60; i++) for(i=0; i<= 60; i++)
putchar(' '); putchar(' ');
rt_kprintf("\r"); rt_kprintf("\r");
#else #else
rt_kprintf("\033[2K\r"); rt_kprintf("\033[2K\r");
#endif #endif
rt_kprintf("%s%s", FINSH_PROMPT, shell->line); rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
return RT_TRUE;;
}
}
return RT_FALSE; return RT_FALSE;
} }
void finsh_push_history(struct finsh_shell* shell) void finsh_push_history(struct finsh_shell* shell)
{ {
if ((shell->use_history == 0) && (shell->line_position != 0)) if (shell->line_position != 0)
{ {
/* push history */ /* push history */
if (shell->history_count >= FINSH_HISTORY_LINES) if (shell->history_count >= FINSH_HISTORY_LINES)
@ -393,10 +323,96 @@ void finsh_thread_entry(void* parameter)
/* read one character from device */ /* read one character from device */
while (rt_device_read(shell->device, 0, &ch, 1) == 1) while (rt_device_read(shell->device, 0, &ch, 1) == 1)
{ {
/* handle history key */ /*
#ifdef FINSH_USING_HISTORY * handle control key
if (finsh_handle_history(shell, ch) == RT_TRUE) continue; * up key : 0x1b 0x5b 0x41
#endif * down key: 0x1b 0x5b 0x42
* right key:0x1b 0x5b 0x43
* left key: 0x1b 0x5b 0x44
*/
if (ch == 0x1b)
{
shell->stat = WAIT_SPEC_KEY;
continue;
}
else if (shell->stat == WAIT_SPEC_KEY)
{
if (ch == 0x5b)
{
shell->stat = WAIT_FUNC_KEY;
continue;
}
shell->stat = WAIT_NORMAL;
}
else if (shell->stat == WAIT_FUNC_KEY)
{
shell->stat = WAIT_NORMAL;
if (ch == 0x41) /* up key */
{
#ifdef FINSH_USING_HISTORY
/* prev history */
if (shell->current_history > 0)
shell->current_history --;
else
{
shell->current_history = 0;
continue;
}
/* copy the history command */
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_curpos = shell->line_position = strlen(shell->line);
finsh_handle_history(shell);
#endif
continue;
}
else if (ch == 0x42) /* down key */
{
#ifdef FINSH_USING_HISTORY
/* next history */
if (shell->current_history < shell->history_count - 1)
shell->current_history ++;
else
{
/* set to the end of history */
if (shell->history_count != 0)
shell->current_history = shell->history_count - 1;
else
continue;
}
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
FINSH_CMD_SIZE);
shell->line_curpos = shell->line_position = strlen(shell->line);
finsh_handle_history(shell);
#endif
continue;
}
else if (ch == 0x44) /* left key */
{
if (shell->line_curpos)
{
rt_kprintf("\b");
shell->line_curpos --;
}
continue;
}
else if (ch == 0x43) /* right key */
{
if (shell->line_curpos < shell->line_position)
{
rt_kprintf("%c", shell->line[shell->line_curpos]);
shell->line_curpos ++;
}
continue;
}
}
/* handle CR key */ /* handle CR key */
if (ch == '\r') if (ch == '\r')
@ -410,22 +426,49 @@ void finsh_thread_entry(void* parameter)
/* handle tab key */ /* handle tab key */
else if (ch == '\t') else if (ch == '\t')
{ {
int i;
/* move the cursor to the beginning of line */
for (i = 0; i < shell->line_curpos; i++)
rt_kprintf("\b");
/* auto complete */ /* auto complete */
finsh_auto_complete(&shell->line[0]); finsh_auto_complete(&shell->line[0]);
/* re-calculate position */ /* re-calculate position */
shell->line_position = strlen(shell->line); shell->line_curpos = shell->line_position = strlen(shell->line);
continue; continue;
} }
/* handle backspace key */ /* handle backspace key */
else if (ch == 0x7f || ch == 0x08) else if (ch == 0x7f || ch == 0x08)
{ {
if (shell->line_position != 0) /* note that shell->line_curpos >= 0 */
if (shell->line_curpos == 0)
continue;
shell->line_position--;
shell->line_curpos--;
if (shell->line_position > shell->line_curpos)
{ {
rt_kprintf("%c %c", ch, ch); int i;
rt_memmove(&shell->line[shell->line_curpos],
&shell->line[shell->line_curpos + 1],
shell->line_position - shell->line_curpos);
shell->line[shell->line_position] = 0;
rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
/* move the cursor to the origin position */
for (i = shell->line_curpos; i <= shell->line_position; i++)
rt_kprintf("\b");
} }
if (shell->line_position <= 0) shell->line_position = 0; else
else shell->line_position --; {
shell->line[shell->line_position] = 0; rt_kprintf("\b \b");
shell->line[shell->line_position] = 0;
}
continue; continue;
} }
@ -457,19 +500,40 @@ void finsh_thread_entry(void* parameter)
rt_kprintf(FINSH_PROMPT); rt_kprintf(FINSH_PROMPT);
memset(shell->line, 0, sizeof(shell->line)); memset(shell->line, 0, sizeof(shell->line));
shell->line_position = 0; shell->line_curpos = shell->line_position = 0;
break; break;
} }
/* it's a large line, discard it */ /* it's a large line, discard it */
if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0; if (shell->line_position >= FINSH_CMD_SIZE)
shell->line_position = 0;
/* normal character */ /* normal character */
shell->line[shell->line_position] = ch; ch = 0; if (shell->line_curpos < shell->line_position)
if (shell->echo_mode) rt_kprintf("%c", shell->line[shell->line_position]); {
int i;
rt_memmove(&shell->line[shell->line_curpos + 1],
&shell->line[shell->line_curpos],
shell->line_position - shell->line_curpos);
shell->line[shell->line_curpos] = ch;
if (shell->echo_mode)
rt_kprintf("%s", &shell->line[shell->line_curpos]);
/* move the cursor to new position */
for (i = shell->line_curpos; i < shell->line_position; i++)
rt_kprintf("\b");
}
else
{
shell->line[shell->line_position] = ch;
rt_kprintf("%c", ch);
}
ch = 0;
shell->line_position ++; shell->line_position ++;
shell->use_history = 0; /* it's a new command */ shell->line_curpos++;
} /* end of device read */ } /* end of device read */
} }
} }

View File

@ -51,17 +51,17 @@ const char* finsh_get_prompt(void);
#endif #endif
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
#ifndef FINSH_HISTORY_LINES
#define FINSH_HISTORY_LINES 5
#endif
#endif
enum input_stat enum input_stat
{ {
WAIT_NORMAL, WAIT_NORMAL,
WAIT_SPEC_KEY, WAIT_SPEC_KEY,
WAIT_FUNC_KEY, WAIT_FUNC_KEY,
}; };
#ifndef FINSH_HISTORY_LINES
#define FINSH_HISTORY_LINES 5
#endif
#endif
struct finsh_shell struct finsh_shell
{ {
struct rt_semaphore rx_sem; struct rt_semaphore rx_sem;
@ -69,7 +69,6 @@ struct finsh_shell
enum input_stat stat; enum input_stat stat;
rt_uint8_t echo_mode:1; rt_uint8_t echo_mode:1;
rt_uint8_t use_history:1;
#ifdef FINSH_USING_HISTORY #ifdef FINSH_USING_HISTORY
rt_uint16_t current_history; rt_uint16_t current_history;
@ -82,6 +81,7 @@ struct finsh_shell
char line[FINSH_CMD_SIZE]; char line[FINSH_CMD_SIZE];
rt_uint8_t line_position; rt_uint8_t line_position;
rt_uint8_t line_curpos;
rt_device_t device; rt_device_t device;
}; };