From a9c5b3e45c1724d317fddfb22e406d2d09327a54 Mon Sep 17 00:00:00 2001 From: DaJiYuQia <88259094+DaJiYuQia@users.noreply.github.com> Date: Tue, 18 Oct 2022 20:38:34 +0800 Subject: [PATCH] Patch shell cursor (#59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 调整代码减少bug * 修复换行光标bug Co-authored-by: longjin --- kernel/filesystem/VFS/VFS.c | 1 - kernel/lib/libUI/textui.c | 5 ++-- user/apps/shell/shell.c | 46 +++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/kernel/filesystem/VFS/VFS.c b/kernel/filesystem/VFS/VFS.c index 24e2a9e6..010dd9c0 100644 --- a/kernel/filesystem/VFS/VFS.c +++ b/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) { // 先找到倒数第二级目录 diff --git a/kernel/lib/libUI/textui.c b/kernel/lib/libUI/textui.c index df5fa2cd..13224268 100644 --- a/kernel/lib/libUI/textui.c +++ b/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() { diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c index fb1c4c60..ab9a8fba 100644 --- a/user/apps/shell/shell.c +++ b/user/apps/shell/shell.c @@ -1,12 +1,13 @@ -#include -#include -#include -#include -#include -#include -#include -#include #include "cmd.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #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(); }