Browse Source

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 <[email protected]>
wang904 2 years ago
parent
commit
4950d43e65
1 changed files with 40 additions and 20 deletions
  1. 40 20
      user/apps/shell/shell.c

+ 40 - 20
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);
+            }
         }
 
         // 输入缓冲区满了之后,直接返回