2024-02-02 00:12:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const GREY = "\x1b[38;2;111;111;111m"
|
|
|
|
const CLR = "\x1b[0m"
|
|
|
|
|
|
|
|
var ESC = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b}
|
|
|
|
var CLEAR = []byte{0x5c, 0x78, 0x31, 0x62, 0x5b, 0x30, 0x6d}
|
2024-02-02 01:21:56 +00:00
|
|
|
var CLRR = []byte("\x1b[0m")
|
2024-02-02 00:12:24 +00:00
|
|
|
|
|
|
|
type Color struct {
|
|
|
|
disable bool
|
2024-06-07 14:04:40 +00:00
|
|
|
values [256][]byte
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Color) Compute() {
|
|
|
|
const WHITEB = "\x1b[1;37m"
|
|
|
|
for i := 0; i < 256; i++ {
|
2024-06-07 14:04:40 +00:00
|
|
|
var fg, bg []byte
|
2024-02-02 00:12:24 +00:00
|
|
|
|
|
|
|
lowVis := i == 0 || (i >= 16 && i <= 20) || (i >= 232 && i <= 242)
|
|
|
|
|
|
|
|
if lowVis {
|
2024-06-07 14:04:40 +00:00
|
|
|
fg = append([]byte(WHITEB), []byte("\x1b[38;5;255m")...)
|
|
|
|
bg = []byte("\x1b[48;5;" + strconv.Itoa(i) + "m")
|
2024-02-02 00:12:24 +00:00
|
|
|
} else {
|
2024-06-07 14:04:40 +00:00
|
|
|
fg = []byte("\x1b[38;5;" + strconv.Itoa(i) + "m")
|
|
|
|
bg = nil
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|
2024-06-07 14:04:40 +00:00
|
|
|
c.values[i] = append(bg, fg...)
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Color) Colorize(s string, clr byte) string {
|
|
|
|
const NOCOLOR = "\x1b[0m"
|
2024-06-07 14:04:40 +00:00
|
|
|
colorCode := c.values[clr]
|
|
|
|
return string(append(append(colorCode, s...), []byte(NOCOLOR)...))
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:21:56 +00:00
|
|
|
// function to colorize bytes - avoiding string conversions
|
|
|
|
func (c *Color) Colorize2(clr byte) ([]byte, []byte) {
|
2024-06-07 14:04:40 +00:00
|
|
|
colorCode := c.values[clr]
|
|
|
|
return colorCode, CLRR
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|