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:
parent
5abd701b40
commit
bcf899df72
@ -56,6 +56,7 @@ type Common struct {
|
|||||||
Enabled bool
|
Enabled bool
|
||||||
RefreshInterval int
|
RefreshInterval int
|
||||||
Title string
|
Title string
|
||||||
|
Config *config.Config
|
||||||
|
|
||||||
focusChar int
|
focusChar int
|
||||||
}
|
}
|
||||||
@ -93,6 +94,7 @@ func NewCommonSettingsFromModule(name string, moduleConfig *config.Config, globa
|
|||||||
Enabled: moduleConfig.UBool("enabled", false),
|
Enabled: moduleConfig.UBool("enabled", false),
|
||||||
RefreshInterval: moduleConfig.UInt("refreshInterval", 300),
|
RefreshInterval: moduleConfig.UInt("refreshInterval", 300),
|
||||||
Title: moduleConfig.UString("title", name),
|
Title: moduleConfig.UString("title", name),
|
||||||
|
Config: moduleConfig,
|
||||||
|
|
||||||
focusChar: moduleConfig.UInt("focusChar", -1),
|
focusChar: moduleConfig.UInt("focusChar", -1),
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ const ConfigDirV1 = "~/.wtf/"
|
|||||||
// ConfigDirV2 defines the path to the second version of the configuration. Use this.
|
// ConfigDirV2 defines the path to the second version of the configuration. Use this.
|
||||||
const ConfigDirV2 = "~/.config/wtf/"
|
const ConfigDirV2 = "~/.config/wtf/"
|
||||||
|
|
||||||
|
var Config *config.Config
|
||||||
|
|
||||||
/* -------------------- Config Migration -------------------- */
|
/* -------------------- Config Migration -------------------- */
|
||||||
|
|
||||||
// MigrateOldConfig copies any existing configuration from the old location
|
// 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())
|
fmt.Printf(" %s\n", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Config = cfg
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
19
main.go
19
main.go
@ -12,7 +12,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/olebedev/config"
|
|
||||||
"github.com/pkg/profile"
|
"github.com/pkg/profile"
|
||||||
"github.com/radovskyb/watcher"
|
"github.com/radovskyb/watcher"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@ -25,9 +24,6 @@ import (
|
|||||||
var focusTracker wtf.FocusTracker
|
var focusTracker wtf.FocusTracker
|
||||||
var runningWidgets []wtf.Wtfable
|
var runningWidgets []wtf.Wtfable
|
||||||
|
|
||||||
// Config parses the config.yml file and makes available the settings within
|
|
||||||
var Config *config.Config
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
commit = "dev"
|
commit = "dev"
|
||||||
date = "dev"
|
date = "dev"
|
||||||
@ -61,11 +57,6 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfigFile(filePath string) {
|
|
||||||
Config = cfg.LoadConfigFile(filePath)
|
|
||||||
wtf.Config = Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func refreshAllWidgets(widgets []wtf.Wtfable) {
|
func refreshAllWidgets(widgets []wtf.Wtfable) {
|
||||||
for _, widget := range widgets {
|
for _, widget := range widgets {
|
||||||
go widget.Refresh()
|
go widget.Refresh()
|
||||||
@ -73,7 +64,7 @@ func refreshAllWidgets(widgets []wtf.Wtfable) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setTerm() {
|
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 {
|
if err != nil {
|
||||||
return
|
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
|
// Disable all widgets to stop scheduler goroutines and remove widgets from memory
|
||||||
disableAllWidgets(runningWidgets)
|
disableAllWidgets(runningWidgets)
|
||||||
|
|
||||||
loadConfigFile(absPath)
|
config := cfg.LoadConfigFile(absPath)
|
||||||
|
|
||||||
widgets := maker.MakeWidgets(app, pages, Config)
|
widgets := maker.MakeWidgets(app, pages, config)
|
||||||
wtf.ValidateWidgets(widgets)
|
wtf.ValidateWidgets(widgets)
|
||||||
runningWidgets = widgets
|
runningWidgets = widgets
|
||||||
|
|
||||||
@ -134,7 +125,7 @@ func main() {
|
|||||||
cfg.MigrateOldConfig()
|
cfg.MigrateOldConfig()
|
||||||
cfg.CreateConfigDir()
|
cfg.CreateConfigDir()
|
||||||
cfg.CreateConfigFile()
|
cfg.CreateConfigFile()
|
||||||
loadConfigFile(flags.ConfigFilePath())
|
config := cfg.LoadConfigFile(flags.ConfigFilePath())
|
||||||
|
|
||||||
if flags.Profile {
|
if flags.Profile {
|
||||||
defer profile.Start(profile.MemProfile).Stop()
|
defer profile.Start(profile.MemProfile).Stop()
|
||||||
@ -145,7 +136,7 @@ func main() {
|
|||||||
app := tview.NewApplication()
|
app := tview.NewApplication()
|
||||||
pages := tview.NewPages()
|
pages := tview.NewPages()
|
||||||
|
|
||||||
widgets := maker.MakeWidgets(app, pages, Config)
|
widgets := maker.MakeWidgets(app, pages, config)
|
||||||
wtf.ValidateWidgets(widgets)
|
wtf.ValidateWidgets(widgets)
|
||||||
runningWidgets = widgets
|
runningWidgets = widgets
|
||||||
|
|
||||||
|
@ -3,9 +3,9 @@ package maker
|
|||||||
import (
|
import (
|
||||||
"github.com/olebedev/config"
|
"github.com/olebedev/config"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/bargraph"
|
|
||||||
"github.com/wtfutil/wtf/logger"
|
"github.com/wtfutil/wtf/logger"
|
||||||
"github.com/wtfutil/wtf/modules/bamboohr"
|
"github.com/wtfutil/wtf/modules/bamboohr"
|
||||||
|
"github.com/wtfutil/wtf/modules/bargraph"
|
||||||
"github.com/wtfutil/wtf/modules/circleci"
|
"github.com/wtfutil/wtf/modules/circleci"
|
||||||
"github.com/wtfutil/wtf/modules/clocks"
|
"github.com/wtfutil/wtf/modules/clocks"
|
||||||
"github.com/wtfutil/wtf/modules/cmdrunner"
|
"github.com/wtfutil/wtf/modules/cmdrunner"
|
||||||
@ -67,7 +67,8 @@ func MakeWidget(
|
|||||||
settings := bamboohr.NewSettingsFromYAML("BambooHR", moduleConfig, globalConfig)
|
settings := bamboohr.NewSettingsFromYAML("BambooHR", moduleConfig, globalConfig)
|
||||||
widget = bamboohr.NewWidget(app, settings)
|
widget = bamboohr.NewWidget(app, settings)
|
||||||
case "bargraph":
|
case "bargraph":
|
||||||
widget = bargraph.NewWidget(app)
|
settings := bargraph.NewSettingsFromYAML(widgetName, moduleConfig, globalConfig)
|
||||||
|
widget = bargraph.NewWidget(app, settings)
|
||||||
case "bittrex":
|
case "bittrex":
|
||||||
settings := bittrex.NewSettingsFromYAML("Bittrex", moduleConfig, globalConfig)
|
settings := bittrex.NewSettingsFromYAML("Bittrex", moduleConfig, globalConfig)
|
||||||
widget = bittrex.NewWidget(app, settings)
|
widget = bittrex.NewWidget(app, settings)
|
||||||
|
18
modules/bargraph/settings.go
Normal file
18
modules/bargraph/settings.go
Normal 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
|
||||||
|
}
|
@ -23,9 +23,9 @@ type Widget struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewWidget Make new instance of widget
|
// NewWidget Make new instance of widget
|
||||||
func NewWidget(app *tview.Application) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
BarGraph: wtf.NewBarGraph(app, "Sample Bar Graph", "bargraph", false),
|
BarGraph: wtf.NewBarGraph(app, "Sample Bar Graph", settings.common, false),
|
||||||
|
|
||||||
app: app,
|
app: app,
|
||||||
}
|
}
|
@ -25,7 +25,7 @@ type Widget struct {
|
|||||||
// NewWidget Make new instance of widget
|
// NewWidget Make new instance of widget
|
||||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
widget := 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,
|
app: app,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
//BarGraph lets make graphs
|
//BarGraph lets make graphs
|
||||||
@ -19,6 +20,7 @@ type BarGraph struct {
|
|||||||
|
|
||||||
RefreshInt int
|
RefreshInt int
|
||||||
View *tview.TextView
|
View *tview.TextView
|
||||||
|
settings *cfg.Common
|
||||||
|
|
||||||
Position
|
Position
|
||||||
}
|
}
|
||||||
@ -30,25 +32,25 @@ type Bar struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewBarGraph initialize your fancy new graph
|
// 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{
|
widget := BarGraph{
|
||||||
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
|
enabled: settings.Enabled,
|
||||||
focusable: focusable,
|
focusable: focusable,
|
||||||
key: configKey,
|
maxStars: settings.Config.UInt("graphStars", 20),
|
||||||
maxStars: Config.UInt(fmt.Sprintf("wtf.mods.%s.graphStars", configKey), 20),
|
name: settings.Title,
|
||||||
name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
|
starChar: settings.Config.UString("graphIcon", "|"),
|
||||||
starChar: Config.UString(fmt.Sprintf("wtf.mods.%s.graphIcon", configKey), "|"),
|
RefreshInt: settings.RefreshInterval,
|
||||||
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey), 1),
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.Position = NewPosition(
|
widget.Position = NewPosition(
|
||||||
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.top", configKey)),
|
settings.Position.Top,
|
||||||
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.left", configKey)),
|
settings.Position.Left,
|
||||||
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.width", configKey)),
|
settings.Position.Width,
|
||||||
Config.UInt(fmt.Sprintf("wtf.mods.%s.position.height", configKey)),
|
settings.Position.Height,
|
||||||
)
|
)
|
||||||
|
|
||||||
widget.View = widget.addView(configKey)
|
widget.View = widget.addView()
|
||||||
widget.View.SetChangedFunc(func() {
|
widget.View.SetChangedFunc(func() {
|
||||||
app.Draw()
|
app.Draw()
|
||||||
})
|
})
|
||||||
@ -58,10 +60,10 @@ func NewBarGraph(app *tview.Application, name string, configKey string, focusabl
|
|||||||
|
|
||||||
func (widget *BarGraph) BorderColor() string {
|
func (widget *BarGraph) BorderColor() string {
|
||||||
if widget.Focusable() {
|
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() {
|
func (widget *BarGraph) Disable() {
|
||||||
@ -112,20 +114,15 @@ func (widget *BarGraph) TextView() *tview.TextView {
|
|||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *BarGraph) addView(configKey string) *tview.TextView {
|
func (widget *BarGraph) addView() *tview.TextView {
|
||||||
view := tview.NewTextView()
|
view := tview.NewTextView()
|
||||||
|
|
||||||
view.SetBackgroundColor(ColorFor(Config.UString("wtf.colors.background", "black")))
|
view.SetBackgroundColor(ColorFor(widget.settings.Colors.Background))
|
||||||
view.SetBorder(true)
|
view.SetBorder(true)
|
||||||
view.SetBorderColor(ColorFor(widget.BorderColor()))
|
view.SetBorderColor(ColorFor(widget.BorderColor()))
|
||||||
view.SetDynamicColors(true)
|
view.SetDynamicColors(true)
|
||||||
view.SetTitle(widget.Name())
|
view.SetTitle(widget.Name())
|
||||||
view.SetTitleColor(ColorFor(
|
view.SetTitleColor(ColorFor(widget.settings.Colors.Title))
|
||||||
Config.UString(
|
|
||||||
fmt.Sprintf("wtf.mods.%s.colors.title", configKey),
|
|
||||||
Config.UString("wtf.colors.title", "white"),
|
|
||||||
),
|
|
||||||
))
|
|
||||||
view.SetWrap(false)
|
view.SetWrap(false)
|
||||||
|
|
||||||
return view
|
return view
|
||||||
|
@ -2,6 +2,7 @@ package wtf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Display struct {
|
type Display struct {
|
||||||
@ -14,7 +15,7 @@ func NewDisplay(widgets []Wtfable) *Display {
|
|||||||
}
|
}
|
||||||
|
|
||||||
display.build(widgets)
|
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
|
return &display
|
||||||
}
|
}
|
||||||
@ -43,8 +44,8 @@ func (display *Display) add(widget Wtfable) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (display *Display) build(widgets []Wtfable) *tview.Grid {
|
func (display *Display) build(widgets []Wtfable) *tview.Grid {
|
||||||
display.Grid.SetColumns(ToInts(Config.UList("wtf.grid.columns"))...)
|
display.Grid.SetColumns(ToInts(cfg.Config.UList("wtf.grid.columns"))...)
|
||||||
display.Grid.SetRows(ToInts(Config.UList("wtf.grid.rows"))...)
|
display.Grid.SetRows(ToInts(cfg.Config.UList("wtf.grid.rows"))...)
|
||||||
display.Grid.SetBorder(false)
|
display.Grid.SetBorder(false)
|
||||||
|
|
||||||
for _, widget := range widgets {
|
for _, widget := range widgets {
|
||||||
|
@ -2,6 +2,7 @@ package wtf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FocusState int
|
type FocusState int
|
||||||
@ -161,7 +162,7 @@ func (tracker *FocusTracker) focus(idx int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
view := widget.TextView()
|
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)
|
tracker.App.SetFocus(view)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,5 +209,5 @@ func (tracker *FocusTracker) increment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tracker *FocusTracker) useNavShortcuts() bool {
|
func (tracker *FocusTracker) useNavShortcuts() bool {
|
||||||
return Config.UBool("wtf.navigation.shortcuts", true)
|
return cfg.Config.UBool("wtf.navigation.shortcuts", true)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package wtf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MultiSourceWidget struct {
|
type MultiSourceWidget struct {
|
||||||
@ -38,8 +40,8 @@ func (widget *MultiSourceWidget) LoadSources() {
|
|||||||
s := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.singular)
|
s := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.singular)
|
||||||
p := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.plural)
|
p := fmt.Sprintf("wtf.mods.%s.%s", widget.module, widget.plural)
|
||||||
|
|
||||||
single := Config.UString(s, "")
|
single := cfg.Config.UString(s, "")
|
||||||
multiple := Config.UList(p, empty)
|
multiple := cfg.Config.UList(p, empty)
|
||||||
|
|
||||||
asStrs := ToStrs(multiple)
|
asStrs := ToStrs(multiple)
|
||||||
|
|
||||||
|
@ -3,13 +3,10 @@ package wtf
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/olebedev/config"
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/cfg"
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config *config.Config
|
|
||||||
|
|
||||||
type TextWidget struct {
|
type TextWidget struct {
|
||||||
enabled bool
|
enabled bool
|
||||||
focusable bool
|
focusable bool
|
||||||
|
@ -7,6 +7,8 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SimpleDateFormat = "Jan 2"
|
const SimpleDateFormat = "Jan 2"
|
||||||
@ -91,7 +93,7 @@ func OpenFile(path string) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
filePath, _ := ExpandHomeDir(path)
|
filePath, _ := ExpandHomeDir(path)
|
||||||
openFileUtil := Config.UString("wtf.openFileUtil", "open")
|
openFileUtil := cfg.Config.UString("wtf.openFileUtil", "open")
|
||||||
cmd := exec.Command(openFileUtil, filePath)
|
cmd := exec.Command(openFileUtil, filePath)
|
||||||
ExecuteCommand(cmd)
|
ExecuteCommand(cmd)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user