Term::ANSIColor 当术语不支持颜色时 [英] Term::ANSIColor when term doesn't support colors

查看:55
本文介绍了Term::ANSIColor 当术语不支持颜色时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 Perl 程序,它为输出着色.

I wrote a Perl program that colorize its output.

不幸的是,我还有一个解释器,它运行我的程序并在不了解任何 ANSI 转义序列的情况下显示其输出.

Unfortunately I also have an interpreter that runs my program and display its output with no understanding of any ANSI escape sequences.

简单地绕过 Term::ANSIColor 的正确方法是什么?对于每个 sayprint 语句,即 say ($ENV{'TERM'} ne 'NONE')?colored('foo', green):'foo';

What is the correct way to simply bypass Term::ANSIColor? It would be awful to always treat both cases for each say or print statement i.e. say ($ENV{'TERM'} ne 'NONE')?colored('foo', green):'foo';

欢迎提出任何建议.

推荐答案

使用ANSI_COLORS_DISABLED 环境变量:

Use the ANSI_COLORS_DISABLED environment variable:

ANSI_COLORS_DISABLED

如果将此环境变量设置为真值,则此模块定义的所有函数(color()、colored() 以及程序中以前未使用的所有常量)都不会输出任何转义序列,并且相反,它只会返回空字符串或根据需要传递原始文本.这旨在支持在不支持 ANSI 转义序列的平台上使用此模块轻松使用脚本.

If this environment variable is set to a true value, all of the functions defined by this module (color(), colored(), and all of the constants not previously used in the program) will not output any escape sequences and instead will just return the empty string or pass through the original text as appropriate. This is intended to support easy use of scripts using this module on platforms that don't support ANSI escape sequences.

这篇关于Term::ANSIColor 当术语不支持颜色时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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