Просмотр исходного кода

Patch shell cursor (#59)

* 调整代码减少bug

* 修复换行光标bug

Co-authored-by: longjin <[email protected]>
DaJiYuQia 2 лет назад
Родитель
Сommit
a9c5b3e45c
3 измененных файлов с 34 добавлено и 12 удалено
  1. 0 1
      kernel/filesystem/VFS/VFS.c
  2. 3 2
      kernel/lib/libUI/textui.c
  3. 31 9
      user/apps/shell/shell.c

+ 0 - 1
kernel/filesystem/VFS/VFS.c

@@ -398,7 +398,6 @@ uint64_t do_open(const char *filename, int flags)
 
     // 寻找文件
     struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
-
     if (dentry == NULL && flags & O_CREAT)
     {
         // 先找到倒数第二级目录

+ 3 - 2
kernel/lib/libUI/textui.c

@@ -181,7 +181,8 @@ static int __textui_putchar_window(struct textui_window_t *window, uint16_t char
         ++vline->index;
         textui_refresh_characters(window, window->vline_operating, vline->index - 1, 1);
         // 换行
-        if (vline->index >= window->chars_per_line)
+        // 加入光标后,因为会识别光标,所以需超过该行最大字符数才能创建新行
+        if (vline->index > window->chars_per_line)
         {
             __textui_new_line(window, window->vline_operating);
         }
@@ -295,7 +296,7 @@ int textui_putchar(uint16_t character, uint32_t FRcolor, uint32_t BKcolor)
 /**
  * @brief 初始化text ui框架
  *
- * @return int
+ * @return int 
  */
 int textui_init()
 {

+ 31 - 9
user/apps/shell/shell.c

@@ -1,12 +1,13 @@
-#include <libc/unistd.h>
-#include <libc/stdio.h>
+#include "cmd.h"
+#include <libKeyboard/keyboard.h>
 #include <libc/fcntl.h>
+#include <libc/printf.h>
+#include <libc/stddef.h>
+#include <libc/stdio.h>
 #include <libc/stdlib.h>
-#include <libKeyboard/keyboard.h>
 #include <libc/string.h>
-#include <libc/stddef.h>
 #include <libc/sys/stat.h>
-#include "cmd.h"
+#include <libc/unistd.h>
 
 #define pause_cpu() asm volatile("pause\n\t");
 #define MEM_HISTORY 1024
@@ -50,11 +51,14 @@ void main_loop(int kb_fd)
     {
         int argc = 0;
         char **argv;
-
+        
         printf("[DragonOS] %s # ", shell_current_path);
-
+        
         memset(input_buffer, 0, INPUT_BUFFER_SIZE);
 
+        //添加初始光标
+        put_string(" ", COLOR_BLACK, COLOR_WHITE);
+
         // 循环读取每一行到buffer
         count_history++;
         int count = shell_readline(kb_fd, input_buffer);
@@ -125,6 +129,7 @@ void change_command(char *buf, int type)
         current_command_index = count_history - 2;
     strcpy(buf, history_commands[current_command_index]);
     printf("%s", buf);
+    put_string(" ", COLOR_BLACK, COLOR_WHITE);
 }
 /**
  * @brief 循环读取每一行
@@ -143,6 +148,8 @@ int shell_readline(int fd, char *buf)
         //向上方向键
         if (count_history != 0 && key == 0xc8)
         {
+            // put_string(" ", COLOR_WHITE, COLOR_BLACK);
+            printf("%c", '\b');
             clear_command(count, buf);
             count = 0;
             //向历史
@@ -152,6 +159,8 @@ int shell_readline(int fd, char *buf)
         //向下方向键
         if (count_history != 0 && key == 0x50)
         {
+            // put_string(" ", COLOR_WHITE, COLOR_BLACK);
+            printf("%c", '\b');
             clear_command(count, buf);
             count = 0;
             //向现在
@@ -162,9 +171,11 @@ int shell_readline(int fd, char *buf)
         {
             if (count > 0 && current_command_index >= count_history)
             {
-                memset(history_commands[current_command_index - 1], 0, sizeof(history_commands[current_command_index - 1]));
+                memset(history_commands[current_command_index - 1], 0,
+                       sizeof(history_commands[current_command_index - 1]));
                 count_history--;
             }
+            printf("%c", '\b');
             return count;
         }
 
@@ -174,14 +185,22 @@ int shell_readline(int fd, char *buf)
             {
                 if (count > 0)
                 {
-                    buf[--count] = 0;
+                    // 回退去除先前光标
                     printf("%c", '\b');
+                    // 去除字符
+                    printf("%c", '\b');
+                    buf[--count] = 0;
+                    // 在最后一个字符处加光标
+                    put_string(" ", COLOR_BLACK, COLOR_WHITE);
                 }
             }
             else
             {
+                printf("%c", '\b');
                 buf[count++] = key;
                 printf("%c", key);
+                // 在最后一个字符处加光标
+                put_string(" ", COLOR_BLACK, COLOR_WHITE);
             }
             if (count > 0 && current_command_index >= count_history)
             {
@@ -197,7 +216,10 @@ int shell_readline(int fd, char *buf)
 
         // 输入缓冲区满了之后,直接返回
         if (count >= INPUT_BUFFER_SIZE - 1)
+        {
+            printf("%c", '\b');
             return count;
+        }
 
         pause_cpu();
     }