简体   繁体   English

标准库中某处有ANSI颜色转义码列表吗?

[英]Is there a list of ANSI color escape codes somewhere in the standard libraries?

I write a lot of little helper scripts, and often these print coloured text in the terminal. 我写了很多小助手脚本,并且通常在终端中打印彩色文本。 For the simplicity of their packaging and distribution, I often want these little scripts to be without any dependencies. 为了简化它们的打包和分发,我经常希望这些小脚本没有任何依赖性。

Hence I'm duplicating data like this a lot in scripts: 因此,我在脚本中重复这样的数据:

ansi_colors = {
    None: '\x1b[0m',  # actually black but whatevs
    'red': '\x1b[31m',
    'green' : '\x1b[32m',
    ...
}

Does this data exist anywhere in the core libraries? 这些数据是否存在于核心库中的任何位置? I dug around and found that curses has some COLOR_* constants, but they are just integers and it's not obvious how those transform into the ANSI escape codes. 我挖了COLOR_* ,发现curses有一些COLOR_*常量,但它们只是整数,并且它们如何转换成ANSI转义码并不明显。

I'm already aware of modules like termcolor , colorama , blessings , so please don't suggest to use those - I want to depend to standard libraries only. 我已经知道像termcolorcoloramablessings这样的模块,所以请不要建议使用那些 - 我只想依赖标准库。

You can check the man page console_codes(4) . 您可以查看手册页console_codes(4) What you want is the ECMA-48 Set Graphics Rendition: 你想要的是ECMA-48 Set Graphics Rendition:

The ECMA-48 SGR sequence ESC [ parameters m sets display attributes. ECMA-48 SGR序列ESC [ parameters m设置显示属性。 Several attributes can be set in the same sequence, separated by semicolons. 可以在同一序列中设置多个属性,以分号分隔。 An empty parameter (between semicolons or string initiator or terminator) is interpreted as a zero. 空参数(在分号或字符串启动器或终结符之间)被解释为零。

  param result 0 reset all attributes to their defaults 1 set bold 2 set half-bright (simulated with color on a color display) 4 set underscore (simulated with color on a color display) (the colors used to simulate dim or underline are set using ESC ] ...) 5 set blink 7 set reverse video 10 reset selected mapping, display control flag, and toggle meta flag (ECMA-48 says "primary font"). 11 select null mapping, set display control flag, reset toggle meta flag (ECMA-48 says "first alternate font"). 12 select null mapping, set display control flag, set toggle meta flag (ECMA-48 says "second alternate font"). The toggle meta flag causes the high bit of a byte to be toggled before the mapping table translation is done. 21 set normal intensity (ECMA-48 says "doubly underlined") 22 set normal intensity 24 underline off 25 blink off 27 reverse video off 30 set black foreground 31 set red foreground 32 set green foreground 33 set brown foreground 34 set blue foreground 35 set magenta foreground 36 set cyan foreground 37 set white foreground 38 set underscore on, set default foreground color 39 set underscore off, set default foreground color 40 set black background 41 set red background 42 set green background 43 set brown background 44 set blue background 45 set magenta background 46 set cyan background 47 set white background 49 set default background color 

I don't think they are available as-is in any standard Python module. 我不认为它们在任何标准Python模块中都是可用的。 But if you look carefully, you'll notice that the foreground colors are 30 plus the curses constant, while the background colors are 40 plus the curses constant. 但是如果仔细观察,你会注意到前景颜色是30加上curses常数,而背景颜色是40加上curses常数。 So you can write something like this: 所以你可以这样写:

import curses
def fcolor(c):
    return '\x1B[{0}m'.format(30 + c)
def bcolor(c):
    return '\x1B[{0}m'.format(40 + c)
def fbcolor(f, b):
    return '\x1B[{0};{1}m'.format(30 + f, 40 + b)

print(fbcolor(curses.COLOR_RED, curses.COLOR_YELLOW) + "hello!")

It depends on what you want. 这取决于你想要什么。 ANSI colors technically refers to the 8-color palette implied by ECMA-48 (ISO-6429) which has named constants in curses. ANSI颜色在技术上是指ECMA-48(ISO-6429)隐含的8色调色板,它在curses中命名了常量。 Libraries don't store escape sequences; 库不存储转义序列; those are in a database . 这些都在数据库中 For an ANSI (sic) terminal, those correspond to escape sequences which set graphic rendition (video attributes such as bold, underline, reverse — and color). 对于ANSI (sic)终端,它们对应于设置图形再现的转义序列(视频属性,如粗体,下划线,反向和颜色)。

termcap, terminfo and curses use a more general notion where you start with a color number and generate the escape sequence which can produce the corresponding color. termcap,terminfo和curses使用更通用的概念,其中您从颜色编号开始并生成可以生成相应颜色的转义序列。 Terminals can have no colors, multiple colors (8 for instance, but possibly 16, 88, 256 for xterm and similar terminals). 终端可以没有颜色,多种颜色(例如8个,但xterm和类似终端可能有16个,88个,256个)。 The information telling how to do this is stored in the terminal database as named capabilities . 告知如何执行此操作的信息作为命名功能存储在终端数据库中。 To set the ANSI foreground color, you would use setaf , either using a library call or a command-line application such as tput , eg, 要设置ANSI前景色,您可以使用setaf ,使用库调用或命令行应用程序(如tput ,例如,

tput setaf 4

for color 4 (blue). 颜色4(蓝色)。 Simple applications use the low-level termcap or terminfo interfaces, usually with terminfo databases. 简单的应用程序使用低级termcap或terminfo接口,通常使用terminfo数据库。 While you might be inclined to format your own escape sequences, theses interfaces provide formatting functions that let you avoid knowing how many colors a terminal might support. 虽然您可能倾向于格式化自己的转义序列,但这些接口提供了格式化功能,可以让您避免知道终端可能支持的颜色数量。 The terminal database tells your program, using the TERM environment variable to select the actual terminal description. 终端数据库告诉您的程序,使用TERM环境变量来选择实际的终端描述。 If you have a terminal supporting more than 8 colors, the escape sequence is not formed by adding 30 or 40 to the color number. 如果您的终端支持8种以上的颜色,则不会通过向颜色编号添加30或40来形成转义序列。

Here is an example using the low-level terminfo interface from Python: 以下是使用Python的低级terminfo接口的示例:

import curses

curses.setupterm()
curses.putp(curses.tparm(curses.tigetstr("setaf"), curses.COLOR_RED))
curses.putp(curses.tparm(curses.tigetstr("setab"), curses.COLOR_YELLOW))
curses.putp("hello!")
curses.putp(curses.tigetstr("sgr0"))
curses.putp("\n")

Further reading: 进一步阅读:

Rodrigo gave a good answer, although the colors are limited to the 8 first colors for foreground. 罗德里戈给出了一个很好的答案,虽然颜色仅限于前景的8种颜色。 This is my little contribution to handle 16 foreground colors 这是我处理16种前景色彩的小小贡献

def fcolor(c):
    if c>7:
        return '\x1B[1;{0}m'.format(22 + c)
    else:
        return '\x1B[0;{0}m'.format(30 + c)
def bcolor(c):
    return '\x1B[{0}m'.format(40 + c)
def fbcolor(f, b):
    if f>7:
        return '\x1B[1;{0};{1}m'.format(22 + f, 40 + b)
    else:
        return '\x1B[0;{0};{1}m'.format(30 + f, 40 + b)

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

相关问题 使用 ANSI 转义码 (Windows) 在 python 3.8 中更改控制台打印颜色 - Change console print color in python 3.8 with ANSI escape codes (Windows) 如何让 powershell 或 windows 终端使用 ANSI 转义码打印彩色文本? - How do I get powershell or windows terminal to print color text using ansi escape codes? 如何利用 input() 命令在 Python 中包含 ANSI 转义颜色代码? - How do I utilize the input() command to include ANSI escape color codes in Python? ANSI 转义码在 IDLE 上不起作用……(python) - ANSI escape codes not working on IDLE… (python) 在python中处理终端颜色代码(ANSI颜色转义代码) - Handling terminal colour codes ( ANSI colour escape codes ) in python Python 无法为终端输出 ANSI 颜色代码 - Python fails to output ANSI color codes for the terminal Python:将ANSI颜色代码转换为HTML - Python: Converting ANSI color codes to HTML 解析标有ANSI颜色转义序列的数据 - parsing data tagged with ANSI color escape sequences 如何检测控制台是否支持Python中的ANSI转义码? - How to detect if the console does support ANSI escape codes in Python? 为什么 ANSI 转义码有时在 CMD 中起作用 - Why do ANSI escape codes sometimes work in CMD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM