如何使用终端颜色调色板和curses [英] How to use terminal color palette with curses
问题描述
我无法让终端调色板使用curses。
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)
此python脚本产生以下屏幕:
This python script yields the following screen:
但是,我在gnome终端调色板中有更多的颜色。
However, I do have more colors in my gnome-terminal palette. How can I access them within curses?
推荐答案
以下我是根据经验找出的。
The following I figured out by experience.
- 有256种颜色(由前8位定义)。
- 其他位用于附加属性,如突出显示。
- 将数字-1作为颜色传递回默认背景颜色和前景颜色。
- 颜色对0(mod 256) (-1,-1)。
- 颜色0到15是终端调色板颜色。
- 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.
请考虑以下测试代码。
将此添加到您的 .bashrc
:
Consider the following testing code.
Add this to your .bashrc
:
# Set proper $TERM if we are running gnome-terminal
if [ "$COLORTERM" == "gnome-terminal" ]
then
TERM=xterm-256color
fi
将它放在python文件中并运行。
Put this in a python file and run it.
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.
如你所见,颜色对1-16是前景颜色的终端颜色调色板。所以这是我们可以初始化终端调色板,并使用它。
As you see, the colors pairs 1-16 are the terminal color palette for foreground colors. So this is how we can initialize the terminal color palette, and use it.
这篇关于如何使用终端颜色调色板和curses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!