Python 如何使用带有诅咒的终端调色板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18551558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:02:15  来源:igfitidea点击:

How to use terminal color palette with curses

pythoncolorscurses

提问by Chiel ten Brinke

I can't get the terminal color palette to work with curses.

我无法让终端调色板与诅咒一起工作。

import curses

def main(stdscr):
    curses.use_default_colors()
    for i in range(0,7):
        stdscr.addstr("Hello", curses.color_pair(i))
    stdscr.getch()

curses.wrapper(main)

This python script yields the following screen:

这个 python 脚本产生以下屏幕:

enter image description here

在此处输入图片说明

However, I do have more colors in my gnome-terminal palette. How can I access them within curses?

但是,我的 gnome 终端调色板中确实有更多颜色。我怎样才能在诅咒中访问它们?

采纳答案by Chiel ten Brinke

The following I figured out by experiment on my own pc (Ubuntu 14.04, python 3).

以下是我在自己的电脑(Ubuntu 14.04,python 3)上通过实验得出的结论。

  • There are 256 colors (defined by the first 8 bits).
  • The other bits are used for additional attributes, such as highlighting.
  • Passing the number -1 as color falls back to the default background and foreground colors.
  • The color pair 0 (mod 256) is fixed on (-1, -1).
  • The colors 0 till 15 are the terminal palette colors.
  • 有 256 种颜色(由前 8 位定义)。
  • 其他位用于附加属性,例如突出显示。
  • 传递数字 -1 作为颜色回退到默认背景和前景色。
  • 颜色对 0 (mod 256) 固定在 (-1, -1) 上。
  • 颜色 0 到 15 是终端调色板颜色。

Consider the following testing code. Add this to your .bashrc:

考虑以下测试代码。将此添加到您的.bashrc

# Set proper $TERM if we are running gnome-terminal
if [ "$COLORTERM" == "gnome-terminal" ]
then
    TERM=xterm-256color
fi

Put this in a python file and run it.

把它放在一个python文件中并运行它。

import curses

def main(stdscr):
    curses.start_color()
    curses.use_default_colors()
    for i in range(0, curses.COLORS):
        curses.init_pair(i + 1, i, -1)
    try:
        for i in range(0, 255):
            stdscr.addstr(str(i), curses.color_pair(i))
    except curses.ERR:
        # End of screen reached
        pass
    stdscr.getch()

curses.wrapper(main)

Running it will yield the following output.

运行它将产生以下输出。

screenshot

截屏

As you see, the colors pairs 1-16 are the terminal color palette for foreground colors.

如您所见,颜色对 1-16 是前景色的终端调色板。

回答by Chiel ten Brinke

I currently put these lines in front of my script.

我目前将这些行放在我的脚本前面。

curses.use_default_colors()
for i in range(0, curses.COLORS):
    curses.init_pair(i, i, -1);

I don't know if it is the best solution, but at least it yields some color pairs that are consistent with the terminal color palette.

我不知道这是否是最好的解决方案,但至少它产生了一些与终端调色板一致的颜色对。

回答by Martijn Pieters

The terminal 'color palette' is set by the terminal application itself to map default curses colours to application-specific 'interpretations'. If you use red, the terminal can choose to display that as burgundy or cherry red, or if the user so desires, something completely different.

终端“调色板”由终端应用程序本身设置,以将默认的curses 颜色映射到特定于应用程序的“解释”。如果您使用红色,终端可以选择将其显示为酒红色或樱桃红色,或者如果用户愿意,则显示完全不同的东西。

In other words, just use the curses colours (combined with or without the bright or blink modifiers) and things should Just Work.

换句话说,只需使用诅咒颜色(结合或不结合明亮或闪烁修饰符),事情就应该正常工作。

I believe that the curses.use_default_colors()call merely makes transparency available; it is a direct call to the use_default_colors()ncurses API function. ncurses colors are otherwise palette based; you need to set your own color attributes per pair number with curses.init_pair()calls, then select a color pair with curses.color_pair()from the palette to display text with that specific pair; or build text attributes directly for a given addstr()call.

我相信这个curses.use_default_colors()电话只是让透明度变得可用;它是对use_default_colors()ncurses API 函数的直接调用。ncurses 颜色是基于调色板的;您需要使用curses.init_pair()call为每对编号设置自己的颜色属性,然后curses.color_pair()从调色板中选择一个颜色对以显示具有该特定对的文本;或直接为给定addstr()调用构建文本属性。

回答by user1404316

I don't have the rep-points to submit this as a comment to Chiel ten Brinke's excellent answer, so I'll offer here a more useful version of his color script:

我没有代表点将其作为对 Chiel 10 Brinke 出色答案的评论提交,因此我将在此处提供他的颜色脚本的更有用版本:

import curses
def main(stdscr):
    curses.start_color()
    curses.use_default_colors()
    for i in range(0, curses.COLORS):
        curses.init_pair(i + 1, i, -1)
    stdscr.addstr(0, 0, '{0} colors available'.format(curses.COLORS))
    maxy, maxx = stdscr.getmaxyx()
    maxx = maxx - maxx % 5
    x = 0
    y = 1
    try:
        for i in range(0, curses.COLORS):
            stdscr.addstr(y, x, '{0:5}'.format(i), curses.color_pair(i))
            x = (x + 5) % maxx
            if x == 0:
                y += 1
    except curses.ERR:
        pass
    stdscr.getch()
curses.wrapper(main)

回答by speller

You can use the culourpackage by installing with:

您可以culour通过安装来使用该软件包:

pip install culour

And then you can use it to print with color to curses:

然后你可以用它来用颜色打印到诅咒:

culour.addstr(window, "colored string")