mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 Common settings implemented
This commit is contained in:
parent
2ed3c087b8
commit
f09d08bda2
@ -13,6 +13,7 @@ type Colors struct {
|
||||
HighlightFore string
|
||||
HighlightBack string
|
||||
Text string
|
||||
Title string
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
@ -33,21 +34,27 @@ type Common struct {
|
||||
Position
|
||||
|
||||
Enabled bool
|
||||
FocusChar int
|
||||
RefreshInterval int
|
||||
Title string
|
||||
}
|
||||
|
||||
func NewCommonSettingsFromYAML(name, configKey string, ymlConfig *config.Config) *Common {
|
||||
colorsPath := "wtf.colors"
|
||||
modulePath := "wtf.mods." + configKey
|
||||
positionPath := "wtf.mods." + configKey + ".position"
|
||||
|
||||
common := Common{
|
||||
Colors: Colors{
|
||||
Background: ymlConfig.UString("wtf.colors.background", "black"),
|
||||
BorderFocusable: ymlConfig.UString("wtf.colors.border.focusable"),
|
||||
BorderFocused: ymlConfig.UString("wtf.colors.border.focused"),
|
||||
BorderNormal: ymlConfig.UString("wtf.colors.border.normal"),
|
||||
Checked: ymlConfig.UString("wtf.colors.checked"),
|
||||
HighlightFore: ymlConfig.UString("wtf.colors.highlight.fore"),
|
||||
HighlightBack: ymlConfig.UString("wtf.colors.highlight.back"),
|
||||
Text: ymlConfig.UString("wtf.colors.text", "white"),
|
||||
Background: ymlConfig.UString(modulePath+".colors.background", ymlConfig.UString(colorsPath+".background", "black")),
|
||||
BorderFocusable: ymlConfig.UString(colorsPath+".border.focusable", "red"),
|
||||
BorderFocused: ymlConfig.UString(colorsPath+".border.focused", "orange"),
|
||||
BorderNormal: ymlConfig.UString(colorsPath+".border.normal", "gray"),
|
||||
Checked: ymlConfig.UString(colorsPath+".checked", "gray"),
|
||||
HighlightFore: ymlConfig.UString(colorsPath+".highlight.fore", "black"),
|
||||
HighlightBack: ymlConfig.UString(colorsPath+".highlight.back", "green"),
|
||||
Text: ymlConfig.UString(modulePath+".colors.text", ymlConfig.UString(colorsPath+".text", "white")),
|
||||
Title: ymlConfig.UString(modulePath+".colors.title", ymlConfig.UString(colorsPath+".title", "white")),
|
||||
},
|
||||
|
||||
Module: Module{
|
||||
@ -55,7 +62,17 @@ func NewCommonSettingsFromYAML(name, configKey string, ymlConfig *config.Config)
|
||||
Name: name,
|
||||
},
|
||||
|
||||
Position: Position{},
|
||||
Position: Position{
|
||||
Height: ymlConfig.UInt(positionPath + ".height"),
|
||||
Left: ymlConfig.UInt(positionPath + ".left"),
|
||||
Top: ymlConfig.UInt(positionPath + ".top"),
|
||||
Width: ymlConfig.UInt(positionPath + ".width"),
|
||||
},
|
||||
|
||||
Enabled: ymlConfig.UBool(modulePath+".enabled", false),
|
||||
FocusChar: ymlConfig.UInt(modulePath+".focusChar", -1),
|
||||
RefreshInterval: ymlConfig.UInt(modulePath+".refreshInterval", 300),
|
||||
Title: ymlConfig.UString(modulePath+".title", name),
|
||||
}
|
||||
|
||||
return &common
|
||||
|
@ -1,12 +1,14 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
// ConfigDirV1 defines the path to the first version of configuration. Do not use this
|
||||
@ -20,8 +22,8 @@ const ConfigDirV2 = "~/.config/wtf/"
|
||||
// MigrateOldConfig copies any existing configuration from the old location
|
||||
// to the new, XDG-compatible location
|
||||
func MigrateOldConfig() {
|
||||
srcDir, _ := wtf.ExpandHomeDir(ConfigDirV1)
|
||||
destDir, _ := wtf.ExpandHomeDir(ConfigDirV2)
|
||||
srcDir, _ := expandHomeDir(ConfigDirV1)
|
||||
destDir, _ := expandHomeDir(ConfigDirV2)
|
||||
|
||||
// If the old config directory doesn't exist, do not move
|
||||
if _, err := os.Stat(srcDir); os.IsNotExist(err) {
|
||||
@ -52,7 +54,7 @@ func MigrateOldConfig() {
|
||||
|
||||
// ConfigDir returns the absolute path to the configuration directory
|
||||
func ConfigDir() (string, error) {
|
||||
configDir, err := wtf.ExpandHomeDir(ConfigDirV2)
|
||||
configDir, err := expandHomeDir(ConfigDirV2)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -120,7 +122,7 @@ func CreateFile(fileName string) (string, error) {
|
||||
|
||||
// LoadConfigFile loads the config.yml file to configure the app
|
||||
func LoadConfigFile(filePath string) *config.Config {
|
||||
absPath, _ := wtf.ExpandHomeDir(filePath)
|
||||
absPath, _ := expandHomeDir(filePath)
|
||||
|
||||
cfg, err := config.ParseYamlFile(absPath)
|
||||
if err != nil {
|
||||
@ -196,3 +198,43 @@ const simpleConfig = `wtf:
|
||||
width: 1
|
||||
refreshInterval: 30
|
||||
`
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
// Expand expands the path to include the home directory if the path
|
||||
// is prefixed with `~`. If it isn't prefixed with `~`, the path is
|
||||
// returned as-is.
|
||||
func expandHomeDir(path string) (string, error) {
|
||||
if len(path) == 0 {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
if path[0] != '~' {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
|
||||
return "", errors.New("cannot expand user-specific home dir")
|
||||
}
|
||||
|
||||
dir, err := home()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(dir, path[1:]), nil
|
||||
}
|
||||
|
||||
// Dir returns the home directory for the executing user.
|
||||
// An error is returned if a home directory cannot be detected.
|
||||
func home() (string, error) {
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if currentUser.HomeDir == "" {
|
||||
return "", errors.New("cannot find user-specific home dir")
|
||||
}
|
||||
|
||||
return currentUser.HomeDir, nil
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, "Logs", "logger", true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
filePath: logFilePath(),
|
||||
settings: settings,
|
||||
|
22
main.go
22
main.go
@ -151,7 +151,7 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid *
|
||||
loadConfigFile(absPath)
|
||||
|
||||
widgets := makeWidgets(app, pages)
|
||||
validateWidgets(widgets)
|
||||
wtf.ValidateWidgets(widgets)
|
||||
|
||||
initializeFocusTracker(app, widgets)
|
||||
|
||||
@ -342,15 +342,15 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) []wtf.Wtfable {
|
||||
return widgets
|
||||
}
|
||||
|
||||
// Check that all the loaded widgets are valid for display
|
||||
func validateWidgets(widgets []wtf.Wtfable) {
|
||||
for _, widget := range widgets {
|
||||
if widget.Enabled() && !widget.IsPositionable() {
|
||||
errStr := fmt.Sprintf("Widget config has invalid values: %s", widget.Key())
|
||||
log.Fatalln(errStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Check that all the loaded widgets are valid for display
|
||||
// func validateWidgets(widgets []wtf.Wtfable) {
|
||||
// for _, widget := range widgets {
|
||||
// if widget.Enabled() && !widget.IsPositionable() {
|
||||
// errStr := fmt.Sprintf("Widget config has invalid values: %s", widget.Key())
|
||||
// log.Fatalln(errStr)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/* -------------------- Main -------------------- */
|
||||
|
||||
@ -376,7 +376,7 @@ func main() {
|
||||
pages := tview.NewPages()
|
||||
|
||||
widgets := makeWidgets(app, pages)
|
||||
validateWidgets(widgets)
|
||||
wtf.ValidateWidgets(widgets)
|
||||
|
||||
initializeFocusTracker(app, widgets)
|
||||
|
||||
|
@ -17,7 +17,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
Client: NewClient(settings.apiKey),
|
||||
|
||||
settings: settings,
|
||||
|
@ -19,7 +19,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
dateFormat: settings.dateFormat,
|
||||
|
@ -20,7 +20,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
args: settings.args,
|
||||
cmd: settings.cmd,
|
||||
|
@ -27,7 +27,7 @@ type Widget struct {
|
||||
// NewWidget Make new instance of widget
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
summaryList: summaryList{},
|
||||
|
@ -20,7 +20,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
device_token: settings.deviceToken,
|
||||
settings: settings,
|
||||
|
@ -22,7 +22,7 @@ type Widget struct {
|
||||
// NewWidget Make new instance of widget
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
priceWidget: price.NewWidget(settings.priceSettings),
|
||||
toplistWidget: toplist.NewWidget(settings.toplistSettings),
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
ch: make(chan struct{}),
|
||||
|
@ -49,7 +49,7 @@ var (
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Idx: 0,
|
||||
settings: settings,
|
||||
|
@ -42,8 +42,8 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget("git", "repository", "repositories"),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common.ConfigKey, "repository", "repositories"),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
pages: pages,
|
||||
|
@ -32,7 +32,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Idx: 0,
|
||||
settings: settings,
|
||||
|
@ -39,7 +39,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Idx: 0,
|
||||
gitlab: gitlab,
|
||||
|
@ -32,7 +32,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ type ipinfo struct {
|
||||
// NewWidget constructor
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ type ipinfo struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common.ConfigKey, "repository", "repositories"),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
pages: pages,
|
||||
|
@ -33,7 +33,7 @@ var offset = 0
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
Battery: NewBattery(),
|
||||
settings: settings,
|
||||
|
@ -35,7 +35,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -30,14 +30,16 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
spotifyClient := spotigopher.NewClient()
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Info: spotigopher.Info{},
|
||||
SpotifyClient: spotifyClient,
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.settings.common.RefreshInterval = 5
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.TextWidget.RefreshInt = 5
|
||||
widget.View.SetInputCapture(widget.captureInput)
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
|
@ -93,7 +93,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Info: Info{},
|
||||
client: client,
|
||||
@ -135,8 +135,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
// If inconvenient, I'll remove this option and save the URL in a file or some other method.
|
||||
wtf.OpenFile(`"` + authURL + `"`)
|
||||
|
||||
widget.settings.common.RefreshInterval = 5
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.TextWidget.RefreshInt = 5
|
||||
widget.View.SetInputCapture(widget.captureInput)
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
|
@ -14,7 +14,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
CurrentIcon: 0,
|
||||
settings: settings,
|
||||
|
@ -19,7 +19,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, date, version string, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
Date: date,
|
||||
settings: settings,
|
||||
|
@ -42,19 +42,17 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common.ConfigKey, "filePath", "filePaths"),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
// Don't use a timer for this widget, watch for filesystem changes instead
|
||||
widget.RefreshInt = 0
|
||||
|
||||
widget.LoadSources()
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
widget.settings.common.RefreshInterval = 0
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
widget.LoadSources()
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetWordWrap(true)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
@ -49,7 +49,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
settings: settings,
|
||||
|
@ -37,7 +37,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common.ConfigKey, "screenName", "screenNames"),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
idx: 0,
|
||||
settings: settings,
|
||||
|
@ -15,7 +15,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, name string, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, name, name, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ type Widget struct {
|
||||
// NewWidget creates a new widget
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
}
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
|
@ -18,7 +18,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, false),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, false),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ type Widget struct {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
Idx: 0,
|
||||
settings: settings,
|
||||
|
@ -19,7 +19,7 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common.Name, settings.common.ConfigKey, true),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
@ -5,44 +5,49 @@ import (
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
var Config *config.Config
|
||||
|
||||
type TextWidget struct {
|
||||
enabled bool
|
||||
focusable bool
|
||||
focusChar string
|
||||
key string
|
||||
name string
|
||||
enabled bool
|
||||
focusable bool
|
||||
focusChar string
|
||||
key string
|
||||
name string
|
||||
refreshInterval int
|
||||
|
||||
RefreshInt int
|
||||
View *tview.TextView
|
||||
View *tview.TextView
|
||||
|
||||
CommonSettings *cfg.Common
|
||||
Position
|
||||
}
|
||||
|
||||
func NewTextWidget(app *tview.Application, name string, configKey string, focusable bool) TextWidget {
|
||||
focusCharValue := Config.UInt(fmt.Sprintf("wtf.mods.%s.focusChar", configKey), -1)
|
||||
focusChar := string('0' + focusCharValue)
|
||||
if focusCharValue == -1 {
|
||||
func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable bool) TextWidget {
|
||||
configKey := commonSettings.ConfigKey
|
||||
|
||||
focusChar := string('0' + commonSettings.FocusChar)
|
||||
if commonSettings.FocusChar == -1 {
|
||||
focusChar = ""
|
||||
}
|
||||
|
||||
widget := TextWidget{
|
||||
enabled: Config.UBool(fmt.Sprintf("wtf.mods.%s.enabled", configKey), false),
|
||||
focusable: focusable,
|
||||
focusChar: focusChar,
|
||||
key: configKey,
|
||||
name: Config.UString(fmt.Sprintf("wtf.mods.%s.title", configKey), name),
|
||||
RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey)),
|
||||
CommonSettings: commonSettings,
|
||||
|
||||
enabled: commonSettings.Enabled,
|
||||
focusable: focusable,
|
||||
focusChar: focusChar,
|
||||
key: commonSettings.ConfigKey,
|
||||
name: commonSettings.Name,
|
||||
refreshInterval: commonSettings.RefreshInterval,
|
||||
}
|
||||
|
||||
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)),
|
||||
commonSettings.Position.Top,
|
||||
commonSettings.Position.Left,
|
||||
commonSettings.Position.Width,
|
||||
commonSettings.Position.Height,
|
||||
)
|
||||
|
||||
widget.addView(app, configKey)
|
||||
@ -54,10 +59,10 @@ func NewTextWidget(app *tview.Application, name string, configKey string, focusa
|
||||
|
||||
func (widget *TextWidget) BorderColor() string {
|
||||
if widget.Focusable() {
|
||||
return Config.UString("wtf.colors.border.focusable", "red")
|
||||
return widget.CommonSettings.Colors.BorderFocusable
|
||||
}
|
||||
|
||||
return Config.UString("wtf.colors.border.normal", "gray")
|
||||
return widget.CommonSettings.Colors.BorderNormal
|
||||
}
|
||||
|
||||
func (widget *TextWidget) ContextualTitle(defaultStr string) string {
|
||||
@ -103,7 +108,7 @@ func (widget *TextWidget) Name() string {
|
||||
}
|
||||
|
||||
func (widget *TextWidget) RefreshInterval() int {
|
||||
return widget.RefreshInt
|
||||
return widget.refreshInterval
|
||||
}
|
||||
|
||||
func (widget *TextWidget) SetFocusChar(char string) {
|
||||
@ -119,34 +124,19 @@ func (widget *TextWidget) TextView() *tview.TextView {
|
||||
func (widget *TextWidget) addView(app *tview.Application, configKey string) {
|
||||
view := tview.NewTextView()
|
||||
|
||||
view.SetBackgroundColor(ColorFor(
|
||||
Config.UString(fmt.Sprintf("wtf.mods.%s.colors.background", configKey),
|
||||
Config.UString("wtf.colors.background", "black"),
|
||||
),
|
||||
))
|
||||
|
||||
view.SetTextColor(ColorFor(
|
||||
Config.UString(
|
||||
fmt.Sprintf("wtf.mods.%s.colors.text", configKey),
|
||||
Config.UString("wtf.colors.text", "white"),
|
||||
),
|
||||
))
|
||||
|
||||
view.SetTitleColor(ColorFor(
|
||||
Config.UString(
|
||||
fmt.Sprintf("wtf.mods.%s.colors.title", configKey),
|
||||
Config.UString("wtf.colors.title", "white"),
|
||||
),
|
||||
))
|
||||
view.SetBackgroundColor(ColorFor(widget.CommonSettings.Colors.Background))
|
||||
view.SetBorderColor(ColorFor(widget.BorderColor()))
|
||||
view.SetTextColor(ColorFor(widget.CommonSettings.Colors.Text))
|
||||
view.SetTitleColor(ColorFor(widget.CommonSettings.Colors.Title))
|
||||
|
||||
view.SetBorder(true)
|
||||
view.SetBorderColor(ColorFor(widget.BorderColor()))
|
||||
view.SetChangedFunc(func() {
|
||||
app.Draw()
|
||||
})
|
||||
view.SetDynamicColors(true)
|
||||
view.SetTitle(widget.ContextualTitle(widget.name))
|
||||
view.SetWrap(false)
|
||||
|
||||
view.SetChangedFunc(func() {
|
||||
app.Draw()
|
||||
})
|
||||
|
||||
widget.View = view
|
||||
}
|
||||
|
@ -5,9 +5,8 @@ import (
|
||||
"io/ioutil"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
//"sync"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
21
wtf/widget_validator.go
Normal file
21
wtf/widget_validator.go
Normal file
@ -0,0 +1,21 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Check that all the loaded widgets are valid for display
|
||||
func ValidateWidgets(widgets []Wtfable) (bool, error) {
|
||||
result := true
|
||||
var err error
|
||||
|
||||
for _, widget := range widgets {
|
||||
if widget.Enabled() && !widget.IsPositionable() {
|
||||
errStr := fmt.Sprintf("Widget config has invalid values: %s", widget.Key())
|
||||
log.Fatalln(errStr)
|
||||
}
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
@ -9,15 +9,15 @@ type Wtfable interface {
|
||||
Scheduler
|
||||
|
||||
BorderColor() string
|
||||
Focusable() bool
|
||||
FocusChar() string
|
||||
Focusable() bool
|
||||
Key() string
|
||||
Name() string
|
||||
SetFocusChar(string)
|
||||
TextView() *tview.TextView
|
||||
|
||||
Top() int
|
||||
Left() int
|
||||
Width() int
|
||||
Height() int
|
||||
Left() int
|
||||
Top() int
|
||||
Width() int
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
. "github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
func TestASCIItoTviewColors(t *testing.T) {
|
||||
func Test_ASCIItoTviewColors(t *testing.T) {
|
||||
Equal(t, "", ASCIItoTviewColors(""))
|
||||
Equal(t, "cat", ASCIItoTviewColors("cat"))
|
||||
Equal(t, "[38;5;226mcat/[-]", ASCIItoTviewColors("[38;5;226mcat/[0m"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user