lettershell项目链接 https://github.com/NevermindZZT/letter-shell
往期精彩内容:
Lettershell之移植篇
Lettershell之命令注册
Lettershell之按键识别
Lettershell之输入缓冲区
Lettershell之命令解析
Lettershell之参数类型判断
自动补全(Auto-completion)广泛应用于各种应用程序中,特别是文本编辑器、集成开发环境(IDE)、搜索引擎以及各种输入框中。
自动补全的主要目的是提高用户的输入效率,通过预测用户可能输入的内容来减少键入的工作量。
lettershell实现自动补全十分简单,主要步骤如下:
- lettershell识别出Tab键,回调Tab按键处理函数:shellTab
- 如果输入缓冲区中没有数据,则输出所有候选命令
- 如果不存在和输入缓冲区匹配的命令,则直接返回,不做任何处理
- 如果存在和输入缓冲区匹配的命令,则输出这些候选命令,输出完毕后,向终端工具再次输出用户键入的命令,等待用户接着输入
按键识别机制,前文已有提及:Lettershell之按键识别,不再赘述。
命令匹配
在讲命令匹配前,我们考虑一种特殊情况:输入缓冲区中没有数据。
在Linux中,如果你在没有输入任何字符的情况下按下Tab键,终端会询问你:
root@vm:/mnt/hgfs/letter-shell-master/demo/x86-gcc/build#
Display all 2011 possibilities? (y or n)
这是因为,终端可能会产生大量的输出,使得屏幕难以阅读。
而在Lettershell中,没有这种交互式询问,直接输出所有注册的命令:
void shellTab(Shell *shell)
{ // ......
if (shell->parser.length == 0)
{
shellListAll(shell);
shellWritePrompt(shell, 1);
}
else if (shell->parser.length > 0)
{
// 命令匹配
}
// ......
}
接下来就是命令匹配的核心代码:
void shellTab(Shell *shell)
{
unsigned short maxMatch = shell->parser.bufferSize;
unsigned short lastMatchIndex = 0;
unsigned short matchNum = 0;
unsigned short length;
// ......
else if (shell->parser.length > 0){
shell->parser.buffer[shell->parser.length] = 0;
ShellCommand *base = (ShellCommand *)shell->commandList.base;
for (short i = 0; i < shell->commandList.count; i++){
if (shellCheckPermission(shell, &base[i]) == 0
&& shellStringCompare(shell->parser.buffer,(char *)shellGetCommandName(&base[i]))
== shell->parser.length){
if (matchNum != 0){
if (matchNum == 1){
shellWriteString(shell, "\r\n");
}
shellListItem(shell, &base[lastMatchIndex]);
length =
shellStringCompare((char *)shellGetCommandName(&base[lastMatchIndex]),
(char *)shellGetCommandName(&base[i]));
maxMatch = (maxMatch > length) ? length : maxMatch;
}
lastMatchIndex = i;
matchNum++;
}
}
// 无匹配
if (matchNum == 0){
return;
}
if (matchNum == 1) {
// 清空命令行输入
shellClearCommandLine(shell);
}
// 这里觉得没什么必要
if (matchNum != 0){
shell->parser.length =
shellStringCopy(shell->parser.buffer,
(char *)shellGetCommandName(&base[lastMatchIndex]));
}
// 多条命令匹配时,显示最后一条匹配的命令
if (matchNum > 1){
shellListItem(shell, &base[lastMatchIndex]);
shellWritePrompt(shell, 1);
shell->parser.length = maxMatch;
}
// 向终端输出用户键入的命令,等待用户接着输入
shell->parser.buffer[shell->parser.length] = 0;
shell->parser.cursor = shell->parser.length;
shellWriteString(shell, shell->parser.buffer);
}
// ......
}
在for循环中,主要进行如下操作:
- 输出上一次匹配的命令
- 计算前缀最大匹配长度(后期向终端工具再次输出用户键入的命令要用到)
循环结束后,进行如下操作:
- 如果没有匹配的命令,则不做任何操作
- 如果只有一个匹配的命令,则清空命令行输入,再次向终端输出用户键入的命令,等待用户接着输入
- 如果有N多条匹配的命令,则输出最后的第N条(前面的N-1条在循环中已经输出),向终端输出用户键入的命令,等待用户接着输入
总结
- Lettershell中的自动补全实现的基本思想十分简单,不过在实现过程中,还是需要注意
清空命令行输入
这种细节,否则终端显示会有问题,读者可以自己尝试。