summaryrefslogtreecommitdiff
path: root/colors.go
blob: d88b9688431a909701315d19a7338e24f7e62846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

// Utility to print all 256 terminal colors. This is useful for debugging themes
// and ensuring the terminal supports the expected color range.

import (
	"fmt"
	"os"

	"github.com/nsf/termbox-go"
)

// PrintColors initializes termbox and draws a grid of all 256 available colors.
func PrintColors() {
	err := termbox.Init()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to init termbox: %v\n", err)
		return
	}
	defer termbox.Close()

	// Enable 256-color mode for the output.
	termbox.SetOutputMode(termbox.Output256)
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

	w, _ := termbox.Size()

	// Adjust grid columns based on terminal width.
	cols := 16
	if w < 64 {
		cols = 8
	}

	// Loop through all 256 colors and draw them in a grid.
	for i := 0; i < 256; i++ {
		row := (i / cols) * 2
		col := (i % cols) * 5

		bg := termbox.Attribute(i)
		fg := termbox.ColorWhite
		// Ensure text is readable against light/dark backgrounds.
		if i == 7 || i > 240 {
			fg = termbox.ColorBlack
		}

		// Draw the color index and a colored block.
		str := fmt.Sprintf("%5d", i)
		for j, r := range str {
			termbox.SetCell(col+j, row, r, fg, bg)
			termbox.SetCell(col+j, row+1, ' ', fg, bg)
		}
	}

	msg := "Press any key to exit..."
	for i, r := range msg {
		termbox.SetCell(i, (256/cols)*2, r, termbox.ColorWhite, termbox.ColorDefault)
	}

	termbox.Flush()
	// Wait for any key press before closing.
	termbox.PollEvent()
}