如何从在Perl终端管不失颜色? [英] How can I pipe from terminal in Perl without losing color?

查看:133
本文介绍了如何从在Perl终端管不失颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个Perl脚本,这需要colorgcc的输出(或打印彩色文本到终端的任何其他脚本),添加/删除部分字符串,然后打印出结果在相同的颜色作为输入字符串。

以下code将通过在 color_producing_script 生产的每行前打印的Hello World。输出将是全黑的,而输入是五彩的。我怎样才能修改这个脚本保存原来的颜色?

 打开(CMDcolor_producing_script |);而(小于CMD&GT){
    打印的'Hello World。 $ _;
}

我使用的庆典端子。

修改

每优异的第一个评论,这本身并不是一个Perl的问题。只是运行 color_producing_script |猫去掉颜色了。所以,问题可以被改写为你如何强制色彩通过管道?

编辑2

它看起来像海湾合作委员会(1.3.2)的最新版本包括if子句中的CGCC_FORCE_COLOR环境变量,如果它被定义,colorgcc力量颜色:

 出口CGCC_FORCE_COLOR =真


解决方案

确实 color_producing_script 改变自己的行为,当它在一个管的使用呢?尝试

  color_producing_script |猫

在命令行。即使是它可能有一个选项,以强制的色彩输出。

的Perl脚本, colorgcc ,是专门检查是否输出到非TTY并跳过上色,如果是这样的话。

 #获取终端类型。
$终端= $ ENV {任期} || 哑;#如果它在终端类型列表中没有的颜色,或者如果
#我们写的东西,这不是一个TTY,不做颜色。
如果(!-t STDOUT || $ NOCOLOR {$终端})
{
   EXEC $编译器,@ARGV
      或死亡(无法exec的);
}

编辑:

您可以修改脚本中的一个或多个以下的方法:


  • 注释掉的考验,使其始终产生彩色输出

  • 添加功能,支持阅读的环境变量设置是否上色

  • 添加功能,支持〜/ .colorgccrc 配置文件中的颜色总是选择

  • 添加功能,支持您的其余选项传递到编译器之前剥离彩色总是命令行选项

您也可以使用 期望 脚本< A HREF =htt​​p://expect.sourceforge.net/example/unbuffer.man.html相对=nofollow> 无缓冲 创建一个伪-tty是这样的:

 无缓冲GCC file.c中|猫

(其中是一个站在接收方)。

所有这些都是基于使用 colorgcc 在命令行。应该有一个Perl脚本中做类似的事情类似物。

I'm trying to write a perl script which takes the output of colorgcc (or any other script that prints colored text to terminal), adds/removes parts of the string, and then prints the result in the same color as the input string.

The following code will print "Hello World" in front of each line produced by the color_producing_script. The output will be all black, while the input is multicolored. How can I modified this script to conserve the original colors?

open(CMD, "color_producing_script |");

while(<CMD>) {
    print 'Hello World' . $_;
}

I'm using bash terminal.

Edit

Per the excellent first comment, this is not a Perl issue per se. Just running color_producing_script | cat strips the color off. So the question can be rephrased to "How do you force color through the pipe?"

Edit 2

It looks like the latest version of gcc (1.3.2) includes the CGCC_FORCE_COLOR environment variable in the if clause, and if it's defined, colorgcc forces color:

export CGCC_FORCE_COLOR=true

解决方案

Does color_producing_script change its behavior when it's used in a pipe? Try

color_producing_script | cat

at the command line. It may have an option to force color output even when it is.

The Perl script, colorgcc, is specifically checking to see if output is to a non-tty and skipping the colorization if that's the case.

# Get the terminal type. 
$terminal = $ENV{"TERM"} || "dumb";

# If it's in the list of terminal types not to color, or if
# we're writing to something that's not a tty, don't do color.
if (! -t STDOUT || $nocolor{$terminal})
{
   exec $compiler, @ARGV
      or die("Couldn't exec");
}

Edit:

You could modify the script in one or more of the following ways:

  • comment out the test and make it always produce color output
  • add functionality to support reading an environment variable that sets whether to colorize
  • add functionality to support a color-always option in the ~/.colorgccrc configuration file
  • add functionality to support a color-always command line option that you strip before passing the rest of the options to the compiler

You could also use the expect script unbuffer to create a pseudo-tty like this:

unbuffer gcc file.c | cat

(where cat is a standin recipient).

All of this is based on using colorgcc from the command line. There should be analogs for doing similar things within a Perl script.

这篇关于如何从在Perl终端管不失颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆