/ background 256色init_pair的意外诅咒

时间:2015-04-09 17:40:29

标签: python terminal curses python-curses

在正确的curses /窗口初始化之后,我可以color_pair默认颜色对,例如使用前景,-1和-1,背景颜色,但当我开始使用自定义对#s与bg / fg颜色值>自定义对时0x8我得到意外或错误的结果。

  

Term的env $ TERM =='xterm-256color'

     

Python的curses.COLORS == 256

     

Python的curses.COLOR_PAIRS == 32767

     

Python的版本== 2.7.7,curses == 2.2

#!/usr/bin/env python
"""Dumbed down code to follow:"""

import curses

# init
window = curses.initscr()
curses.start_color()
curses.use_default_colors()

# assign 'default' pairs, pairs are assigned +1 MORE than the color value!
for each in range(curses.COLORS):
    curses.init_pair(each + 1, each, -1)
for each in range(curses.COLORS):
    curses.init_pair(each + 1 + curses.COLORS, -1, each)

# custom/non-default pair
curses.init_pair(1 + 2*curses.COLORS, 0x0f, 0x15)  # white on cobalt according to colors above ???
curses.init_pair(4321, 0xd5, 0x81)  # hot pink on violet according to colors above ???

# setup
curses.meta(1)
curses.noecho()
curses.cbreak()
window.leaveok(1)
window.scrollok(0)
window.keypad(1)
window.refresh()

# print all pairs in their colors
for each in range(1 + 2*curses.COLORS):
    window.addstr(hex(each).join('  '), curses.color_pair(each))  # these are all perfect
window.addstr(hex(1 + 2*curses.COLORS).join('  '), curses.color_pair(1 + 2*curses.COLORS))  # nope: this prints 0,-1: black on default ???
window.addstr(hex(4321).join('  '), curses.color_pair(4321))  # nope: this prints 0xe1,-1: pinkish on default ???

# update
window.noutrefresh()
curses.doupdate()

# pause
window.getch()

# teardown
window.leaveok(0)
window.scrollok(1)
window.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()

检查上面的“???”。我错过了什么概念?我想为每种单色256色和一把定制的fg / bg配一个颜色对吗?

1 个答案:

答案 0 :(得分:3)

使用ncurses5,您只能拥有256个颜色对,因为这些值存储在8位字段中。这是在ncurses FAQ Why not make "xterm" equated to "xterm-256color"?中。