Patch add shell history(update for issue #23) (#24)

* add empty line if out of range

* support update history command

* query immediately

* fix bug

* fix bug(final,without debug)

* fix bug

* 格式修正

* fix bug

* fix

* less_empty_row

* retain history

* Improved formatting

Co-authored-by: fslongjin <longjin@RinGoTek.cn>
This commit is contained in:
wang904 2022-08-18 21:38:11 +08:00 committed by GitHub
parent 992fa27d9e
commit 4950d43e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,11 +21,13 @@
int shell_readline(int fd, char *buf); int shell_readline(int fd, char *buf);
void print_ascii_logo(); void print_ascii_logo();
extern char *shell_current_path; extern char *shell_current_path;
//保存的历史命令 //保存的历史命令(瞬时更改)
char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
//真正的历史命令
char real_history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
int count_history; int count_history;
//现在对应的命令 //现在对应的命令
int pointer; int current_command_index;
/** /**
* @brief shell主循环 * @brief shell主循环
* *
@ -34,7 +36,7 @@ int pointer;
void main_loop(int kb_fd) void main_loop(int kb_fd)
{ {
count_history = 0; count_history = 0;
pointer = 1; current_command_index = 0;
unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
// 初始化当前工作目录的路径 // 初始化当前工作目录的路径
@ -54,24 +56,28 @@ void main_loop(int kb_fd)
memset(input_buffer, 0, INPUT_BUFFER_SIZE); memset(input_buffer, 0, INPUT_BUFFER_SIZE);
// 循环读取每一行到buffer // 循环读取每一行到buffer
count_history++;
int count = shell_readline(kb_fd, input_buffer); int count = shell_readline(kb_fd, input_buffer);
if (!count || current_command_index < count_history - 1)
count_history--;
if (count)
{
strcpy(real_history_commands[count_history - 1], input_buffer);
count_history++;
memset(history_commands, 0, sizeof(history_commands));
for (int i = 0; i <= count_history - 2; i++)
strcpy(history_commands[i], real_history_commands[i]);
current_command_index = count_history - 1;
}
if (count) if (count)
{ {
char command_origin[strlen(input_buffer)]; char command_origin[strlen(input_buffer)];
strcpy(command_origin, input_buffer); strcpy(command_origin, input_buffer);
int cmd_num = parse_command(input_buffer, &argc, &argv); int cmd_num = parse_command(input_buffer, &argc, &argv);
printf("\n"); printf("\n");
if (cmd_num >= 0) if (cmd_num >= 0)
{
//加入历史命令
strcpy(history_commands[count_history], command_origin);
count_history++;
pointer = count_history;
shell_run_built_in_command(cmd_num, argc, argv); shell_run_built_in_command(cmd_num, argc, argv);
} }
}
else else
printf("\n"); printf("\n");
} }
@ -100,9 +106,7 @@ int main()
void clear_command(int count, char *buf) void clear_command(int count, char *buf)
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{
printf("%c", '\b'); printf("%c", '\b');
}
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
} }
/** /**
@ -113,13 +117,13 @@ void clear_command(int count, char *buf)
*/ */
void change_command(char *buf, int type) void change_command(char *buf, int type)
{ {
pointer -= type; current_command_index -= type;
//处理边界 //处理边界
if (pointer >= count_history) if (current_command_index < 0)
pointer--; current_command_index++;
if (pointer < 0) if (current_command_index >= count_history - 1)
pointer++; current_command_index = count_history - 2;
strcpy(buf, history_commands[pointer]); strcpy(buf, history_commands[current_command_index]);
printf("%s", buf); printf("%s", buf);
} }
/** /**
@ -155,7 +159,14 @@ int shell_readline(int fd, char *buf)
count = strlen(buf); count = strlen(buf);
} }
if (key == '\n') if (key == '\n')
{
if (count > 0 && current_command_index >= count_history)
{
memset(history_commands[current_command_index - 1], 0, sizeof(history_commands[current_command_index - 1]));
count_history--;
}
return count; return count;
}
if (key && key != 0x50 && key != 0xc8) if (key && key != 0x50 && key != 0xc8)
{ {
@ -170,9 +181,18 @@ int shell_readline(int fd, char *buf)
else else
{ {
buf[count++] = key; buf[count++] = key;
printf("%c", key); printf("%c", key);
} }
if (count > 0 && current_command_index >= count_history)
{
memset(history_commands[count_history], 0, sizeof(history_commands[count_history]));
strcpy(history_commands[count_history], buf);
}
else if (count > 0)
{
memset(history_commands[current_command_index], 0, sizeof(history_commands[current_command_index]));
strcpy(history_commands[current_command_index], buf);
}
} }
// 输入缓冲区满了之后,直接返回 // 输入缓冲区满了之后,直接返回