更新TAB自动补全的功能

This commit is contained in:
hqm 2021-09-29 22:02:06 +08:00
parent 96b9059fa3
commit 1b0323481e
3 changed files with 44 additions and 16 deletions

View File

@ -136,7 +136,8 @@ char* xcmd_display_get(void);
* @param {char*}
* @return
*/
void xcmd_display_set(const char *msg);
void xcmd_display_print(const char *msg);
void xcmd_display_write(const char* buf, uint16_t len);
/**
* @description:

View File

@ -53,7 +53,7 @@ struct
}history_list;
#endif
char display_line[XCMD_LINE_MAX_LENGTH]; /* 显示区的缓存 */
char display_line[XCMD_LINE_MAX_LENGTH+1]; /* 显示区的缓存 */
const char *prompt; /* 显示区的提示 */
uint16_t byte_num; /* 当前行的字符个数 */
uint16_t cursor; /* 光标所在位置 */
@ -339,16 +339,26 @@ void xcmd_print(const char *fmt, ...)
return;
}
void xcmd_display_set(const char *msg)
void xcmd_display_write(const char* buf, uint16_t len)
{
xcmd_display_clear();
uint16_t len = strlen(msg);
strncpy(g_xcmder.parser.display_line, msg, XCMD_LINE_MAX_LENGTH);
if(len > XCMD_LINE_MAX_LENGTH)
{
len = XCMD_LINE_MAX_LENGTH;
}
strncpy(g_xcmder.parser.display_line, buf, len);
xcmd_print(g_xcmder.parser.display_line);
g_xcmder.parser.byte_num = len;
g_xcmder.parser.cursor = len;
}
void xcmd_display_print(const char *msg)
{
xcmd_display_write(msg, strlen(msg));
}
char* xcmd_display_get(void)
{
char *line = g_xcmder.parser.display_line;

View File

@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2021-09-15 00:11:50
* @LastEditTime: 2021-09-17 23:18:12
* @LastEditTime: 2021-09-29 22:00:26
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/src/xcmd_default_keys.c
@ -10,6 +10,19 @@
#include "xcmd.h"
#include "xcmd_default_keys.h"
static int xcmd_str_match(const char* str1, const char* str2)
{
int i=0;
for(i=0; str1[i] && str2[i]; i++)
{
if(str1[i] != str2[i])
{
break;
}
}
return i;
}
static int xcmd_del_char(void *pv)
{
xcmd_display_delete_char();
@ -40,7 +53,7 @@ static int xcmd_history_dw(void *pv)
char *line = xcmd_history_prev();
if(line)
{
xcmd_display_set(line);
xcmd_display_print(line);
}
else
{
@ -54,19 +67,18 @@ static int xcmd_history_up(void *pv)
char *line = xcmd_history_next();
if(line)
{
xcmd_display_set(line);
xcmd_display_print(line);
}
return 0;
}
static int xcmd_auto_completion(void *pv)
{
xcmd_t *match_cmd = NULL;
xcmd_t *match_cmd_first = NULL;
uint16_t match_num = 0;
uint16_t match_subscript_min = 0;
xcmd_t *p = xcmd_cmdlist_get();
char *display_line = xcmd_display_get();
char display_backup[XCMD_LINE_MAX_LENGTH];
strncpy(display_backup, display_line, XCMD_LINE_MAX_LENGTH);
uint16_t cursor_pos = xcmd_display_cursor_get();
while(p)
{
@ -74,11 +86,12 @@ static int xcmd_auto_completion(void *pv)
{
if(match_num == 0)
{
match_cmd = p;
match_cmd_first = p;
match_subscript_min = strlen(p->name);
}
else if(match_num == 1)
{
xcmd_print("\r\n%-15s%-15s", match_cmd->name, p->name);
xcmd_print("\r\n%-15s%-15s", match_cmd_first->name, p->name);
}
else
{
@ -88,6 +101,11 @@ static int xcmd_auto_completion(void *pv)
xcmd_print("\r\n");
}
}
uint16_t subscript = xcmd_str_match(match_cmd_first->name, p->name);
if( subscript < match_subscript_min)
{
match_subscript_min = subscript;
}
match_num++;
}
p = p->next;
@ -95,17 +113,16 @@ static int xcmd_auto_completion(void *pv)
if(match_num == 1)
{
xcmd_display_set(match_cmd->name);
xcmd_display_print(match_cmd_first->name);
}
else if(match_num > 1)
{
xcmd_print("\r\n");
xcmd_display_set(display_backup);
xcmd_display_write(match_cmd_first->name, match_subscript_min);
}
return 0;
}
static xcmd_key_t default_keys[] =
{
{BACKSPACE, xcmd_del_char, "backspace", NULL},