@pnpm/tabtab (英语)
执行自定义命令行的节点软件包 <tab><tab> 任何项目的补全
系统指挥,用于巴什,兹什,和鱼壳.
使用与 npm 相同的技术实现(其完成率相当) 真棒)依靠贝壳脚本桥来完成实际完成从 节点之地.

警告/ 中断更改
- 不支持窗口
- 缓存已删除
- 现在只支持节点
> 7.10.1,用于支持节点的上一个版本 6 确定使用标签2.2.x
目录
3.0.0版本的目标
简化一切,大修,从零开始重写.
功能性强,抽象性较弱,文献记录更清晰,测试覆盖面好, 支持节点 10 没有bayl.
上至相依性,更能调试,更能测试.
应该继续支持出击,zsh和鱼 但出击是重点 阿尔法版本.
没有二进制文件了,只是一个库(仍在与我自己辩论)
此重写的目标有两重:
安装
pnpm add @pnpm/tabtab
使用量
写入完成是一个两步的过程:安装和日志. 标签 仅此而已.
以下是一个使用 最小数 以分析参数.
#! /usr/bin/env node
const tabtab = require('@pnpm/tabtab');
const opts = require('minimist')(process.argv.slice(2), {
string: ['foo', 'bar'],
boolean: ['help', 'version', 'loglevel']
});
const args = opts._;
const completion = env => {
if (!env.complete) return;
// Write your completions there
if (env.prev === 'foo') {
return tabtab.log(['is', 'this', 'the', 'real', 'life']);
}
if (env.prev === 'bar') {
return tabtab.log(['is', 'this', 'just', 'fantasy']);
}
if (env.prev === '--loglevel') {
return tabtab.log(['error', 'warn', 'info', 'notice', 'verbose']);
}
return tabtab.log([
'--help',
'--version',
'--loglevel',
'foo',
'bar',
'install-completion',
'completion',
'someCommand:someCommand is some kind of command with a description',
{
name: 'someOtherCommand:hey',
description: 'You must add a description for items with ":" in them'
},
'anotherOne'
]);
};
const run = async () => {
const cmd = args[0];
// Write your CLI there
// Here we install for the program `tabtab-test` (this file), with
// completer being the same program. Sometimes, you want to complete
// another program that's where the `completer` option might come handy.
if (cmd === 'install-completion') {
await tabtab
.install({
name: 'tabtab-test',
completer: 'tabtab-test'
})
.catch(err => console.error('INSTALL ERROR', err));
return;
}
if (cmd === 'uninstall-completion') {
// Here we uninstall for the program `tabtab-test` (this file).
await tabtab
.uninstall({
name: 'tabtab-test'
})
.catch(err => console.error('UNINSTALL ERROR', err));
return;
}
// The completion command is added automatically by tabtab when the program
// is completed.
if (cmd === 'completion') {
const env = tabtab.parseEnv(process.env);
return completion(env);
}
};
run();
请参看 实例/tabtab测试-完成 一个软件包 实例。 以下的用法文件就是以此为基础的.
1. 安装完成
要完成给定的程序或软件包,您必须启用
在您或用户的系统中完成。 这是通过呼叫 tabtab.install()
通常位于 program install-completion 命令或类似的东西.
// Here we install for the program `tabtab-test`, with completer being the same
// program. Sometimes, you want to complete another program that's where the
// `completer` option might come handy.
tabtab.install({
name: 'tabtab-test',
completer: 'tabtab-test'
})
.then(() => console.log('Completion installed'))
.catch(err => console.error(err))
方法还原一个承诺,所以 await / async 使用是可能的。 它需要一个
options 参数,使用:
name: 要完成的程序completer:完成的程序(可以是同一程序).
tabtab.install() 将询问用户使用哪个 SHELL, 并选择路径
写给。 这将增加一条新行到其中之一 ~/.bashrc, (中文(简体) ), ~/.zshrc 或者说
~/.config/fish/config.fish 文件到源 tatab 补全脚本.
只有一行会被添加,即使它被多次调用.
2. 日志完成情况
一旦完成启用并激活,您就可以为
程序(在此示例中) tabtab-test) (中文(简体) ). 简而言之,增加的完成是:
和日志输出一样简单 stdout,具有一些特殊性(即:
Bash, 和描述), 但这是由 tabtab.log()。 。 。 .
tabtab.log([
'--help',
'--version',
'command'
'command-two'
]);
这是添加完成数的最简单方式. 您也可以使用对象,
而不是简单的字符串,与 { name, description } 属性
为每个完成项添加描述, 用于支持它们的弹壳
(如Zsh或鱼. 或者用更简单的 name:description 形式.
tabtab.log([
{ name: 'command', description: 'Description for command' },
'command-two:Description for command-two'
]);
这个 { name, description } 方法在您完成时更为可取
带有 : 在他们身上.
请注意,你可以打电话 tabtab.log() 多次,如果你喜欢这样做,它
仅按顺序记录到控制台.
文件系统完成
如果您有一个参数可以期望某个文件的路径, 您可以允许 贝壳使用其本地自动补全。 这样可以省去写作的功用 自定义文件系统自动完成逻辑 。 况且 本地的自动完成能力更好 处理诸如 dircolors 或隐藏文件之类的事项.
要启动文件系统补全, 使用 tabtab.logFiles() 没有
参数.
if (previousFlag === '--file') {
tabtab.logFiles();
}
3. 剖析坑
如果你想增加更多的智能完成, 你需要检查和
查看已完成行中最后一个或上一个单词,以便您能够
添加特定命令或旗帜的选项(例如日志级别) --loglevel
(例如,见a/cn.9/wg.iii/wp.iii/wp.1号文件).
Tabtab 添加了一些环境变量供您检查和使用, 这是
打电话 tabtab.parseEnv() 方法.
const env = tabtab.parseEnv(process.env);
// env:
//
// - complete A Boolean indicating whether we act in "plumbing mode" or not
// - words The Number of words in the completed line
// - point A Number indicating cursor position
// - line The String input line
// - partial The String part of line preceding cursor position
// - last The last String word of the line
// - lastPartial The last word String of partial
// - prev The String word preceding last
通常,你会想检查 env.last 或者说 env.prev。 。 。 .
if (env.prev === '--loglevel') {
tabtab.log(['error', 'warn', 'info', 'notice', 'verbose']);
}
完成机制
随便浏览 脚本 检查各种目录
创建补全时使用的模板文件 tabtab.install()。 。 。 .
以下是由 Tabtab 创建的 Bash 补全片段 .
###-begin-tabtab-test-completion-###
if type complete &>/dev/null; then
_tabtab-test_completion () {
local words cword
if type _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref -n = -n @ -n : -w words -i cword
else
cword="$COMP_CWORD"
words=("${COMP_WORDS[@]}")
fi
local si="$IFS"
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
tabtab-test completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
if type __ltrim_colon_completions &>/dev/null; then
__ltrim_colon_completions "${words[cword]}"
fi
}
complete -o default -F _tabtab-test_completion tabtab-test
fi
###-end-tabtab-test-completion-###
这个系统相当简单(虽然很难把它钉住,但是谢谢你 npm). 一个新的
创建 Bash 函数,每当 tabtab-test 是标签
已完成。 此函数然后引用完整器 tabtab-test completion
与 COMP_CWORD, (中文(简体) ), COMP_LINE 财务报告和审定财务报表 COMP_POINT 环境变量(即
解析 tabtab.parseEnv()) (中文(简体) ).
同样的机制可以适用于Zsh和Fish.
完成安装
如本报告所述 Usage > Install Completion
区域,安装一个补全涉及在源中增加一条新行
~/.bashrc, (中文(简体) ), ~/.zshrc 或者说 ~/.config/fish/config.fish 文档.
在那个 3.0.0 版本,它已被改进为只添加一行
在系统上安装的每个完成包各一个.
这样,就增加了一条单行线,以便完成各种项目 程序不会对 Shell 配置文件进行挤压.
示例 ~/.bashrc
# tabtab source for packages
# uninstall by removing these lines
[ -f ~/.config/tabtab/__tabtab.bash ] && . ~/.config/tabtab/__tabtab.bash || true
会装入文件的 __tabtab.bash中创建 ~/.config/tabtab 目录,
它将保存每个标签包定义 a 的所有源行
已完成.
# tabtab source for foo package
# uninstall by removing these lines
[ -f ~/.config/tabtab/foo.bash ] && . ~/.config/tabtab/foo.bash || true
# tabtab source for tabtab-test package
# uninstall by removing these lines
[ -f ~/.config/tabtab/tabtab-test.bash ] && . ~/.config/tabtab/tabtab-test.bash || true
完成未安装
您可以跟踪您 SHELL 配置文件中添加的文件并禁用一个 删除上述行后完成.
或者通过删除您 SHELL 配置文件中的行来简单禁用标签 .
或者,你可以用来 tabtab.uninstall() 为了你.
if (cmd === 'uninstall-completion') {
// Here we uninstall for the program `tabtab-test`
await tabtab
.uninstall({
name: 'tabtab-test'
})
.catch(err => console.error('UNINSTALL ERROR', err));
return;
}
调试
tabtab 内部记录了很多东西,使用 调试 软件包.
当测试完成时,可以看到这些日志,但写给
stdout 或者说 stderr 完成某件事会很麻烦的.
你可以用这个 TABTAB_DEBUG 要指定要登录的文件的环境变量
换句话说.
export TABTAB_DEBUG="/tmp/tabtab.log"
tail -f /tmp/tabtab.log
# in another shell
tabtab-test <tab>
见 标签 详细文件.
API 文档
请参考 api 密码 要查看生成的文档的目录( 使用) 日记2md(中文(简体) )
更改日志
请参考 变化 以查看此工程的所有可能更改.
贷项
npm的完成功能相当惊人 击打和zsh 提供命令标签补全,允许您完成名称 命令在您的路径。 通常这些函数意味着击打 脚本,对于npm来说,部分是真实的.
有一个特别的 npm completion 命令你也许想四处看看,
如果还没有.
npm completion
运行此程序应丢弃 这个
脚本
到操纵台。 此脚本同时使用 bash/zsh 并绘制正确的地图
npm 可执行的补全函数。 这些职能需要照顾
分析 comp_* 点击标签完成时可用的变量
命令,将其设置为环境变量并运行 npm completion 命令后继 -- words 字词匹配值的
命令正在完成.
这意味着使用这种技术 npm 能够执行 bash/zsh 使用节点和javascript完成。 实际上,npm的综合性 完成是相当惊人的.
这个软件包/模块完全基于 npm 的代码和 @isaacs 工作时.
光标 * > mklabs.github.io (英语) * > @mklabs (英语)