1package main
 2
 3// Utility to print all 256 terminal colors. This is useful for debugging themes
 4// and ensuring the terminal supports the expected color range.
 5
 6import (
 7	"fmt"
 8	"os"
 9
10	"github.com/nsf/termbox-go"
11)
12
13// PrintColors initializes termbox and draws a grid of all 256 available colors.
14func PrintColors() {
15	err := termbox.Init()
16	if err != nil {
17		fmt.Fprintf(os.Stderr, "failed to init termbox: %v\n", err)
18		return
19	}
20	defer termbox.Close()
21
22	// Enable 256-color mode for the output.
23	termbox.SetOutputMode(termbox.Output256)
24	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
25
26	w, _ := termbox.Size()
27
28	// Adjust grid columns based on terminal width.
29	cols := 16
30	if w < 64 {
31		cols = 8
32	}
33
34	// Loop through all 256 colors and draw them in a grid.
35	for i := 0; i < 256; i++ {
36		row := (i / cols) * 2
37		col := (i % cols) * 5
38
39		bg := termbox.Attribute(i)
40		fg := termbox.ColorWhite
41		// Ensure text is readable against light/dark backgrounds.
42		if i == 7 || i > 240 {
43			fg = termbox.ColorBlack
44		}
45
46		// Draw the color index and a colored block.
47		str := fmt.Sprintf("%5d", i)
48		for j, r := range str {
49			termbox.SetCell(col+j, row, r, fg, bg)
50			termbox.SetCell(col+j, row+1, ' ', fg, bg)
51		}
52	}
53
54	msg := "Press any key to exit..."
55	for i, r := range msg {
56		termbox.SetCell(i, (256/cols)*2, r, termbox.ColorWhite, termbox.ColorDefault)
57	}
58
59	termbox.Flush()
60	// Wait for any key press before closing.
61	termbox.PollEvent()
62}