Skip to content

Commit

Permalink
pl_readline更新
Browse files Browse the repository at this point in the history
  • Loading branch information
min0911Y committed Dec 14, 2024
1 parent a63dd12 commit d8cc74f
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 276 deletions.
77 changes: 39 additions & 38 deletions include/pl_readline.h
Original file line number Diff line number Diff line change
@@ -1,75 +1,76 @@
#pragma once
#include <data-structure.h>
#include <libc.h>

#define PL_READLINE_KEY_UP 0xff00
#define PL_READLINE_KEY_DOWN 0xff01
#define PL_READLINE_KEY_LEFT 0xff02
#define PL_READLINE_KEY_RIGHT 0xff03
#define PL_READLINE_KEY_HOME 0xff04
#define PL_READLINE_KEY_END 0xff05
#define PL_READLINE_KEY_PAGE_UP 0xff06
#define PL_READLINE_KEY_PAGE_DOWN 0xff07
#define PL_READLINE_KEY_ENTER '\n'
#define PL_READLINE_KEY_TAB '\t'
#define PL_READLINE_KEY_CTRL_A 0x01
#define PL_READLINE_KEY_CTRL_C 0x03
#define PL_READLINE_KEY_BACKSPACE '\b'
#define _SELF pl_readline_t self
#define PL_READLINE_SUCCESS 0
#define PL_READLINE_FAILED -1
#define PL_READLINE_NOT_FINISHED 1

#define _self pl_readline_t self
#define PL_READLINE_SUCCESS 0
#define PL_READLINE_FAILED -1
#define PL_READLINE_NOT_FINISHED 1
#define PL_READLINE_DEFAULT_BUFFER_LEN 32

typedef struct pl_readline_word {
char *word; // 词组
/**
如果first为true,
这个word必须在第一个参数的位置的时候才能得到补全
如abc 则必须输入"ab"然后按tab,才会有可能有"abc"
如果是“qwe ab”则不会补全"qwe abc",除非first为false.
*/
如果first为true,
这个word必须在第一个参数的位置的时候才能得到补全
如abc 则必须输入"ab"然后按tab,才会有可能有"abc"
如果是“qwe ab”则不会补全"qwe abc",除非first为false.
*/
bool first;

char sep; // 分隔符
char sep; // 分隔符
} pl_readline_word;

typedef struct pl_readline_words {
int len; // 词组数量
int max_len; // 词组最大数量
size_t len; // 词组数量
size_t max_len; // 词组最大数量
pl_readline_word *words; // 词组列表
} *pl_readline_words_t;

typedef struct pl_readline {
int (*pl_readline_hal_getch)(); // 输入函数
int (*pl_readline_hal_getch)(void); // 输入函数
void (*pl_readline_hal_putch)(int ch); // 输出函数
void (*pl_readline_hal_flush)(); // 刷新函数
void (*pl_readline_hal_flush)(void); // 刷新函数
void (*pl_readline_get_words)(char *buf,
pl_readline_words_t words); // 获取词组列表
list_t history; // 历史记录列表
} *pl_readline_t;
typedef struct pl_readline_runtime {
char *buffer; // 输入缓冲区
int p; // 输入缓冲区指针
int length; // 输入缓冲区长度(已经输入的字符数)
char *buffer; // 输入缓冲区
char *input_buf; // 输入缓冲区(补全的前缀)
size_t ptr; // 输入缓冲区指针
size_t input_ptr; // 输入缓冲区(补全的前缀)指针
size_t maxlen; // 缓冲区最大长度
size_t length; // 输入缓冲区长度(已经输入的字符数)
list_t history; // 历史记录列表
int history_idx; // 历史记录指针
char *prompt; // 提示符
size_t maxlen; // 缓冲区最大长度
char *input_buf; // 输入缓冲区(补全的前缀)
int input_buf_ptr; // 输入缓冲区(补全的前缀)指针
bool intellisense_mode; // 智能补全模式
char *intellisense_word; // 智能补全词组
} pl_readline_runtime;
} *pl_readline_t;

pl_readline_words_t pl_readline_word_maker_init();
pl_readline_t pl_readline_init(int (*pl_readline_hal_getch)(),
pl_readline_words_t pl_readline_word_maker_init(void);
pl_readline_t pl_readline_init(int (*pl_readline_hal_getch)(void),
void (*pl_readline_hal_putch)(int ch),
void (*pl_readline_hal_flush)(),
void (*pl_readline_hal_flush)(void),
void (*pl_readline_get_words)(char *buf, pl_readline_words_t words));
int pl_readline(_SELF, char *prompt, char *buffer, size_t maxlen);
pl_readline_word pl_readline_intellisense(_SELF, pl_readline_runtime *rt,
pl_readline_words_t words);
void pl_readline_insert_char_and_view(_SELF, char ch, pl_readline_runtime *rt);
const char *pl_readline(_self, char *prompt);
pl_readline_word pl_readline_intellisense(_self, pl_readline_words_t words);
void pl_readline_insert_char_and_view(_self, char ch);
void pl_readline_insert_char(char *str, char ch, int idx);
int pl_readline_word_maker_add(char *word, pl_readline_words_t words, bool is_first, char sep);
void pl_readline_print(_SELF, char *str);
void pl_readline_intellisense_insert(_SELF, pl_readline_runtime *rt, pl_readline_word words);
void pl_readline_print(_self, char *str);
void pl_readline_intellisense_insert(_self, pl_readline_word words);
void pl_readline_word_maker_destroy(pl_readline_words_t words);
void pl_readline_next_line(_SELF, pl_readline_runtime *rt);
int pl_readline_handle_key(_SELF, int ch, pl_readline_runtime *rt);
void pl_readline_uninit(_SELF);
void pl_readline_next_line(_self);
int pl_readline_handle_key(_self, int ch);
void pl_readline_uninit(_self);
2 changes: 1 addition & 1 deletion src/kernel/task/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void shell() {
char prompt[256];
while (true) {
sprintf(prompt, "%s# ", path);
pl_readline(n, prompt, line, 255);
cstr line = pl_readline(n, prompt);
shell_exec(path, line);
}
}
Expand Down
Loading

0 comments on commit d8cc74f

Please sign in to comment.