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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user