Modify XCMD print function

This commit is contained in:
weidonglin 2021-09-13 20:10:34 +08:00
parent 6f90dcd5a0
commit 22dd57e08b
4 changed files with 58 additions and 22 deletions

View File

@ -1,6 +1,7 @@
#include "xcmd.h"
#include "malloc.h"
#define CMD_IS_ENDLINE(c) ((c == '\n') || (c == '\r'))
#define CMD_IS_PRINT(c) ((c >= 32) && (c <= 126))
@ -128,13 +129,13 @@ static uint32_t xcmd_bytes_encode(xcmder_t * cmder, uint8_t byte)
static void xcmd_display_update(xcmder_t *cmder)
{
char *line = xcmd_display_get(cmder);
xcmd_print_str(cmder, "\r->");
xcmd_print_str(cmder, line);
xcmd_print_str(cmder, "\r->");
xcmd_print(cmder, "\r->");
xcmd_print(cmder, line);
xcmd_print(cmder, "\r->");
/* move cursor */
for(uint16_t i = 0; i<cmder->parser.cursor; i++)
{
xcmd_print_str(cmder, "\x1B\x5B\x43");
xcmd_print(cmder, "\x1B\x5B\x43");
}
}
@ -157,12 +158,12 @@ static char* xcmd_line_end(xcmder_t *cmder)
}
cmder->parser.byte_num = 0;
cmder->parser.cursor = 0;
xcmd_print_str(cmder, "\r\n");
xcmd_print(cmder, "\r\n");
xcmd_history_reset(cmder);
}
else
{
xcmd_print_str(cmder, "\r\n->");
xcmd_print(cmder, "\r\n->");
}
return ret;
}
@ -191,11 +192,26 @@ static char* xcmd_parser(xcmder_t * cmder, uint8_t byte)
}
void xcmd_print_str(xcmder_t * cmder, char* str)
{
{
while(*str)
{
cmder->io.put_c(*str++);
}
return;
}
void xcmd_print(xcmder_t * cmder, const char *fmt, ...)
{
char ucstring[256] = {0};
unsigned short wdatalen;
va_list arg;
va_start(arg, fmt);
wdatalen = vsprintf(ucstring, fmt, arg);
va_end(arg);
xcmd_print_str(cmder, ucstring);
return;
}
char* xcmd_display_get(xcmder_t *cmder)
@ -210,12 +226,12 @@ void xcmd_display_clear(xcmder_t *cmder)
uint16_t len = strlen(line);
if(len)
{
xcmd_print_str(cmder, "\r->");
xcmd_print(cmder, "\r->");
for(uint16_t i=0; i<len; i++)
{
cmder->io.put_c(' ');
}
xcmd_print_str(cmder, "\r->");
xcmd_print(cmder, "\r->");
cmder->parser.byte_num = 0;
cmder->parser.cursor = 0;
line[0] = '\0';

View File

@ -7,6 +7,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <stdarg.h>
typedef void(*cmd_func_t)(int argv, char* argc[]);
typedef int(*cmd_key_func_t)(void *data);
@ -190,7 +191,8 @@ uint8_t xcmd_exec(xcmder_t *cmder, char *str);
* @param {char*} str
* @return
*/
void xcmd_print_str(xcmder_t * cmder, char* str);
void xcmd_print(xcmder_t * cmder, const char *fmt, ...);
/**
* @description:

View File

@ -5,7 +5,7 @@ static xcmder_t *g_cmder = NULL;
static void cmd_clear(int argc, char* argv[])
{
printf("\033c");
xcmd_print(g_cmder, "\033c");
}
static void cmd_help(int argc, char* argv[])
@ -13,7 +13,7 @@ static void cmd_help(int argc, char* argv[])
xcmd_t *p = g_cmder->cmd_list.next;
while(p)
{
printf("%-20s %s\r\n",p->name, p->help);
xcmd_print(g_cmder, "%-20s %s\r\n",p->name, p->help);
p = p->next;
}
}
@ -30,7 +30,7 @@ static void cmd_logo(int argc, char* argv[])
( \\/ )/ __)( \\/ )( _ \\ \n\
) (( (__ ) ( )(_) )\n\
(_/\\_)\\___)(_/\\/\\_)(____/\n ";
printf("%s", log);
xcmd_print(g_cmder, "%s", log);
}
static xcmd_t cmds[] =

View File

@ -24,7 +24,7 @@ static int xcmd_cursor_left(void *pv)
if(cmder->parser.cursor > 0)
{
cmder->parser.cursor--;
xcmd_print_str(cmder, STR_LEFT);
xcmd_print(cmder, STR_LEFT);
}
return 0;
}
@ -35,7 +35,7 @@ static int xcmd_cursor_right(void *pv)
if(cmder->parser.cursor < cmder->parser.byte_num)
{
cmder->parser.cursor++;
xcmd_print_str(cmder, STR_RIGHT);
xcmd_print(cmder, STR_RIGHT);
}
return 0;
}
@ -52,7 +52,7 @@ static int xcmd_history_dw(void *pv)
if (len)
{
strncpy(display_line, line, cmder->parser.line_len);
xcmd_print_str(cmder, line);
xcmd_print(cmder, line);
cmder->parser.byte_num = len;
cmder->parser.cursor = len;
}
@ -76,7 +76,7 @@ static int xcmd_history_up(void *pv)
if (len)
{
strncpy(display_line, line, cmder->parser.line_len);
xcmd_print_str(cmder, line);
xcmd_print(cmder, line);
cmder->parser.byte_num = len;
cmder->parser.cursor = len;
}
@ -84,13 +84,31 @@ static int xcmd_history_up(void *pv)
return 0;
}
static int xcmd_auto_completion(void *pv)
{
xcmder_t *cmder = (xcmder_t*)pv;
char *pdat = xcmd_display_get(cmder);
xcmd_t *p = cmder->cmd_list.next;
while(p)
{
printf("%-20s %s\r\n",p->name, p->help);
p = p->next;
}
return 0;
}
static xcmd_key_t default_keys[] =
{
{L_DELETE, xcmd_del_char, NULL},
{LEFT, xcmd_cursor_left, NULL},
{RIGHT, xcmd_cursor_right, NULL},
{DW, xcmd_history_dw, NULL},
{UP, xcmd_history_up, NULL},
{BACKSPACE, xcmd_del_char, NULL},
{L_DELETE, xcmd_del_char, NULL},
{LEFT, xcmd_cursor_left, NULL},
{RIGHT, xcmd_cursor_right, NULL},
{DW, xcmd_history_dw, NULL},
{UP, xcmd_history_up, NULL},
{TAB, xcmd_auto_completion, NULL},
};
void default_keys_init(xcmder_t *cmder)