Last active
May 18, 2024 13:16
-
-
Save cmang/7d366f677cd067c00e58a9d9c97141c5 to your computer and use it in GitHub Desktop.
Test extended curses colors. Make your terminal kind of big for this one. Like 35 lines, 105 columns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Test program for ncurses extended color pairs. (by Sam Foster, may 2024) | |
# | |
# Generates 256 foreground colors, background color 0, to use the first 256 color pairs. | |
# Then does the same for BG color 1, then 2, etc. until we have 256 * 256 pairs. | |
# | |
# If it works, we should end up with every combination of FG colors 0-255 and BG colors 0-255. | |
# If it doesn't work right, the color pairs repeat themselves. | |
# | |
# Use left and right arrows (or "j" and "k") to scroll through the color pairs. Space advances by 25 pairs. | |
# "q" quits. | |
import curses | |
def init_color_pairs(stdscr): | |
curses.use_default_colors() | |
fg_colors = 256 | |
bg_colors = 256 | |
pair = 0 | |
for bg in range(0, bg_colors): | |
for fg in range(0, fg_colors): | |
curses.init_pair(pair, fg, bg) | |
pair += 1 | |
stdscr.addstr(1, 1, f"Initializing pair: {pair}") | |
stdscr.refresh() | |
return pair # total number of color pairs | |
def main(stdscr): | |
stdscr.clear() | |
pairs_to_show = 400 # how many pairs to show on screen at a given time | |
total_color_pairs = 0 | |
hicolor_support = curses.has_extended_color_support() | |
if hicolor_support: | |
total_color_pairs = init_color_pairs(stdscr) | |
help_string = 'j/k or left/right to scroll. space to scroll 25 pairs. < and > for home and end. q to quit' | |
help_line = 26 | |
running = True | |
c = None # for getch() | |
pair = 0 # first color pair to display on screen | |
while running: | |
# update screen | |
status_string = f"Extended colors: {hicolor_support}, max colors: {curses.COLORS}, total pairs: {total_color_pairs}, first shown pair: {pair}" | |
stdscr.clear() | |
stdscr.addstr(0, 0, status_string) | |
stdscr.addstr(help_line, 0, help_string) | |
# draw some colors, up to 500 at a time | |
line = 1 | |
col = 0 | |
for shown_pair in range(pair, min(pair+pairs_to_show, total_color_pairs)): | |
try: | |
stdscr.addstr(line, col, f"{shown_pair}", curses.color_pair(shown_pair)) | |
except: | |
print(f"Error, line: {line}, col: {col}, pair: {shown_pair}") | |
if col < 80: | |
col += 5 | |
else: | |
col = 0 | |
line += 1 | |
stdscr.refresh() | |
# get input | |
c = stdscr.getch() | |
if c == ord("q"): | |
running = False | |
if c == ord("k") or c == 261: # 261 = right arrow key | |
if pair < total_color_pairs - 200: | |
pair += 1 | |
if c == ord("j") or c == 260: # 260 = left arrow key | |
if pair > 0: | |
pair = pair - 1 | |
if c == ord(" "): # Space = go forward 25 pairs | |
pair = min(pair + 25, total_color_pairs) | |
if c == ord("<"): # < = go to first page | |
pair = 0 | |
if c == ord(">"): # > = go to last page | |
pair = max(total_color_pairs - pairs_to_show, 0) | |
curses.wrapper(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment