From 4950d43e65566d295bb9e5ed7198b4f272601972 Mon Sep 17 00:00:00 2001 From: wang904 <56588029+wang904@users.noreply.github.com> Date: Thu, 18 Aug 2022 21:38:11 +0800 Subject: [PATCH] Patch add shell history(update for issue #23) (#24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- user/apps/shell/shell.c | 60 +++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index fcd0ac9d..0cb915df 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -21,11 +21,13 @@ int shell_readline(int fd, char *buf); void print_ascii_logo(); extern char *shell_current_path; -//保存的历史命令 +//保存的历史命令(瞬时更改) char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; +//真正的历史命令 +char real_history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; int count_history; //现在对应的命令 -int pointer; +int current_command_index; /** * @brief shell主循环 * @@ -34,7 +36,7 @@ int pointer; void main_loop(int kb_fd) { count_history = 0; - pointer = 1; + current_command_index = 0; unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; // 初始化当前工作目录的路径 @@ -54,23 +56,27 @@ void main_loop(int kb_fd) memset(input_buffer, 0, INPUT_BUFFER_SIZE); // 循环读取每一行到buffer + count_history++; 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) { char command_origin[strlen(input_buffer)]; strcpy(command_origin, input_buffer); int cmd_num = parse_command(input_buffer, &argc, &argv); - printf("\n"); 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); - } } else printf("\n"); @@ -100,9 +106,7 @@ int main() void clear_command(int count, char *buf) { for (int i = 0; i < count; i++) - { printf("%c", '\b'); - } memset(buf, 0, sizeof(buf)); } /** @@ -113,13 +117,13 @@ void clear_command(int count, char *buf) */ void change_command(char *buf, int type) { - pointer -= type; + current_command_index -= type; //处理边界 - if (pointer >= count_history) - pointer--; - if (pointer < 0) - pointer++; - strcpy(buf, history_commands[pointer]); + if (current_command_index < 0) + current_command_index++; + if (current_command_index >= count_history - 1) + current_command_index = count_history - 2; + strcpy(buf, history_commands[current_command_index]); printf("%s", buf); } /** @@ -155,7 +159,14 @@ int shell_readline(int fd, char *buf) count = strlen(buf); } 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; + } if (key && key != 0x50 && key != 0xc8) { @@ -170,9 +181,18 @@ int shell_readline(int fd, char *buf) else { buf[count++] = 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); + } } // 输入缓冲区满了之后,直接返回