意想不到的诅咒fore/background 256色init_pair'ing

Unexpected curses fore/background 256 color init_pair'ing

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

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

Python's curses.COLORS == 256

Python's curses.COLOR_PAIRS == 32767

Python's version == 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?

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