Skip to content

Add curses dynamic color-pair functions: alloc_pair, find_pair, free_pair, reset_color_pairs #151774

@serhiy-storchaka

Description

@serhiy-storchaka

The ncurses extended-color extension provides dynamic color-pair management that the standard :mod:curses functions cannot express, and which is currently not wrapped.

alloc_pair(fg, bg) allocates a color pair for the given foreground and background colors and returns its number, reusing an existing pair if the same combination was already allocated. init_pair(pair, fg, bg) instead requires the caller to choose the pair number, with no way to let curses pick a free one.

find_pair(fg, bg) returns the number of an already-allocated pair for the given colors, or -1 if none has been allocated. There is no standard reverse lookup (the inverse of pair_content).

free_pair(pair) releases a pair previously allocated by alloc_pair(); there is no standard equivalent.

reset_color_pairs() discards all color-pair definitions at once; there is no standard bulk reset.

I propose to expose these four functions, available when CPython is built against a wide-character ncurses with extended-color support (the same condition as curses.has_extended_color_support()). The already-wrapped init_color/init_pair/color_content/pair_content already route to the *_extended_* variants internally, so those four extended functions do not need separate names; only the dynamic pair-management functions above add new capability.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions