Add colorscheme command line flag

pull/38/head
Miguel Mota 5 years ago
parent 12d56decc3
commit 1308bec565
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -19,7 +19,7 @@ debug:
DEBUG=1 go run main.go DEBUG=1 go run main.go
build: build:
@go build main.go @go build -o bin/cointop main.go
# http://macappstore.org/upx # http://macappstore.org/upx
build/mac: clean/mac build/mac: clean/mac

@ -10,7 +10,7 @@ import (
// Run ... // Run ...
func Run() { func Run() {
var v, ver, test, clean, reset bool var v, ver, test, clean, reset bool
var config, cmcAPIKey, apiChoice string var config, cmcAPIKey, apiChoice, colorscheme string
flag.BoolVar(&v, "v", false, "Version") flag.BoolVar(&v, "v", false, "Version")
flag.BoolVar(&ver, "version", false, "Version") flag.BoolVar(&ver, "version", false, "Version")
flag.BoolVar(&test, "test", false, "Run test") flag.BoolVar(&test, "test", false, "Run test")
@ -19,6 +19,7 @@ func Run() {
flag.StringVar(&config, "config", "", "Config filepath") flag.StringVar(&config, "config", "", "Config filepath")
flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "CoinMarketCap API key") flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "CoinMarketCap API key")
flag.StringVar(&apiChoice, "api", cointop.CoinGecko, "API choice") flag.StringVar(&apiChoice, "api", cointop.CoinGecko, "API choice")
flag.StringVar(&colorscheme, "colorscheme", "", "Colorscheme name")
flag.Parse() flag.Parse()
if v || ver { if v || ver {
fmt.Printf("cointop v%s", cointop.Version()) fmt.Printf("cointop v%s", cointop.Version())
@ -33,6 +34,7 @@ func Run() {
ConfigFilepath: config, ConfigFilepath: config,
CoinMarketCapAPIKey: cmcAPIKey, CoinMarketCapAPIKey: cmcAPIKey,
APIChoice: apiChoice, APIChoice: apiChoice,
Colorscheme: colorscheme,
}).Run() }).Run()
} }
} }

@ -29,7 +29,7 @@ type Cointop struct {
g *gocui.Gui g *gocui.Gui
apiChoice string apiChoice string
colorschemename string colorschemename string
colorscheme *ColorScheme colorscheme *Colorscheme
marketbarviewname string marketbarviewname string
marketbarview *gocui.View marketbarview *gocui.View
chartview *gocui.View chartview *gocui.View
@ -115,6 +115,7 @@ type portfolio struct {
// Config config options // Config config options
type Config struct { type Config struct {
APIChoice string APIChoice string
Colorscheme string
ConfigFilepath string ConfigFilepath string
CoinMarketCapAPIKey string CoinMarketCapAPIKey string
NoPrompts bool NoPrompts bool
@ -126,6 +127,7 @@ type apiKeys struct {
} }
var defaultConfigPath = "~/.cointop/config.toml" var defaultConfigPath = "~/.cointop/config.toml"
var defaultColorscheme = "cointop"
// NewCointop initializes cointop // NewCointop initializes cointop
func NewCointop(config *Config) *Cointop { func NewCointop(config *Config) *Cointop {
@ -233,6 +235,16 @@ func NewCointop(config *Config) *Cointop {
} }
} }
if config.Colorscheme != "" {
ct.colorschemename = config.Colorscheme
}
colors, err := ct.getColorschemeColors()
if err != nil {
log.Fatal(err)
}
ct.colorscheme = NewColorscheme(colors)
if config.APIChoice != "" { if config.APIChoice != "" {
ct.apiChoice = config.APIChoice ct.apiChoice = config.APIChoice
if err := ct.saveConfig(); err != nil { if err := ct.saveConfig(); err != nil {

@ -14,8 +14,8 @@ type ISprintf func(...interface{}) string
// colorCache .. // colorCache ..
type colorCache map[string]ISprintf type colorCache map[string]ISprintf
// ColorScheme ... // Colorscheme ...
type ColorScheme struct { type Colorscheme struct {
colors colorschemeColors colors colorschemeColors
cache colorCache cache colorCache
} }
@ -53,190 +53,190 @@ var gocuicolorschemeColorsMap = map[string]gocui.Attribute{
"yellow": gocui.ColorYellow, "yellow": gocui.ColorYellow,
} }
// NewColorScheme ... // NewColorscheme ...
func NewColorScheme(colors colorschemeColors) *ColorScheme { func NewColorscheme(colors colorschemeColors) *Colorscheme {
return &ColorScheme{ return &Colorscheme{
colors: colors, colors: colors,
cache: make(colorCache), cache: make(colorCache),
} }
} }
// BaseFg ... // BaseFg ...
func (c *ColorScheme) BaseFg() gocui.Attribute { func (c *Colorscheme) BaseFg() gocui.Attribute {
return c.gocuiFgColor("base") return c.gocuiFgColor("base")
} }
// BaseBg ... // BaseBg ...
func (c *ColorScheme) BaseBg() gocui.Attribute { func (c *Colorscheme) BaseBg() gocui.Attribute {
return c.gocuiBgColor("base") return c.gocuiBgColor("base")
} }
// Chart ... // Chart ...
func (c *ColorScheme) Chart(a ...interface{}) string { func (c *Colorscheme) Chart(a ...interface{}) string {
return c.color("chart", a...) return c.color("chart", a...)
} }
// Marketbar ... // Marketbar ...
func (c *ColorScheme) Marketbar(a ...interface{}) string { func (c *Colorscheme) Marketbar(a ...interface{}) string {
return c.color("marketbar", a...) return c.color("marketbar", a...)
} }
// MarketbarSprintf ... // MarketbarSprintf ...
func (c *ColorScheme) MarketbarSprintf() ISprintf { func (c *Colorscheme) MarketbarSprintf() ISprintf {
return c.toSprintf("marketbar") return c.toSprintf("marketbar")
} }
// MarketbarChangeSprintf ... // MarketbarChangeSprintf ...
func (c *ColorScheme) MarketbarChangeSprintf() ISprintf { func (c *Colorscheme) MarketbarChangeSprintf() ISprintf {
// NOTE: reusing table styles // NOTE: reusing table styles
return c.toSprintf("table_column_change") return c.toSprintf("table_column_change")
} }
// MarketbarChangeDownSprintf ... // MarketbarChangeDownSprintf ...
func (c *ColorScheme) MarketbarChangeDownSprintf() ISprintf { func (c *Colorscheme) MarketbarChangeDownSprintf() ISprintf {
// NOTE: reusing table styles // NOTE: reusing table styles
return c.toSprintf("table_column_change_down") return c.toSprintf("table_column_change_down")
} }
// MarketbarChangeUpSprintf ... // MarketbarChangeUpSprintf ...
func (c *ColorScheme) MarketbarChangeUpSprintf() ISprintf { func (c *Colorscheme) MarketbarChangeUpSprintf() ISprintf {
// NOTE: reusing table styles // NOTE: reusing table styles
return c.toSprintf("table_column_change_up") return c.toSprintf("table_column_change_up")
} }
// MarketBarLabelActive ... // MarketBarLabelActive ...
func (c *ColorScheme) MarketBarLabelActive(a ...interface{}) string { func (c *Colorscheme) MarketBarLabelActive(a ...interface{}) string {
return c.color("marketbar_label_active", a...) return c.color("marketbar_label_active", a...)
} }
// Menu ... // Menu ...
func (c *ColorScheme) Menu(a ...interface{}) string { func (c *Colorscheme) Menu(a ...interface{}) string {
return c.color("menu", a...) return c.color("menu", a...)
} }
// MenuHeader ... // MenuHeader ...
func (c *ColorScheme) MenuHeader(a ...interface{}) string { func (c *Colorscheme) MenuHeader(a ...interface{}) string {
return c.color("menu_header", a...) return c.color("menu_header", a...)
} }
// MenuLabel ... // MenuLabel ...
func (c *ColorScheme) MenuLabel(a ...interface{}) string { func (c *Colorscheme) MenuLabel(a ...interface{}) string {
return c.color("menu_label", a...) return c.color("menu_label", a...)
} }
// MenuLabelActive ... // MenuLabelActive ...
func (c *ColorScheme) MenuLabelActive(a ...interface{}) string { func (c *Colorscheme) MenuLabelActive(a ...interface{}) string {
return c.color("menu_label_active", a...) return c.color("menu_label_active", a...)
} }
// Searchbar ... // Searchbar ...
func (c *ColorScheme) Searchbar(a ...interface{}) string { func (c *Colorscheme) Searchbar(a ...interface{}) string {
return c.color("searchbar", a...) return c.color("searchbar", a...)
} }
// Statusbar ... // Statusbar ...
func (c *ColorScheme) Statusbar(a ...interface{}) string { func (c *Colorscheme) Statusbar(a ...interface{}) string {
return c.color("statusbar", a...) return c.color("statusbar", a...)
} }
// TableColumnPrice ... // TableColumnPrice ...
func (c *ColorScheme) TableColumnPrice(a ...interface{}) string { func (c *Colorscheme) TableColumnPrice(a ...interface{}) string {
return c.color("table_column_price", a...) return c.color("table_column_price", a...)
} }
// TableColumnPriceSprintf ... // TableColumnPriceSprintf ...
func (c *ColorScheme) TableColumnPriceSprintf() ISprintf { func (c *Colorscheme) TableColumnPriceSprintf() ISprintf {
return c.toSprintf("table_column_price") return c.toSprintf("table_column_price")
} }
// TableColumnChange ... // TableColumnChange ...
func (c *ColorScheme) TableColumnChange(a ...interface{}) string { func (c *Colorscheme) TableColumnChange(a ...interface{}) string {
return c.color("table_column_change", a...) return c.color("table_column_change", a...)
} }
// TableColumnChangeSprintf ... // TableColumnChangeSprintf ...
func (c *ColorScheme) TableColumnChangeSprintf() ISprintf { func (c *Colorscheme) TableColumnChangeSprintf() ISprintf {
return c.toSprintf("table_column_change") return c.toSprintf("table_column_change")
} }
// TableColumnChangeDown ... // TableColumnChangeDown ...
func (c *ColorScheme) TableColumnChangeDown(a ...interface{}) string { func (c *Colorscheme) TableColumnChangeDown(a ...interface{}) string {
return c.color("table_column_change_down", a...) return c.color("table_column_change_down", a...)
} }
// TableColumnChangeDownSprintf ... // TableColumnChangeDownSprintf ...
func (c *ColorScheme) TableColumnChangeDownSprintf() ISprintf { func (c *Colorscheme) TableColumnChangeDownSprintf() ISprintf {
return c.toSprintf("table_column_change_down") return c.toSprintf("table_column_change_down")
} }
// TableColumnChangeUp ... // TableColumnChangeUp ...
func (c *ColorScheme) TableColumnChangeUp(a ...interface{}) string { func (c *Colorscheme) TableColumnChangeUp(a ...interface{}) string {
return c.color("table_column_change_up", a...) return c.color("table_column_change_up", a...)
} }
// TableColumnChangeUpSprintf ... // TableColumnChangeUpSprintf ...
func (c *ColorScheme) TableColumnChangeUpSprintf() ISprintf { func (c *Colorscheme) TableColumnChangeUpSprintf() ISprintf {
return c.toSprintf("table_column_change_up") return c.toSprintf("table_column_change_up")
} }
// TableHeader ... // TableHeader ...
func (c *ColorScheme) TableHeader(a ...interface{}) string { func (c *Colorscheme) TableHeader(a ...interface{}) string {
return c.color("table_header", a...) return c.color("table_header", a...)
} }
// TableHeaderSprintf ... // TableHeaderSprintf ...
func (c *ColorScheme) TableHeaderSprintf() ISprintf { func (c *Colorscheme) TableHeaderSprintf() ISprintf {
return c.toSprintf("table_header") return c.toSprintf("table_header")
} }
// TableHeaderColumnActive ... // TableHeaderColumnActive ...
func (c *ColorScheme) TableHeaderColumnActive(a ...interface{}) string { func (c *Colorscheme) TableHeaderColumnActive(a ...interface{}) string {
return c.color("table_header_column_active", a...) return c.color("table_header_column_active", a...)
} }
// TableHeaderColumnActiveSprintf ... // TableHeaderColumnActiveSprintf ...
func (c *ColorScheme) TableHeaderColumnActiveSprintf() ISprintf { func (c *Colorscheme) TableHeaderColumnActiveSprintf() ISprintf {
return c.toSprintf("table_header_column_active") return c.toSprintf("table_header_column_active")
} }
// TableRow ... // TableRow ...
func (c *ColorScheme) TableRow(a ...interface{}) string { func (c *Colorscheme) TableRow(a ...interface{}) string {
return c.color("table_row", a...) return c.color("table_row", a...)
} }
// TableRowSprintf ... // TableRowSprintf ...
func (c *ColorScheme) TableRowSprintf() ISprintf { func (c *Colorscheme) TableRowSprintf() ISprintf {
return c.toSprintf("table_row") return c.toSprintf("table_row")
} }
// TableRowActive ... // TableRowActive ...
func (c *ColorScheme) TableRowActive(a ...interface{}) string { func (c *Colorscheme) TableRowActive(a ...interface{}) string {
return c.color("table_row_active", a...) return c.color("table_row_active", a...)
} }
// TableRowFavorite ... // TableRowFavorite ...
func (c *ColorScheme) TableRowFavorite(a ...interface{}) string { func (c *Colorscheme) TableRowFavorite(a ...interface{}) string {
return c.color("table_row_favorite", a...) return c.color("table_row_favorite", a...)
} }
// TableRowFavoriteSprintf ... // TableRowFavoriteSprintf ...
func (c *ColorScheme) TableRowFavoriteSprintf() ISprintf { func (c *Colorscheme) TableRowFavoriteSprintf() ISprintf {
return c.toSprintf("table_row_favorite") return c.toSprintf("table_row_favorite")
} }
// SetViewColor ... // SetViewColor ...
func (c *ColorScheme) SetViewColor(view *gocui.View, name string) { func (c *Colorscheme) SetViewColor(view *gocui.View, name string) {
view.FgColor = c.gocuiFgColor(name) view.FgColor = c.gocuiFgColor(name)
view.BgColor = c.gocuiBgColor(name) view.BgColor = c.gocuiBgColor(name)
} }
// SetViewActiveColor ... // SetViewActiveColor ...
func (c *ColorScheme) SetViewActiveColor(view *gocui.View, name string) { func (c *Colorscheme) SetViewActiveColor(view *gocui.View, name string) {
view.SelFgColor = c.gocuiFgColor(name) view.SelFgColor = c.gocuiFgColor(name)
view.SelBgColor = c.gocuiBgColor(name) view.SelBgColor = c.gocuiBgColor(name)
} }
func (c *ColorScheme) toSprintf(name string) ISprintf { func (c *Colorscheme) toSprintf(name string) ISprintf {
if cached, ok := c.cache[name]; ok { if cached, ok := c.cache[name]; ok {
return cached return cached
} }
@ -267,11 +267,11 @@ func (c *ColorScheme) toSprintf(name string) ISprintf {
return c.cache[name] return c.cache[name]
} }
func (c *ColorScheme) color(name string, a ...interface{}) string { func (c *Colorscheme) color(name string, a ...interface{}) string {
return c.toSprintf(name)(a...) return c.toSprintf(name)(a...)
} }
func (c *ColorScheme) gocuiFgColor(name string) gocui.Attribute { func (c *Colorscheme) gocuiFgColor(name string) gocui.Attribute {
if v, ok := c.colors[name+"_fg"].(string); ok { if v, ok := c.colors[name+"_fg"].(string); ok {
if fg, ok := c.toGocuiAttr(v); ok { if fg, ok := c.toGocuiAttr(v); ok {
return fg return fg
@ -281,7 +281,7 @@ func (c *ColorScheme) gocuiFgColor(name string) gocui.Attribute {
return gocui.ColorDefault return gocui.ColorDefault
} }
func (c *ColorScheme) gocuiBgColor(name string) gocui.Attribute { func (c *Colorscheme) gocuiBgColor(name string) gocui.Attribute {
if v, ok := c.colors[name+"_bg"].(string); ok { if v, ok := c.colors[name+"_bg"].(string); ok {
if bg, ok := c.toGocuiAttr(v); ok { if bg, ok := c.toGocuiAttr(v); ok {
return bg return bg
@ -291,25 +291,25 @@ func (c *ColorScheme) gocuiBgColor(name string) gocui.Attribute {
return gocui.ColorDefault return gocui.ColorDefault
} }
func (c *ColorScheme) toFgAttr(k string) (color.Attribute, bool) { func (c *Colorscheme) toFgAttr(k string) (color.Attribute, bool) {
attr, ok := fgcolorschemeColorsMap[k] attr, ok := fgcolorschemeColorsMap[k]
return attr, ok return attr, ok
} }
func (c *ColorScheme) toBgAttr(k string) (color.Attribute, bool) { func (c *Colorscheme) toBgAttr(k string) (color.Attribute, bool) {
attr, ok := bgcolorschemeColorsMap[k] attr, ok := bgcolorschemeColorsMap[k]
return attr, ok return attr, ok
} }
func (c *ColorScheme) toBoldAttr(v bool) (color.Attribute, bool) { func (c *Colorscheme) toBoldAttr(v bool) (color.Attribute, bool) {
return color.Bold, v return color.Bold, v
} }
func (c *ColorScheme) toUnderlineAttr(v bool) (color.Attribute, bool) { func (c *Colorscheme) toUnderlineAttr(v bool) (color.Attribute, bool) {
return color.Underline, v return color.Underline, v
} }
func (c *ColorScheme) toGocuiAttr(k string) (gocui.Attribute, bool) { func (c *Colorscheme) toGocuiAttr(k string) (gocui.Attribute, bool) {
attr, ok := gocuicolorschemeColorsMap[k] attr, ok := gocuicolorschemeColorsMap[k]
return attr, ok return attr, ok
} }

@ -20,7 +20,7 @@ type config struct {
DefaultView interface{} `toml:"defaultView"` DefaultView interface{} `toml:"defaultView"`
CoinMarketCap map[string]interface{} `toml:"coinmarketcap"` CoinMarketCap map[string]interface{} `toml:"coinmarketcap"`
API interface{} `toml:"api"` API interface{} `toml:"api"`
ColorScheme interface{} `toml:"colorscheme"` Colorscheme interface{} `toml:"colorscheme"`
} }
func (ct *Cointop) setupConfig() error { func (ct *Cointop) setupConfig() error {
@ -51,7 +51,7 @@ func (ct *Cointop) setupConfig() error {
if err := ct.loadAPIChoiceFromConfig(); err != nil { if err := ct.loadAPIChoiceFromConfig(); err != nil {
return err return err
} }
if err := ct.loadColorSchemeFromConfig(); err != nil { if err := ct.loadColorschemeFromConfig(); err != nil {
return err return err
} }
@ -189,7 +189,7 @@ func (ct *Cointop) configToToml() ([]byte, error) {
var inputs = &config{ var inputs = &config{
API: apiChoiceIfc, API: apiChoiceIfc,
ColorScheme: colorschemeIfc, Colorscheme: colorschemeIfc,
CoinMarketCap: cmcIfc, CoinMarketCap: cmcIfc,
Currency: currencyIfc, Currency: currencyIfc,
DefaultView: defaultViewIfc, DefaultView: defaultViewIfc,
@ -260,27 +260,42 @@ func (ct *Cointop) loadAPIKeysFromConfig() error {
return nil return nil
} }
func (ct *Cointop) loadColorSchemeFromConfig() error { func (ct *Cointop) loadColorschemeFromConfig() error {
if colorscheme, ok := ct.config.ColorScheme.(string); ok { if colorscheme, ok := ct.config.Colorscheme.(string); ok {
ct.colorschemename = colorscheme ct.colorschemename = colorscheme
} }
return nil
}
func (ct *Cointop) getColorschemeColors() (map[string]interface{}, error) {
var colors map[string]interface{} var colors map[string]interface{}
if ct.colorschemename == "" { if ct.colorschemename == "" {
ct.colorschemename = "cointop" ct.colorschemename = defaultColorscheme
if _, err := toml.Decode(DefaultColors, &colors); err != nil { if _, err := toml.Decode(DefaultColors, &colors); err != nil {
return err return nil, err
} }
} else { } else {
path := normalizePath(fmt.Sprintf("~/.cointop/colors/%s.toml", ct.colorschemename)) path := normalizePath(fmt.Sprintf("~/.cointop/colors/%s.toml", ct.colorschemename))
if _, err := os.Stat(path); os.IsNotExist(err) {
// NOTE: case for when cointop is set as the theme but the colorscheme file doesn't exist
if ct.colorschemename == "cointop" {
if _, err := toml.Decode(DefaultColors, &colors); err != nil {
return nil, err
}
return colors, nil
}
return nil, fmt.Errorf("The colorscheme file %q was not found.\n\nFor help and colorschemes, please visit: https://github.com/cointop-sh/colors", path)
}
if _, err := toml.DecodeFile(path, &colors); err != nil { if _, err := toml.DecodeFile(path, &colors); err != nil {
return err return nil, err
} }
} }
ct.colorscheme = NewColorScheme(colors) return colors, nil
return nil
} }
func (ct *Cointop) loadAPIChoiceFromConfig() error { func (ct *Cointop) loadAPIChoiceFromConfig() error {

@ -1,7 +1,7 @@
package cointop package cointop
// TODO: make dynamic based on git tag // TODO: make dynamic based on git tag
const version = "1.2.2" const version = "1.3.0"
func (ct *Cointop) version() string { func (ct *Cointop) version() string {
return version return version

Loading…
Cancel
Save