2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-05 00:00:14 +00:00

colorscheme parse hex

This commit is contained in:
Miguel Mota 2019-06-09 17:51:06 -07:00
parent eb14d981d8
commit a7b5713587
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
3 changed files with 81 additions and 40 deletions

View File

@ -1,8 +1,9 @@
package cointop
import (
"github.com/fatih/color"
"github.com/jroimartin/gocui"
fcolor "github.com/fatih/color"
gocui "github.com/jroimartin/gocui"
xtermcolor "github.com/tomnomnom/xtermcolor"
)
// colorschemeColors ..
@ -20,29 +21,29 @@ type Colorscheme struct {
cache colorCache
}
var fgcolorschemeColorsMap = map[string]color.Attribute{
"black": color.FgBlack,
"blue": color.FgBlue,
"cyan": color.FgCyan,
"green": color.FgGreen,
"magenta": color.FgMagenta,
"red": color.FgRed,
"white": color.FgWhite,
"yellow": color.FgYellow,
var fgcolorschemeColorsMap = map[string]fcolor.Attribute{
"black": fcolor.FgBlack,
"blue": fcolor.FgBlue,
"cyan": fcolor.FgCyan,
"green": fcolor.FgGreen,
"magenta": fcolor.FgMagenta,
"red": fcolor.FgRed,
"white": fcolor.FgWhite,
"yellow": fcolor.FgYellow,
}
var bgcolorschemeColorsMap = map[string]color.Attribute{
"black": color.BgBlack,
"blue": color.BgBlue,
"cyan": color.BgCyan,
"green": color.BgGreen,
"magenta": color.BgMagenta,
"red": color.BgRed,
"white": color.BgWhite,
"yellow": color.BgYellow,
var bgcolorschemeColorsMap = map[string]fcolor.Attribute{
"black": fcolor.BgBlack,
"blue": fcolor.BgBlue,
"cyan": fcolor.BgCyan,
"green": fcolor.BgGreen,
"magenta": fcolor.BgMagenta,
"red": fcolor.BgRed,
"white": fcolor.BgWhite,
"yellow": fcolor.BgYellow,
}
var gocuicolorschemeColorsMap = map[string]gocui.Attribute{
var gocuiColorschemeColorsMap = map[string]gocui.Attribute{
"black": gocui.ColorBlack,
"blue": gocui.ColorBlue,
"cyan": gocui.ColorCyan,
@ -241,29 +242,29 @@ func (c *Colorscheme) toSprintf(name string) ISprintf {
return cached
}
var colors []color.Attribute
var attrs []fcolor.Attribute
if v, ok := c.colors[name+"_fg"].(string); ok {
if fg, ok := c.toFgAttr(v); ok {
colors = append(colors, fg)
attrs = append(attrs, fg)
}
}
if v, ok := c.colors[name+"_bg"].(string); ok {
if bg, ok := c.toBgAttr(v); ok {
colors = append(colors, bg)
attrs = append(attrs, bg)
}
}
if v, ok := c.colors[name+"_bold"].(bool); ok {
if bold, ok := c.toBoldAttr(v); ok {
colors = append(colors, bold)
attrs = append(attrs, bold)
}
}
if v, ok := c.colors[name+"_underline"].(bool); ok {
if underline, ok := c.toUnderlineAttr(v); ok {
colors = append(colors, underline)
attrs = append(attrs, underline)
}
}
c.cache[name] = color.New(colors...).SprintFunc()
c.cache[name] = fcolor.New(attrs...).SprintFunc()
return c.cache[name]
}
@ -291,25 +292,55 @@ func (c *Colorscheme) gocuiBgColor(name string) gocui.Attribute {
return gocui.ColorDefault
}
func (c *Colorscheme) toFgAttr(k string) (color.Attribute, bool) {
attr, ok := fgcolorschemeColorsMap[k]
return attr, ok
func (c *Colorscheme) toFgAttr(v string) (fcolor.Attribute, bool) {
if attr, ok := fgcolorschemeColorsMap[v]; ok {
return attr, true
}
if code, ok := hexToAnsi(v); ok {
return fcolor.Attribute(code), true
}
return 0, false
}
func (c *Colorscheme) toBgAttr(k string) (color.Attribute, bool) {
attr, ok := bgcolorschemeColorsMap[k]
return attr, ok
func (c *Colorscheme) toBgAttr(v string) (fcolor.Attribute, bool) {
if attr, ok := bgcolorschemeColorsMap[v]; ok {
return attr, true
}
if code, ok := hexToAnsi(v); ok {
return fcolor.Attribute(code), true
}
return 0, false
}
func (c *Colorscheme) toBoldAttr(v bool) (color.Attribute, bool) {
return color.Bold, v
func (c *Colorscheme) toBoldAttr(v bool) (fcolor.Attribute, bool) {
return fcolor.Bold, v
}
func (c *Colorscheme) toUnderlineAttr(v bool) (color.Attribute, bool) {
return color.Underline, v
func (c *Colorscheme) toUnderlineAttr(v bool) (fcolor.Attribute, bool) {
return fcolor.Underline, v
}
func (c *Colorscheme) toGocuiAttr(k string) (gocui.Attribute, bool) {
attr, ok := gocuicolorschemeColorsMap[k]
return attr, ok
func (c *Colorscheme) toGocuiAttr(v string) (gocui.Attribute, bool) {
if attr, ok := gocuiColorschemeColorsMap[v]; ok {
return attr, true
}
if code, ok := hexToAnsi(v); ok {
return gocui.Attribute(code), true
}
return 0, false
}
func hexToAnsi(h string) (uint8, bool) {
code, err := xtermcolor.FromHexStr(h)
if err != nil {
return 0, false
}
return code, true
}

2
go.mod
View File

@ -15,9 +15,11 @@ require (
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/miguelmota/go-coinmarketcap v0.1.4
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/nsf/termbox v1.1.2 // indirect
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sirupsen/logrus v1.4.1
github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect
golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 // indirect
golang.org/x/sys v0.0.0-20190416152802-12500544f89f // indirect

8
go.sum
View File

@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/anaskhan96/soup v1.0.1/go.mod h1:pT5vs4HXDwA5y4KQCsKvnkpQd3D+joP7IqpiGskfWW0=
github.com/anaskhan96/soup v1.1.1 h1:Duux/0htS2Va7XLJ9qIakCSey790hg9OFRm2FwlMTy0=
github.com/anaskhan96/soup v1.1.1/go.mod h1:pT5vs4HXDwA5y4KQCsKvnkpQd3D+joP7IqpiGskfWW0=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
@ -40,6 +42,8 @@ github.com/miguelmota/go-coinmarketcap v0.1.4/go.mod h1:Jdv/kqtKclIElmoNAZMMJn0D
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/nsf/termbox v1.1.2 h1:w1jYRf7nu3ZBICMbHKsyihdYyydAAhBEKcTfVxJQsxE=
github.com/nsf/termbox v1.1.2/go.mod h1:vzR0wsVVTMx636zhtIZ1b1VbIMxrLeI3tSRN13kMIPI=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
@ -49,6 +53,8 @@ github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e h1:Ee+VZw13r9NTOMnwTPs6O5KZ0MJU54hsxu9FpZ4pQ10=
github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e/go.mod h1:fSIW/szJHsRts/4U8wlMPhs+YqJC+7NYR+Qqb1uJVpA=
golang.org/x/arch v0.0.0-20190312162104-788fe5ffcd8c/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
@ -67,5 +73,7 @@ golang.org/x/sys v0.0.0-20190416152802-12500544f89f h1:1ZH9RnjNgLzh6YrsRp/c6ddZ8
golang.org/x/sys v0.0.0-20190416152802-12500544f89f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/go-playground/colors.v1 v1.2.0 h1:SPweMUve+ywPrfwao+UvfD5Ah78aOLUkT5RlJiZn52c=
gopkg.in/go-playground/colors.v1 v1.2.0/go.mod h1:AvbqcMpNXVl5gBrM20jBm3VjjKBbH/kI5UnqjU7lxFI=
gopkg.in/h2non/gock.v1 v1.0.14/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=