1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Further reduce usage of global

Bargraph moves to common settings
"Global" config moves out of wtf and into cfg for the few things that need it
We can probably eliminate a global config used across things if we want to
This commit is contained in:
Sean Smith 2019-04-27 15:08:02 -04:00
parent 5abd701b40
commit bcf899df72
13 changed files with 67 additions and 52 deletions

View File

@ -56,6 +56,7 @@ type Common struct {
Enabled bool
RefreshInterval int
Title string
Config *config.Config
focusChar int
}
@ -93,6 +94,7 @@ func NewCommonSettingsFromModule(name string, moduleConfig *config.Config, globa
Enabled: moduleConfig.UBool("enabled", false),
RefreshInterval: moduleConfig.UInt("refreshInterval", 300),
Title: moduleConfig.UString("title", name),
Config: moduleConfig,
focusChar: moduleConfig.UInt("focusChar", -1),
}

View File

@ -17,6 +17,8 @@ const ConfigDirV1 = "~/.wtf/"
// ConfigDirV2 defines the path to the second version of the configuration. Use this.
const ConfigDirV2 = "~/.config/wtf/"
var Config *config.Config
/* -------------------- Config Migration -------------------- */
// MigrateOldConfig copies any existing configuration from the old location
@ -130,6 +132,7 @@ func LoadConfigFile(filePath string) *config.Config {
fmt.Printf(" %s\n", err.Error())
os.Exit(1)
}
Config = cfg
return cfg
}

19
main.go
View File

@ -12,7 +12,6 @@ import (
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/pkg/profile"
"github.com/radovskyb/watcher"
"github.com/rivo/tview"
@ -25,9 +24,6 @@ import (
var focusTracker wtf.FocusTracker
var runningWidgets []wtf.Wtfable
// Config parses the config.yml file and makes available the settings within
var Config *config.Config
var (
commit = "dev"
date = "dev"
@ -61,11 +57,6 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
return event
}
func loadConfigFile(filePath string) {
Config = cfg.LoadConfigFile(filePath)
wtf.Config = Config
}
func refreshAllWidgets(widgets []wtf.Wtfable) {
for _, widget := range widgets {
go widget.Refresh()
@ -73,7 +64,7 @@ func refreshAllWidgets(widgets []wtf.Wtfable) {
}
func setTerm() {
err := os.Setenv("TERM", Config.UString("wtf.term", os.Getenv("TERM")))
err := os.Setenv("TERM", cfg.Config.UString("wtf.term", os.Getenv("TERM")))
if err != nil {
return
}
@ -93,9 +84,9 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid *
// Disable all widgets to stop scheduler goroutines and remove widgets from memory
disableAllWidgets(runningWidgets)
loadConfigFile(absPath)
config := cfg.LoadConfigFile(absPath)
widgets := maker.MakeWidgets(app, pages, Config)
widgets := maker.MakeWidgets(app, pages, config)
wtf.ValidateWidgets(widgets)
runningWidgets = widgets
@ -134,7 +125,7 @@ func main() {
cfg.MigrateOldConfig()
cfg.CreateConfigDir()
cfg.CreateConfigFile()
loadConfigFile(flags.ConfigFilePath())
config := cfg.LoadConfigFile(flags.ConfigFilePath())
if flags.Profile {
defer profile.Start(profile.MemProfile).Stop()
@ -145,7 +136,7 @@ func main() {
app := tview.NewApplication()
pages := tview.NewPages()
widgets := maker.MakeWidgets(app, pages, Config)
widgets := maker.MakeWidgets(app, pages, config)
wtf.ValidateWidgets(widgets)
runningWidgets = widgets

View File

@ -3,9 +3,9 @@ package maker
import (
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/bargraph"
"github.com/wtfutil/wtf/logger"
"github.com/wtfutil/wtf/modules/bamboohr"
"github.com/wtfutil/wtf/modules/bargraph"
"github.com/wtfutil/wtf/modules/circleci"
"github.com/wtfutil/wtf/modules/clocks"
"github.com/wtfutil/wtf/modules/cmdrunner"
@ -67,7 +67,8 @@ func MakeWidget(
settings := bamboohr.NewSettingsFromYAML("BambooHR", moduleConfig, globalConfig)
widget = bamboohr.NewWidget(app, settings)
case "bargraph":
widget = bargraph.NewWidget(app)
settings := bargraph.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
widget = bargraph.NewWidget(app, settings)
case "bittrex":
settings := bittrex.NewSettingsFromYAML("Bittrex", moduleConfig, globalConfig)
widget = bittrex.NewWidget(app, settings)

View File

@ -0,0 +1,18 @@
package bargraph
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
}
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, ymlConfig, globalConfig),
}
return &settings
}

View File

@ -23,9 +23,9 @@ type Widget struct {
}
// NewWidget Make new instance of widget
func NewWidget(app *tview.Application) *Widget {
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
BarGraph: wtf.NewBarGraph(app, "Sample Bar Graph", "bargraph", false),
BarGraph: wtf.NewBarGraph(app, "Sample Bar Graph", settings.common, false),
app: app,
}

View File

@ -25,7 +25,7 @@ type Widget struct {
// NewWidget Make new instance of widget
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
BarGraph: wtf.NewBarGraph(app, settings.common.Name, settings.common.Name, false),
BarGraph: wtf.NewBarGraph(app, settings.common.Name, settings.common, false),
app: app,
settings: settings,

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
)
//BarGraph lets make graphs
@ -19,6 +20,7 @@ type BarGraph struct {
RefreshInt int
View *tview.TextView
settings *cfg.Common
Position
}
@ -30,25 +32,25 @@ type Bar struct {
}
// NewBarGraph initialize your fancy new graph
func NewBarGraph(app *tview.Application, name string, configKey string, focusable bool) BarGraph {
func NewBarGraph(app *tview.Application, name string, settings *cfg.Common, focusable bool) BarGraph {
widget := BarGraph{
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
enabled: settings.Enabled,
focusable: focusable,
key: configKey,
maxStars: Config.UInt(fmt.Sprintf("wtf.mods.%s.graphStars", configKey), 20),
name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
starChar: Config.UString(fmt.Sprintf("wtf.mods.%s.graphIcon", configKey), "|"),
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey), 1),
maxStars: settings.Config.UInt("graphStars", 20),
name: settings.Title,
starChar: settings.Config.UString("graphIcon", "|"),
RefreshInt: settings.RefreshInterval,
settings: settings,
}
widget.Position = NewPosition(
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.top", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.left", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.width", configKey)),
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.height", configKey)),
settings.Position.Top,
settings.Position.Left,
settings.Position.Width,
settings.Position.Height,
)
widget.View = widget.addView(configKey)
widget.View = widget.addView()
widget.View.SetChangedFunc(func() {
app.Draw()
})
@ -58,10 +60,10 @@ func NewBarGraph(app *tview.Application, name string, configKey string, focusabl
func (widget *BarGraph) BorderColor() string {
if widget.Focusable() {
return Config.UString("wtf.colors.border.focusable", "red")
return widget.settings.Colors.BorderFocusable
}
return Config.UString("wtf.colors.border.normal", "gray")
return widget.settings.Colors.BorderNormal
}
func (widget *BarGraph) Disable() {
@ -112,20 +114,15 @@ func (widget *BarGraph) TextView() *tview.TextView {
/* -------------------- Unexported Functions -------------------- */
func (widget *BarGraph) addView(configKey string) *tview.TextView {
func (widget *BarGraph) addView() *tview.TextView {
view := tview.NewTextView()
view.SetBackgroundColor(ColorFor(Config.UString("wtf.colors.background", "black")))
view.SetBackgroundColor(ColorFor(widget.settings.Colors.Background))
view.SetBorder(true)
view.SetBorderColor(ColorFor(widget.BorderColor()))
view.SetDynamicColors(true)
view.SetTitle(widget.Name())
view.SetTitleColor(ColorFor(
Config.UString(
fmt.Sprintf("wtf.mods.%s.colors.title", configKey),
Config.UString("wtf.colors.title", "white"),
),
))
view.SetTitleColor(ColorFor(widget.settings.Colors.Title))
view.SetWrap(false)
return view

View File

@ -2,6 +2,7 @@ package wtf
import (
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
)
type Display struct {
@ -14,7 +15,7 @@ func NewDisplay(widgets []Wtfable) *Display {
}
display.build(widgets)
display.Grid.SetBackgroundColor(ColorFor(Config.UString("wtf.colors.background", "black")))
display.Grid.SetBackgroundColor(ColorFor(cfg.Config.UString("wtf.colors.background", "black")))
return &display
}
@ -43,8 +44,8 @@ func (display *Display) add(widget Wtfable) {
}
func (display *Display) build(widgets []Wtfable) *tview.Grid {
display.Grid.SetColumns(ToInts(Config.UList("wtf.grid.columns"))...)
display.Grid.SetRows(ToInts(Config.UList("wtf.grid.rows"))...)
display.Grid.SetColumns(ToInts(cfg.Config.UList("wtf.grid.columns"))...)
display.Grid.SetRows(ToInts(cfg.Config.UList("wtf.grid.rows"))...)
display.Grid.SetBorder(false)
for _, widget := range widgets {

View File

@ -2,6 +2,7 @@ package wtf
import (
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
)
type FocusState int
@ -161,7 +162,7 @@ func (tracker *FocusTracker) focus(idx int) {
}
view := widget.TextView()
view.SetBorderColor(ColorFor(Config.UString("wtf.colors.border.focused", "gray")))
view.SetBorderColor(ColorFor(cfg.Config.UString("wtf.colors.border.focused", "gray")))
tracker.App.SetFocus(view)
}
@ -208,5 +209,5 @@ func (tracker *FocusTracker) increment() {
}
func (tracker *FocusTracker) useNavShortcuts() bool {
return Config.UBool("wtf.navigation.shortcuts", true)
return cfg.Config.UBool("wtf.navigation.shortcuts", true)
}

View File

@ -2,6 +2,8 @@ package wtf
import (
"fmt"
"github.com/wtfutil/wtf/cfg"
)
type MultiSourceWidget struct {
@ -38,8 +40,8 @@ func (widget *MultiSourceWidget) LoadSources() {
s := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.singular)
p := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.plural)
single := Config.UString(s, "")
multiple := Config.UList(p, empty)
single := cfg.Config.UString(s, "")
multiple := cfg.Config.UList(p, empty)
asStrs := ToStrs(multiple)

View File

@ -3,13 +3,10 @@ package wtf
import (
"fmt"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
)
var Config *config.Config
type TextWidget struct {
enabled bool
focusable bool

View File

@ -7,6 +7,8 @@ import (
"regexp"
"runtime"
"strings"
"github.com/wtfutil/wtf/cfg"
)
const SimpleDateFormat = "Jan 2"
@ -91,7 +93,7 @@ func OpenFile(path string) {
}
} else {
filePath, _ := ExpandHomeDir(path)
openFileUtil := Config.UString("wtf.openFileUtil", "open")
openFileUtil := cfg.Config.UString("wtf.openFileUtil", "open")
cmd := exec.Command(openFileUtil, filePath)
ExecuteCommand(cmd)
}