Skip to content

Commit

Permalink
WTF-400 Bittrex extracted to new config format
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Cummer committed Apr 15, 2019
1 parent fbf8944 commit 3db2848
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 40 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
case "bargraph":
widget = bargraph.NewWidget(app)
case "bittrex":
widget = bittrex.NewWidget(app)
cfg := bittrex.NewSettingsFromYAML(wtf.Config)
widget = bittrex.NewWidget(app, cfg)
case "blockfolio":
widget = blockfolio.NewWidget(app)
case "circleci":
Expand Down
19 changes: 13 additions & 6 deletions modules/cryptoexchanges/bittrex/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ func (widget *Widget) display() {
return
}

widget.View.SetText(summaryText(&widget.summaryList, &widget.TextColors))
summaryText := widget.summaryText(&widget.summaryList)
widget.View.SetText(summaryText)
}

func summaryText(list *summaryList, colors *TextColors) string {
func (widget *Widget) summaryText(list *summaryList) string {
str := ""

for _, baseCurrency := range list.items {
str += fmt.Sprintf(" [%s]%s[%s] (%s)\n\n", colors.base.displayName, baseCurrency.displayName, colors.base.name, baseCurrency.name)
str += fmt.Sprintf(
" [%s]%s[%s] (%s)\n\n",
widget.settings.colors.base.displayName,
baseCurrency.displayName,
widget.settings.colors.base.name,
baseCurrency.name,
)

resultTemplate := template.New("bittrex")

Expand All @@ -38,9 +45,9 @@ func summaryText(list *summaryList, colors *TextColors) string {
)

strTemplate.Execute(writer, map[string]string{
"nameColor": colors.market.name,
"fieldColor": colors.market.field,
"valueColor": colors.market.value,
"nameColor": widget.settings.colors.market.name,
"fieldColor": widget.settings.colors.market.field,
"valueColor": widget.settings.colors.market.value,
"mName": marketCurrency.name,
"High": marketCurrency.High,
"Low": marketCurrency.Low,
Expand Down
45 changes: 45 additions & 0 deletions modules/cryptoexchanges/bittrex/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bittrex

import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)

type colors struct {
base struct {
name string
displayName string
}
market struct {
name string
field string
value string
}
}

type Settings struct {
colors
common *cfg.Common

summary map[string]interface{}
}

func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.bittrex")

settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
}

settings.colors.base.name = localConfig.UString("colors.base.name")
settings.colors.base.displayName = localConfig.UString("colors.base.displayName")

settings.colors.market.name = localConfig.UString("colors.market.name")
settings.colors.market.field = localConfig.UString("colors.market.field")
settings.colors.market.value = localConfig.UString("colors.market.value")

summaryMap, _ := ymlConfig.Map("summary")
settings.summary = summaryMap

return &settings
}
34 changes: 8 additions & 26 deletions modules/cryptoexchanges/bittrex/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,38 @@ import (
"github.com/wtfutil/wtf/wtf"
)

type TextColors struct {
base struct {
name string
displayName string
}
market struct {
name string
field string
value string
}
}

var ok = true
var errorText = ""

var baseURL = "https://bittrex.com/api/v1.1/public/getmarketsummary"
const baseURL = "https://bittrex.com/api/v1.1/public/getmarketsummary"

// Widget define wtf widget to register widget later
type Widget struct {
wtf.TextWidget

settings *Settings
summaryList
TextColors
}

// NewWidget Make new instance of widget
func NewWidget(app *tview.Application) *Widget {
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Bittrex", "bittrex", false),
TextWidget: wtf.NewTextWidget(app, "Bittrex", "bittrex", false),

settings: settings,
summaryList: summaryList{},
}

ok = true
errorText = ""

widget.config()
widget.setSummaryList()

return &widget
}

func (widget *Widget) config() {
widget.TextColors.base.name = wtf.Config.UString("wtf.mods.bittrex.colors.base.name", "red")
widget.TextColors.base.displayName = wtf.Config.UString("wtf.mods.bittrex.colors.base.displayName", "grey")
widget.TextColors.market.name = wtf.Config.UString("wtf.mods.bittrex.colors.market.name", "red")
widget.TextColors.market.field = wtf.Config.UString("wtf.mods.bittrex.colors.market.field", "coral")
widget.TextColors.market.value = wtf.Config.UString("wtf.mods.bittrex.colors.market.value", "white")
}

func (widget *Widget) setSummaryList() {
sCurrencies, _ := wtf.Config.Map("wtf.mods.bittrex.summary")
sCurrencies := widget.settings.summary
for baseCurrencyName := range sCurrencies {
displayName, _ := wtf.Config.String("wtf.mods.bittrex.summary." + baseCurrencyName + ".displayName")
mCurrencyList := makeSummaryMarketList(baseCurrencyName)
Expand Down
8 changes: 4 additions & 4 deletions modules/todo/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

type Settings struct {
Common *cfg.Common
common *cfg.Common

FilePath string
filePath string
}

func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.todo")

settings := Settings{
Common: cfg.NewCommonSettingsFromYAML(ymlConfig),
FilePath: localConfig.UString("filename"),
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
filePath: localConfig.UString("filename"),
}

return &settings
Expand Down
6 changes: 3 additions & 3 deletions modules/todo/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *

app: app,
settings: settings,
filePath: settings.FilePath,
filePath: settings.filePath,
list: checklist.NewChecklist(),
pages: pages,
}
Expand Down Expand Up @@ -257,8 +257,8 @@ func (widget *Widget) modalFocus(form *tview.Form) {
}

func (widget *Widget) modalForm(lbl, text string) *tview.Form {
form := tview.NewForm().SetFieldBackgroundColor(wtf.ColorFor(widget.settings.Common.Colors.Background))
form.SetButtonsAlign(tview.AlignCenter).SetButtonTextColor(wtf.ColorFor(widget.settings.Common.Colors.Text))
form := tview.NewForm().SetFieldBackgroundColor(wtf.ColorFor(widget.settings.common.Colors.Background))
form.SetButtonsAlign(tview.AlignCenter).SetButtonTextColor(wtf.ColorFor(widget.settings.common.Colors.Text))

form.AddInputField(lbl, text, 60, nil, nil)

Expand Down

0 comments on commit 3db2848

Please sign in to comment.