From 5a8dbc6347b3541e84fe669b22c17ad3b715e258 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 21 Jan 2026 20:22:09 +0100 Subject: Engage! --- colors.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 colors.go (limited to 'colors.go') diff --git a/colors.go b/colors.go new file mode 100644 index 0000000..d88b968 --- /dev/null +++ b/colors.go @@ -0,0 +1,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() +} -- cgit v1.2.3