Mar*_*ter 3 command-line colors pipe grc
我正在使用rlwrap
星号为提示着色CLI
:
rlwrap -s 99999 -a -pRED /usr/sbin/asterisk -r
Run Code Online (Sandbox Code Playgroud)
我读到man rlwrap
我也可以使用
rlwrap -z pipeto
Run Code Online (Sandbox Code Playgroud)
通过着色器管道输出。我有grc
像这样工作的着色器:
cat foo | grcat <conf_file>
Run Code Online (Sandbox Code Playgroud)
上面的示例foo
使用来自 的规则着色<conf_file>
。
我怎样才能使用rlwrap -z pipeto
颜色从rlwrap
through输出grcat
?
pipeto
不幸的是,rlwrap
的内置过滤器pipeto
不会按照您希望的方式过滤输出。我发现文档相当具有误导性,但它的作用是如果您rlwrap -z pipeto some-shell
在交互中运行,然后:
如果您键入没有任何管道符号 ( |
) 的命令,则这些命令将逐字传递给some-shell
,然后简单地打印其输出;
如果您键入command | filter
,则command
传递给some-shell
解释,并在filter
打印之前通过管道传输输出(filter
您可以从 Unix shell 的命令行运行的任何命令在哪里)。
所以好消息是你可以获得你正在寻找的行为,有点,有点,通过运行rlwrap -z pipeto asterisk
然后记住附加| grc
到你想要传递给星号的每个命令。但这不会很方便,不是吗?因此outfilter
。
outfilter
我建议创建以下rlwrap
过滤器脚本:
#! /usr/bin/perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;
my $filter = new RlwrapFilter;
my $name = $filter->name;
my $filter_command = join ' ', @ARGV;
$filter->help_text("Usage: rlwrap -z '$name <filter-command>' <command>\n"
. "Filter <command> output through <filter-command>");
$filter->output_handler(sub {""});
$filter->prompt_handler(\&prompt);
$filter->run;
sub prompt {
my $prompt = shift;
my $output = $filter->cumulative_output;
$output =~ s/\r//g;
open (PIPE, "| $filter_command")
or die "Failed to create pipe: $!";
print PIPE $output;
close PIPE;
return $prompt;
}
Run Code Online (Sandbox Code Playgroud)
将其另存为outfilter
,使其可执行,然后运行rlwrap -z './outfilter <coloring-filter>' shell
. 我试过:
rlwrap -z './outfilter ccze -A' gosh
Run Code Online (Sandbox Code Playgroud)
这很好地为 Gauche 的输出着色。在您的情况下,这将变成:
rlwrap -z './outfilter grcat grcat-config' asterisk
Run Code Online (Sandbox Code Playgroud)
如果您喜欢过滤器并希望能够在不指定其路径的情况下运行它,您可以将它与内置过滤器一起移动(在我的系统上,它在目录中/usr/share/rlwrap/filters
)。
请注意,所写的过滤器可能效率低下(它为与命令外壳的每次交互生成一个新的着色过滤器副本,因为这是我能找到的让它刷新缓冲区的最短方法)并且很脆弱,但是如果外壳你正在交互并没有自己的任何黑魔法,它应该可以工作。