From 492c9eee102a71f4b4432b5bb6d0f7cfcdd86276 Mon Sep 17 00:00:00 2001 From: hqm <13720409820@163.com> Date: Wed, 20 Oct 2021 16:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9xcmd=5Fdisplay=5Fprint?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0xcmd=5Fput=5Fstr?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/xcmd.h | 8 ++++---- src/xcmd.c | 42 +++++++++++++++++++++++++---------------- src/xcmd_default_keys.c | 5 +++-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/inc/xcmd.h b/inc/xcmd.h index d283365..55a86f5 100755 --- a/inc/xcmd.h +++ b/inc/xcmd.h @@ -14,8 +14,8 @@ extern "C" { #endif -typedef int(*cmd_func_t)(int argv, char* argc[]); -typedef int(*cmd_key_func_t)(void *data); +typedef int(*cmd_func_t)(int, char**); +typedef int(*cmd_key_func_t)(void *); typedef struct __cmd { @@ -108,7 +108,7 @@ int xcmd_exec(char *str); * @return 无 */ void xcmd_print(const char *fmt, ...); - +void xcmd_put_str(const char *str); /** * @description: 向显示器插入一个字符 @@ -150,7 +150,7 @@ char* xcmd_display_get(void); * @param {char*} 要现实的内容 * @return 无 */ -void xcmd_display_print(const char *msg); +void xcmd_display_print(const char *fmt, ...); void xcmd_display_write(const char* buf, uint16_t len); /** diff --git a/src/xcmd.c b/src/xcmd.c index c2d395d..efa15a6 100755 --- a/src/xcmd.c +++ b/src/xcmd.c @@ -206,8 +206,10 @@ static void xcmd_key_match(char* key) { if(strcmp(key, p->key) == 0) { - p->func(&g_xcmder); - break; + if(p->func(&g_xcmder) == 0) + { + break; + } } p = p->next; } @@ -313,6 +315,14 @@ static void xcmd_parser(uint8_t byte) } } +void xcmd_put_str(const char *str) +{ + for(uint16_t i=0; str[i]; i++) + { + g_xcmder.io.put_c(str[i]); + }; +} + void xcmd_print(const char *fmt, ...) { char ucstring[XCMD_PRINT_BUF_MAX_LENGTH] = {0}; @@ -320,12 +330,7 @@ void xcmd_print(const char *fmt, ...) va_start(arg, fmt); vsnprintf(ucstring, XCMD_PRINT_BUF_MAX_LENGTH, fmt, arg); va_end(arg); - - for(uint16_t i=0; ucstring[i]; i++) - { - g_xcmder.io.put_c(ucstring[i]); - }; - return; + xcmd_put_str(ucstring); } void xcmd_display_write(const char* buf, uint16_t len) @@ -337,18 +342,21 @@ void xcmd_display_write(const char* buf, uint16_t len) } memcpy(g_xcmder.parser.display_line, buf, len); g_xcmder.parser.display_line[len] = '\0'; - xcmd_print(g_xcmder.parser.display_line); + xcmd_put_str(g_xcmder.parser.display_line); g_xcmder.parser.byte_num = len; g_xcmder.parser.cursor = len; } -void xcmd_display_print(const char *msg) +void xcmd_display_print(const char *fmt, ...) { - xcmd_display_write(msg, strlen(msg)); + char ucstring[XCMD_PRINT_BUF_MAX_LENGTH] = {0}; + va_list arg; + va_start(arg, fmt); + vsnprintf(ucstring, XCMD_PRINT_BUF_MAX_LENGTH, fmt, arg); + va_end(arg); + xcmd_display_write(ucstring, strlen(ucstring)); } - - char* xcmd_display_get(void) { char *line = g_xcmder.parser.display_line; @@ -358,11 +366,13 @@ char* xcmd_display_get(void) void xcmd_display_clear(void) { char *line = xcmd_display_get(); - xcmd_print(DL(0)); + xcmd_put_str("\x1B[0M"); #ifndef XCMD_DEFAULT_PROMPT_CLOLR - xcmd_print("%s", xcmd_get_prompt()); + xcmd_put_str(xcmd_get_prompt()); #else - xcmd_print(XCMD_DEFAULT_PROMPT_CLOLR "%s" TX_DEF, xcmd_get_prompt()); + xcmd_put_str(XCMD_DEFAULT_PROMPT_CLOLR); + xcmd_put_str(xcmd_get_prompt()); + xcmd_put_str(TX_DEF); #endif g_xcmder.parser.byte_num = 0; g_xcmder.parser.cursor = 0; diff --git a/src/xcmd_default_keys.c b/src/xcmd_default_keys.c index fc29341..3c330ff 100755 --- a/src/xcmd_default_keys.c +++ b/src/xcmd_default_keys.c @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-09-15 00:11:50 - * @LastEditTime: 2021-10-11 21:38:26 + * @LastEditTime: 2021-10-12 14:58:23 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /xcmd/src/xcmd_default_keys.c @@ -133,7 +133,7 @@ static int xcmd_auto_completion(void *pv) if(match_num == 1) { - xcmd_display_print(match_cmd_first->name); + xcmd_display_print("%s ", match_cmd_first->name); } else if(match_num > 1) { @@ -146,6 +146,7 @@ static int xcmd_auto_completion(void *pv) static xcmd_key_t default_keys[] = { {KEY_CTR_M, xcmd_enter, "enter", NULL}, + {KEY_CTR_J, xcmd_enter, "enter", NULL}, {KEY_CTR_H, xcmd_del_char, "backspace", NULL}, {KEY_BACKSPACE, xcmd_del_char, "delete", NULL}, {KEY_LEFT, xcmd_cursor_left, "left", NULL},