Skip to content

Commit

Permalink
新增 双击tab快速帮助
Browse files Browse the repository at this point in the history
  • Loading branch information
NevermindZZT committed Jun 12, 2021
1 parent fe95543 commit 6b79f9a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
| SHELL_PARAMETER_MAX_NUMBER | shell命令参数最大数量 |
| SHELL_HISTORY_MAX_NUMBER | 历史命令记录数量 |
| SHELL_DOUBLE_CLICK_TIME | 双击间隔(ms) |
| SHELL_QUICK_HELP | 快速帮助 |
| SHELL_MAX_NUMBER | 管理的最大shell数量 |
| SHELL_GET_TICK() | 获取系统时间(ms) |
| SHELL_USING_LOCK | 是否使用锁 |
Expand Down
9 changes: 8 additions & 1 deletion demo/x86-gcc/shell_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define __SHELL_CFG_H__

#include "stdlib.h"
unsigned int userGetTick();

/**
* @brief 是否使用默认shell任务while循环,使能宏`SHELL_USING_TASK`后此宏有意义
Expand Down Expand Up @@ -101,6 +102,12 @@
*/
#define SHELL_DOUBLE_CLICK_TIME 200

/**
* @brief 快速帮助
* 作用于双击tab的场景,当使能此宏时,双击tab不会对命令进行help补全,而是直接显示对应命令的帮助信息
*/
#define SHELL_QUICK_HELP 1

/**
* @brief 管理的最大shell数量
*/
Expand All @@ -124,7 +131,7 @@
* 定义此宏为获取系统Tick,如`HAL_GetTick()`
* @note 此宏不定义时无法使用双击tab补全命令help,无法使用shell超时锁定
*/
#define SHELL_GET_TICK() 0
#define SHELL_GET_TICK() userGetTick()

/**
* @brief 使用锁
Expand Down
13 changes: 13 additions & 0 deletions demo/x86-gcc/shell_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <sys/time.h>

Shell shell;
char shellBuffer[512];
Expand All @@ -28,6 +29,18 @@ Log log = {
.level = LOG_DEBUG
};

/**
* @brief 获取系统tick
*
* @return unsigned int 系统tick
*/
unsigned int userGetTick()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000) & 0x7FFFFFFF;
}

/**
* @brief 日志写函数实现
*
Expand Down
50 changes: 34 additions & 16 deletions src/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ ShellCommand* shellSeekCommand(Shell *shell,
const char *cmd,
ShellCommand *base,
unsigned short compareLength);
static void shellWriteCommandHelp(Shell *shell, char *cmd);

/**
* @brief shell 初始化
Expand Down Expand Up @@ -1543,6 +1544,12 @@ void shellTab(Shell *shell)
&& shell->status.tabFlag
&& SHELL_GET_TICK() - shell->info.activeTime < SHELL_DOUBLE_CLICK_TIME)
{
#if SHELL_QUICK_HELP == 1
shellWriteString(shell, "\r\n");
shellWriteCommandHelp(shell, shell->parser.buffer);
shellWritePrompt(shell, 1);
shellWriteString(shell, shell->parser.buffer);
#else
shellClearCommandLine(shell);
for (short i = shell->parser.length; i >= 0; i--)
{
Expand All @@ -1553,6 +1560,7 @@ void shellTab(Shell *shell)
shell->parser.length += 5;
shell->parser.cursor = shell->parser.length;
shellWriteString(shell, shell->parser.buffer);
#endif
}
else
{
Expand Down Expand Up @@ -1614,6 +1622,31 @@ SHELL_EXPORT_KEY(SHELL_CMD_PERMISSION(0)|SHELL_CMD_ENABLE_UNCHECKED,
0x0D0A0000, shellEnter, enter);
#endif

/**
* @brief shell 写命令帮助信息
*
* @param shell shell对象
* @param cmd 命令字符串
*/
static void shellWriteCommandHelp(Shell *shell, char *cmd)
{
ShellCommand *command = shellSeekCommand(shell,
cmd,
shell->commandList.base,
0);
if (command)
{
shellWriteString(shell, shellText[SHELL_TEXT_HELP_HEADER]);
shellWriteString(shell, shellGetCommandName(command));
shellWriteString(shell, "\r\n");
shellWriteString(shell, shellGetCommandDesc(command));
shellWriteString(shell, "\r\n");
}
else
{
shellWriteString(shell, shellText[SHELL_TEXT_CMD_NOT_FOUND]);
}
}

/**
* @brief shell help
Expand All @@ -1631,22 +1664,7 @@ void shellHelp(int argc, char *argv[])
}
else if (argc > 1)
{
ShellCommand *command = shellSeekCommand(shell,
argv[1],
shell->commandList.base,
0);
if (command)
{
shellWriteString(shell, shellText[SHELL_TEXT_HELP_HEADER]);
shellWriteString(shell, shellGetCommandName(command));
shellWriteString(shell, "\r\n");
shellWriteString(shell, shellGetCommandDesc(command));
shellWriteString(shell, "\r\n");
}
else
{
shellWriteString(shell, shellText[SHELL_TEXT_CMD_NOT_FOUND]);
}
shellWriteCommandHelp(shell, argv[1]);
}
}
SHELL_EXPORT_CMD(
Expand Down
7 changes: 7 additions & 0 deletions src/shell_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
*/
#define SHELL_DOUBLE_CLICK_TIME 200

/**
* @brief 快速帮助
* 作用于双击tab的场景,当使能此宏时,双击tab不会对命令进行help补全,而是直接显示对应命令的帮助信息
*/
#define SHELL_QUICK_HELP 1


/**
* @brief 管理的最大shell数量
*/
Expand Down

0 comments on commit 6b79f9a

Please sign in to comment.