如何将终端的错误消息设为彩色?

时间:2023-02-04 20:55:53

I would like to see errors in my terminal colorfully like:

我希望看到终端中的错误像:

如何将终端的错误消息设为彩色?

3 个解决方案

#1


IPython will syntax-highlight Python code and stacktraces, as in the example you give (which is, in fact, a Python stack trace).

IPython将语法突出显示Python代码和堆栈跟踪,如您给出的示例(实际上是Python堆栈跟踪)。

The example you link to was not actually syntax-highlighted on the user's console, or with support from their shell, but rather highlighted by the Stack Overflow website after upload.

您链接到的示例实际上并未在用户控制台上突出显示语法,或者来自其shell的支持,而是在上载后由Stack Overflow网站突出显示。

If you want a newbie-friendly UNIX shell which does some level of general-purpose syntax highlighting, I recommend the fish shell.

如果你想要一个新手友好的UNIX shell,它可以进行某种程度的通用语法高亮显示,我推荐使用fish shell。

#2


Unfortunately, there is no perfect general solution. But there are a few general ways that work.

不幸的是,没有完美的通用解决方案。但是有一些通用的方法可行。

GRC, stands for Generic Colouriser and is for beautifying your logfiles or output of commands. It's packaged on major distributions. I've used it to colorize my make output very nicely. GRC is usually used for common commands with aliases like "grc comand". There are other colorisers and websites sometimes have their own unique ones.

GRC代表Generic Colouriser,用于美化日志文件或输出命令。它包装在主要发行版上。我用它来非常好地着色我的make输出。 GRC通常用于具有“grc comand”之类别名的常见命令。还有其他着色剂和网站有时会有自己独特的颜色。

A great many programs use the conventional command line switch --color=auto. --color=always will make color happen even from a non-interactive shell, which can be nice to look at later for logged comands using terminal code aware commands like less -R.

许多程序使用传统的命令行开关--color = auto。 --color =总是会使颜色在非交互式shell中发生,以后可以使用终端代码感知命令(如less -R)查看记录的命令。

If your text is from any half-way known program and is ever logged or stored in a file, the text editors Vim or Emacs can do some very smart highlighting on it. Other editors/ides can too but none are as comprehensive. They have general highlighting schemes for configuration files as well as specific filetypes that get very obscure, such as "Quake[1-3] configuration file". And of course there are many many color schemes for each editor to change the colors of hightlighted areas recognized by the editor. This is probably the method most used to get really good highlighting.

如果您的文本来自任何中途已知程序并且已记录或存储在文件中,则文本编辑Vim或Emacs可以对其进行一些非常智能的突出显示。其他编辑/想法也可以,但没有一个是全面的。它们具有配置文件的一般突出显示方案以及非常模糊的特定文件类型,例如“Quake [1-3]配置文件”。当然,每个编辑器都有许多颜色方案可以改变编辑器识别的高亮区域的颜色。这可能是最常用于突出显示效果的方法。

Of course there are ways to manually type colors in your terminal, with tput or the actual escape codes, and individual programs like ipython or whatever. You didn't ask that question, but you've chosen an answer like that as your favorite solution :(

当然,有一些方法可以在终端中手动输入颜色,包括tput或实际的转义码,以及ipython等各个程序。你没有问过这个问题,但是你选择了这样的答案作为你最喜欢的解决方案:(

A general solution you might expect would be to colorize standard error in your terminal. Unfortunately there is no way to do this because of lots of complications, for example your prompt is sent to standard error.

您可能期望的一般解决方案是在终端中着色标准错误。不幸的是,由于很多复杂性,没有办法做到这一点,例如你的提示被发送到标准错误。

#3


Try this sample Perl script:

试试这个Perl脚本示例:

use Term::ANSIColor;
print "Hello "
  .color("green")
  ."world"
  .color("reset")
  ."\n"

or as a shell one-liner:

或作为壳单线:

perl -e 'use Term::ANSIColor; print "Hello ".color("green")."world".color("reset")."\n"'

Note that you need to have the Term::ANSIColor module installed.

请注意,您需要安装Term :: ANSIColor模块。

#1


IPython will syntax-highlight Python code and stacktraces, as in the example you give (which is, in fact, a Python stack trace).

IPython将语法突出显示Python代码和堆栈跟踪,如您给出的示例(实际上是Python堆栈跟踪)。

The example you link to was not actually syntax-highlighted on the user's console, or with support from their shell, but rather highlighted by the Stack Overflow website after upload.

您链接到的示例实际上并未在用户控制台上突出显示语法,或者来自其shell的支持,而是在上载后由Stack Overflow网站突出显示。

If you want a newbie-friendly UNIX shell which does some level of general-purpose syntax highlighting, I recommend the fish shell.

如果你想要一个新手友好的UNIX shell,它可以进行某种程度的通用语法高亮显示,我推荐使用fish shell。

#2


Unfortunately, there is no perfect general solution. But there are a few general ways that work.

不幸的是,没有完美的通用解决方案。但是有一些通用的方法可行。

GRC, stands for Generic Colouriser and is for beautifying your logfiles or output of commands. It's packaged on major distributions. I've used it to colorize my make output very nicely. GRC is usually used for common commands with aliases like "grc comand". There are other colorisers and websites sometimes have their own unique ones.

GRC代表Generic Colouriser,用于美化日志文件或输出命令。它包装在主要发行版上。我用它来非常好地着色我的make输出。 GRC通常用于具有“grc comand”之类别名的常见命令。还有其他着色剂和网站有时会有自己独特的颜色。

A great many programs use the conventional command line switch --color=auto. --color=always will make color happen even from a non-interactive shell, which can be nice to look at later for logged comands using terminal code aware commands like less -R.

许多程序使用传统的命令行开关--color = auto。 --color =总是会使颜色在非交互式shell中发生,以后可以使用终端代码感知命令(如less -R)查看记录的命令。

If your text is from any half-way known program and is ever logged or stored in a file, the text editors Vim or Emacs can do some very smart highlighting on it. Other editors/ides can too but none are as comprehensive. They have general highlighting schemes for configuration files as well as specific filetypes that get very obscure, such as "Quake[1-3] configuration file". And of course there are many many color schemes for each editor to change the colors of hightlighted areas recognized by the editor. This is probably the method most used to get really good highlighting.

如果您的文本来自任何中途已知程序并且已记录或存储在文件中,则文本编辑Vim或Emacs可以对其进行一些非常智能的突出显示。其他编辑/想法也可以,但没有一个是全面的。它们具有配置文件的一般突出显示方案以及非常模糊的特定文件类型,例如“Quake [1-3]配置文件”。当然,每个编辑器都有许多颜色方案可以改变编辑器识别的高亮区域的颜色。这可能是最常用于突出显示效果的方法。

Of course there are ways to manually type colors in your terminal, with tput or the actual escape codes, and individual programs like ipython or whatever. You didn't ask that question, but you've chosen an answer like that as your favorite solution :(

当然,有一些方法可以在终端中手动输入颜色,包括tput或实际的转义码,以及ipython等各个程序。你没有问过这个问题,但是你选择了这样的答案作为你最喜欢的解决方案:(

A general solution you might expect would be to colorize standard error in your terminal. Unfortunately there is no way to do this because of lots of complications, for example your prompt is sent to standard error.

您可能期望的一般解决方案是在终端中着色标准错误。不幸的是,由于很多复杂性,没有办法做到这一点,例如你的提示被发送到标准错误。

#3


Try this sample Perl script:

试试这个Perl脚本示例:

use Term::ANSIColor;
print "Hello "
  .color("green")
  ."world"
  .color("reset")
  ."\n"

or as a shell one-liner:

或作为壳单线:

perl -e 'use Term::ANSIColor; print "Hello ".color("green")."world".color("reset")."\n"'

Note that you need to have the Term::ANSIColor module installed.

请注意,您需要安装Term :: ANSIColor模块。