mirror of
https://github.com/NotAShelf/hexxy.git
synced 2024-11-01 12:01:15 +00:00
streamline ANSI color code generation for faster execution
This was significantly faster, wow
This commit is contained in:
parent
fea3864961
commit
6f68697db7
1 changed files with 11 additions and 11 deletions
22
color.go
22
color.go
|
@ -13,35 +13,35 @@ var CLRR = []byte("\x1b[0m")
|
|||
|
||||
type Color struct {
|
||||
disable bool
|
||||
values [256]string
|
||||
cvalues [256][]byte
|
||||
values [256][]byte
|
||||
}
|
||||
|
||||
func (c *Color) Compute() {
|
||||
const WHITEB = "\x1b[1;37m"
|
||||
for i := 0; i < 256; i++ {
|
||||
var fg, bg string
|
||||
var fg, bg []byte
|
||||
|
||||
lowVis := i == 0 || (i >= 16 && i <= 20) || (i >= 232 && i <= 242)
|
||||
|
||||
if lowVis {
|
||||
fg = WHITEB + "\x1b[38;5;" + "255" + "m"
|
||||
bg = "\x1b[48;5;" + strconv.Itoa(int(i)) + "m"
|
||||
fg = append([]byte(WHITEB), []byte("\x1b[38;5;255m")...)
|
||||
bg = []byte("\x1b[48;5;" + strconv.Itoa(i) + "m")
|
||||
} else {
|
||||
fg = "\x1b[38;5;" + strconv.Itoa(int(i)) + "m"
|
||||
bg = ""
|
||||
fg = []byte("\x1b[38;5;" + strconv.Itoa(i) + "m")
|
||||
bg = nil
|
||||
}
|
||||
c.values[i] = bg + fg
|
||||
c.cvalues[i] = []byte(bg + fg)
|
||||
c.values[i] = append(bg, fg...)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Color) Colorize(s string, clr byte) string {
|
||||
const NOCOLOR = "\x1b[0m"
|
||||
return c.values[clr] + s + NOCOLOR
|
||||
colorCode := c.values[clr]
|
||||
return string(append(append(colorCode, s...), []byte(NOCOLOR)...))
|
||||
}
|
||||
|
||||
// function to colorize bytes - avoiding string conversions
|
||||
func (c *Color) Colorize2(clr byte) ([]byte, []byte) {
|
||||
return c.cvalues[clr], CLRR
|
||||
colorCode := c.values[clr]
|
||||
return colorCode, CLRR
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue