2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-10 13:10:26 +00:00
cointop/cointop/colorscheme.go

385 lines
9.0 KiB
Go
Raw Normal View History

2019-06-09 01:16:57 +00:00
package cointop
import (
2020-11-21 22:22:28 +00:00
"fmt"
"strconv"
"sync"
2019-06-10 00:51:06 +00:00
fcolor "github.com/fatih/color"
"github.com/miguelmota/gocui"
"github.com/tomnomnom/xtermcolor"
2019-06-09 01:16:57 +00:00
)
2019-07-01 01:50:48 +00:00
// TODO: fix hex color support
2021-09-04 21:16:49 +00:00
// ColorschemeColors is a map of color string names to Attribute types
type ColorschemeColors map[string]interface{}
2019-06-09 01:16:57 +00:00
2019-11-18 07:37:23 +00:00
// ISprintf is a sprintf interface
2019-06-09 06:18:07 +00:00
type ISprintf func(...interface{}) string
// ColorCache is a map of color string names to sprintf functions
2021-09-04 21:16:49 +00:00
type ColorCache map[string]ISprintf
2019-06-09 01:16:57 +00:00
2019-11-18 07:37:23 +00:00
// Colorscheme is the struct for colorscheme
2019-06-09 06:49:26 +00:00
type Colorscheme struct {
2021-09-04 21:16:49 +00:00
colors ColorschemeColors
cache ColorCache
cacheMutex sync.RWMutex
2019-06-09 06:18:07 +00:00
}
2021-09-04 21:16:49 +00:00
var FgColorschemeColorsMap = map[string]fcolor.Attribute{
2019-06-10 00:51:06 +00:00
"black": fcolor.FgBlack,
"blue": fcolor.FgBlue,
"cyan": fcolor.FgCyan,
"green": fcolor.FgGreen,
"magenta": fcolor.FgMagenta,
"red": fcolor.FgRed,
"white": fcolor.FgWhite,
"yellow": fcolor.FgYellow,
2019-06-09 06:18:07 +00:00
}
2021-09-04 21:16:49 +00:00
var BgColorschemeColorsMap = map[string]fcolor.Attribute{
2019-06-10 00:51:06 +00:00
"black": fcolor.BgBlack,
"blue": fcolor.BgBlue,
"cyan": fcolor.BgCyan,
"green": fcolor.BgGreen,
"magenta": fcolor.BgMagenta,
"red": fcolor.BgRed,
"white": fcolor.BgWhite,
"yellow": fcolor.BgYellow,
2019-06-09 06:18:07 +00:00
}
2021-09-04 21:16:49 +00:00
var GocuiColorschemeColorsMap = map[string]gocui.Attribute{
2019-06-09 06:18:07 +00:00
"black": gocui.ColorBlack,
"blue": gocui.ColorBlue,
"cyan": gocui.ColorCyan,
"green": gocui.ColorGreen,
"magenta": gocui.ColorMagenta,
"red": gocui.ColorRed,
"white": gocui.ColorWhite,
"yellow": gocui.ColorYellow,
2019-06-09 01:16:57 +00:00
}
2019-06-09 06:49:26 +00:00
// NewColorscheme ...
2021-09-04 21:16:49 +00:00
func NewColorscheme(colors ColorschemeColors) *Colorscheme {
2019-06-09 06:49:26 +00:00
return &Colorscheme{
colors: colors,
2021-09-04 21:16:49 +00:00
cache: make(ColorCache),
cacheMutex: sync.RWMutex{},
2019-06-09 01:16:57 +00:00
}
}
2019-06-09 06:18:07 +00:00
// BaseFg ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) BaseFg() gocui.Attribute {
2021-09-04 21:16:49 +00:00
return c.GocuiFgColor("base")
2019-06-09 01:16:57 +00:00
}
2019-06-09 06:18:07 +00:00
// BaseBg ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) BaseBg() gocui.Attribute {
2021-09-04 21:16:49 +00:00
return c.GocuiBgColor("base")
2019-06-09 06:18:07 +00:00
}
// Chart ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) Chart(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("chart", a...)
2019-06-09 06:18:07 +00:00
}
// Marketbar ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) Marketbar(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("marketbar", a...)
2019-06-09 06:18:07 +00:00
}
// MarketbarSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MarketbarSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("marketbar")
2019-06-09 06:18:07 +00:00
}
// MarketbarChangeSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MarketbarChangeSprintf() ISprintf {
2019-06-09 06:18:07 +00:00
// NOTE: reusing table styles
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change")
2019-06-09 06:18:07 +00:00
}
// MarketbarChangeDownSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MarketbarChangeDownSprintf() ISprintf {
2019-06-09 06:18:07 +00:00
// NOTE: reusing table styles
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change_down")
2019-06-09 06:18:07 +00:00
}
// MarketbarChangeUpSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MarketbarChangeUpSprintf() ISprintf {
2019-06-09 06:18:07 +00:00
// NOTE: reusing table styles
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change_up")
2019-06-09 06:18:07 +00:00
}
// MarketBarLabelActive ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MarketBarLabelActive(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("marketbar_label_active", a...)
2019-06-09 06:18:07 +00:00
}
// Menu ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) Menu(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("menu", a...)
2019-06-09 06:18:07 +00:00
}
// MenuHeader ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MenuHeader(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("menu_header", a...)
2019-06-09 06:18:07 +00:00
}
// MenuLabel ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MenuLabel(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("menu_label", a...)
2019-06-09 06:18:07 +00:00
}
// MenuLabelActive ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) MenuLabelActive(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("menu_label_active", a...)
2019-06-09 06:18:07 +00:00
}
// Searchbar ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) Searchbar(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("searchbar", a...)
2019-06-09 06:18:07 +00:00
}
// Statusbar ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) Statusbar(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("statusbar", a...)
2019-06-09 06:18:07 +00:00
}
// TableColumnPrice ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnPrice(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_column_price", a...)
2019-06-09 06:18:07 +00:00
}
// TableColumnPriceSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnPriceSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_price")
2019-06-09 06:18:07 +00:00
}
// TableColumnChange ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChange(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_column_change", a...)
2019-06-09 06:18:07 +00:00
}
// TableColumnChangeSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChangeSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change")
2019-06-09 06:18:07 +00:00
}
// TableColumnChangeDown ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChangeDown(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_column_change_down", a...)
2019-06-09 06:18:07 +00:00
}
// TableColumnChangeDownSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChangeDownSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change_down")
2019-06-09 06:18:07 +00:00
}
// TableColumnChangeUp ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChangeUp(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_column_change_up", a...)
2019-06-09 06:18:07 +00:00
}
// TableColumnChangeUpSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableColumnChangeUpSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_column_change_up")
2019-06-09 06:18:07 +00:00
}
// TableHeader ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableHeader(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_header", a...)
2019-06-09 06:18:07 +00:00
}
// TableHeaderSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableHeaderSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_header")
2019-06-09 06:18:07 +00:00
}
// TableHeaderColumnActive ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableHeaderColumnActive(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_header_column_active", a...)
2019-06-09 06:18:07 +00:00
}
// TableHeaderColumnActiveSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableHeaderColumnActiveSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_header_column_active")
2019-06-09 06:18:07 +00:00
}
// TableRow ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableRow(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_row", a...)
2019-06-09 06:18:07 +00:00
}
// TableRowSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableRowSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_row")
2019-06-09 06:18:07 +00:00
}
// TableRowActive ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableRowActive(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_row_active", a...)
2019-06-09 06:18:07 +00:00
}
// TableRowFavorite ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableRowFavorite(a ...interface{}) string {
2021-09-04 21:16:49 +00:00
return c.Color("table_row_favorite", a...)
2019-06-09 06:18:07 +00:00
}
// TableRowFavoriteSprintf ...
2019-06-09 06:49:26 +00:00
func (c *Colorscheme) TableRowFavoriteSprintf() ISprintf {
2021-09-04 21:16:49 +00:00
return c.ToSprintf("table_row_favorite")
2019-06-09 06:18:07 +00:00
}
2020-11-21 22:22:28 +00:00
// Default ...
func (c *Colorscheme) Default(a ...interface{}) string {
return fmt.Sprintf(a[0].(string), a[1:]...)
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToSprintf(name string) ISprintf {
2021-02-05 09:04:09 +00:00
c.cacheMutex.Lock()
defer c.cacheMutex.Unlock()
2019-06-09 01:16:57 +00:00
if cached, ok := c.cache[name]; ok {
2019-06-09 06:18:07 +00:00
return cached
2019-06-09 01:16:57 +00:00
}
2019-06-10 00:51:06 +00:00
var attrs []fcolor.Attribute
2019-06-09 01:16:57 +00:00
if v, ok := c.colors[name+"_fg"].(string); ok {
2021-09-04 21:16:49 +00:00
if fg, ok := c.ToFgAttr(v); ok {
2019-06-10 00:51:06 +00:00
attrs = append(attrs, fg)
2019-06-09 01:16:57 +00:00
}
}
if v, ok := c.colors[name+"_bg"].(string); ok {
2021-09-04 21:16:49 +00:00
if bg, ok := c.ToBgAttr(v); ok {
2019-06-10 00:51:06 +00:00
attrs = append(attrs, bg)
2019-06-09 01:16:57 +00:00
}
}
if v, ok := c.colors[name+"_bold"].(bool); ok {
2021-09-04 21:16:49 +00:00
if bold, ok := c.ToBoldAttr(v); ok {
2019-06-10 00:51:06 +00:00
attrs = append(attrs, bold)
2019-06-09 01:16:57 +00:00
}
}
if v, ok := c.colors[name+"_underline"].(bool); ok {
2021-09-04 21:16:49 +00:00
if underline, ok := c.ToUnderlineAttr(v); ok {
2019-06-10 00:51:06 +00:00
attrs = append(attrs, underline)
2019-06-09 01:16:57 +00:00
}
}
2019-06-10 00:51:06 +00:00
c.cache[name] = fcolor.New(attrs...).SprintFunc()
2019-06-09 06:18:07 +00:00
return c.cache[name]
2019-06-09 01:16:57 +00:00
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) Color(name string, a ...interface{}) string {
return c.ToSprintf(name)(a...)
2019-06-09 01:16:57 +00:00
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
2021-05-01 20:14:17 +00:00
var attrs []gocui.Attribute
2019-06-09 06:18:07 +00:00
if v, ok := c.colors[name+"_fg"].(string); ok {
2021-09-04 21:16:49 +00:00
if fg, ok := c.ToGocuiAttr(v); ok {
2021-05-01 20:14:17 +00:00
attrs = append(attrs, fg)
}
}
if v, ok := c.colors[name+"_bold"].(bool); ok {
if v {
attrs = append(attrs, gocui.AttrBold)
}
}
if v, ok := c.colors[name+"_underline"].(bool); ok {
if v {
attrs = append(attrs, gocui.AttrUnderline)
}
}
if len(attrs) > 0 {
var combined gocui.Attribute
for _, v := range attrs {
combined = combined ^ v
2019-06-09 06:18:07 +00:00
}
2021-05-01 20:14:17 +00:00
return combined
2019-06-09 06:18:07 +00:00
}
return gocui.ColorDefault
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) GocuiBgColor(name string) gocui.Attribute {
2019-06-09 06:18:07 +00:00
if v, ok := c.colors[name+"_bg"].(string); ok {
2021-09-04 21:16:49 +00:00
if bg, ok := c.ToGocuiAttr(v); ok {
2019-06-09 06:18:07 +00:00
return bg
}
}
return gocui.ColorDefault
2019-06-09 01:16:57 +00:00
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToFgAttr(v string) (fcolor.Attribute, bool) {
if attr, ok := FgColorschemeColorsMap[v]; ok {
2019-06-10 00:51:06 +00:00
return attr, true
}
2020-07-27 07:26:20 +00:00
if code, ok := HexToAnsi(v); ok {
2019-06-10 00:51:06 +00:00
return fcolor.Attribute(code), true
}
return 0, false
}
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToBgAttr(v string) (fcolor.Attribute, bool) {
if attr, ok := BgColorschemeColorsMap[v]; ok {
2019-06-10 00:51:06 +00:00
return attr, true
}
2020-07-27 07:26:20 +00:00
if code, ok := HexToAnsi(v); ok {
2019-06-10 00:51:06 +00:00
return fcolor.Attribute(code), true
}
return 0, false
2019-06-09 01:16:57 +00:00
}
// ToBoldAttr converts a boolean to an Attribute type
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToBoldAttr(v bool) (fcolor.Attribute, bool) {
2019-06-10 00:51:06 +00:00
return fcolor.Bold, v
2019-06-09 01:16:57 +00:00
}
// ToUnderlineAttr converts a boolean to an Attribute type
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToUnderlineAttr(v bool) (fcolor.Attribute, bool) {
2019-06-10 00:51:06 +00:00
return fcolor.Underline, v
2019-06-09 01:16:57 +00:00
}
// ToGocuiAttr converts a color string name to a gocui Attribute type
2021-09-04 21:16:49 +00:00
func (c *Colorscheme) ToGocuiAttr(v string) (gocui.Attribute, bool) {
if attr, ok := GocuiColorschemeColorsMap[v]; ok {
2019-06-10 00:51:06 +00:00
return attr, true
}
2020-07-27 07:26:20 +00:00
if code, ok := HexToAnsi(v); ok {
2019-06-10 00:51:06 +00:00
return gocui.Attribute(code), true
}
return 0, false
2019-06-09 01:16:57 +00:00
}
2020-07-27 07:26:20 +00:00
// HexToAnsi converts a hex color string to a uint8 ansi code
func HexToAnsi(h string) (uint8, bool) {
if h == "" {
return 0, false
}
n, err := strconv.Atoi(h)
if err == nil {
if n <= 255 {
return uint8(n), true
}
}
2019-06-10 00:51:06 +00:00
code, err := xtermcolor.FromHexStr(h)
if err != nil {
return 0, false
}
return code, true
2019-06-09 06:18:07 +00:00
}
// gocui can use xterm colors