简体   繁体   English

鱼壳自定义功能,用于输出文本着色

[英]Fish shell custom function for output text coloring

When using fish shell in a terminal-emulator (such as terminator) together with a command that outputs lots of text it could be useful to get some color coding on the output. 在终端仿真器(例如终结器)中使用鱼壳以及输出大量文本的命令时,在输出上进行一些颜色编码可能很有用。 I know that a script can add color code information to the output like " grep --color=auto ". 我知道脚本可以将颜色代码信息添加到输出中,例如“ grep --color = auto ”。 I guess it's possible to modify the fish terminal to scan through the output and add this in special places? 我想可以修改fish终端以扫描输出并将其添加到特殊位置吗?

What I want to do is that the text "error" appearing in the output from any script always is marked red and that "warning" always is marked yellow. 我想做的是,出现在任何脚本输出中的文本“错误”始终标记为红色,而“警告”始终标记为黄色。 Anyone knows if this is possible by introducing function files in the ~/.config/fish/functions dir or similar? 有人知道通过在〜/ .config / fish / functions目录或类似目录中引入功能文件是否可行?

This is basically a layering violation. 这基本上是分层违规。 Usually the output of external commands does not go back through the shell. 通常,外部命令的输出不会通过外壳返回。 It goes directly to the terminal. 它直接进入终端。

Also, anything you do here has the potential to slow output down. 另外,您在这里所做的任何事情都有可能减慢输出速度。 (And because of fish issue #1396 , this can be rather extreme). (并且由于#1396号鱼类,这可能非常极端)。

That said, it's possible if you always pipe to a function like this 就是说,有可能总是将管道传递给这样的函数

function colorstuff
    while read -l input
        switch $input
            case "*error*"
                set_color red
                echo $input
            case "*warning*"
                set_color yellow
                echo $input
            case "*"
                set_color normal
                echo $input
        end
    end
    set_color normal
end

Use it like somecommand | colorstuff somecommand | colorstuff一样使用它somecommand | colorstuff somecommand | colorstuff . somecommand | colorstuff (And maybe add ^&1 if you also wish to have stderr colored) (如果您还希望为stderr上色,则可以添加^&1

In my tests, this causes a noticeable slowdown, and even with that issue fixed it will still be slower since it has to match every single line. 在我的测试中,这会导致明显的速度下降,即使修复了该问题,它也仍然会变慢,因为它必须匹配每一行。

Really, the real solution is for whatever tool you are using to color itself, since it knows what the output means . 确实,真正的解决方案是使用您自己用于着色的任何工具,因为它知道输出的含义 All this can do is look for keywords. 所有可以做的就是寻找关键字。

出于一般输出着色的需要,我正是为此目的将grc插件添加到了Tackle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM