Skip to content

Commit

Permalink
Merge pull request #402 from wtfutil/WTF-400-widget-configs
Browse files Browse the repository at this point in the history
WTF-400 Extracting config out of modules
  • Loading branch information
senorprogrammer committed Apr 22, 2019
2 parents de4e285 + 7973314 commit 25a21f7
Show file tree
Hide file tree
Showing 137 changed files with 2,897 additions and 1,091 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,6 +19,7 @@ gcal/client_secret.json
gspreadsheets/client_secret.json
profile.pdf
report.*
.vscode

# All things node
node_modules/
Expand Down
91 changes: 91 additions & 0 deletions cfg/common_settings.go
@@ -0,0 +1,91 @@
package cfg

import (
"github.com/olebedev/config"
)

type Colors struct {
Background string
BorderFocusable string
BorderFocused string
BorderNormal string
Checked string
HighlightFore string
HighlightBack string
Text string
Title string
}

type Module struct {
ConfigKey string
Name string
}

type Position struct {
Height int
Left int
Top int
Width int
}

type Sigils struct {
CheckedIcon string
UncheckedIcon string
}

type Common struct {
Colors
Module
Position
Sigils

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"
sigilsPath := "wtf.sigils"

common := Common{
Colors: Colors{
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", "white"),
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{
ConfigKey: configKey,
Name: name,
},

Position: Position{
Height: ymlConfig.UInt(positionPath + ".height"),
Left: ymlConfig.UInt(positionPath + ".left"),
Top: ymlConfig.UInt(positionPath + ".top"),
Width: ymlConfig.UInt(positionPath + ".width"),
},

Sigils: Sigils{
CheckedIcon: ymlConfig.UString(sigilsPath+".checkedIcon", "x"),
UncheckedIcon: ymlConfig.UString(sigilsPath+".uncheckedIcon", " "),
},

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
}
57 changes: 48 additions & 9 deletions cfg/config_files.go
@@ -1,13 +1,14 @@
package cfg

import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"

"github.com/olebedev/config"
"github.com/wtfutil/wtf/logger"
"github.com/wtfutil/wtf/wtf"
)

// ConfigDirV1 defines the path to the first version of configuration. Do not use this
Expand All @@ -21,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) {
Expand All @@ -38,15 +39,13 @@ func MigrateOldConfig() {
err := Copy(srcDir, destDir)
if err != nil {
panic(err)
} else {
logger.Log(fmt.Sprintf("Copied old config from %s to %s", srcDir, destDir))
}

// Delete the old directory if the new one exists
if _, err := os.Stat(destDir); err == nil {
err := os.RemoveAll(srcDir)
if err != nil {
logger.Log(err.Error())
fmt.Println(err)
}
}
}
Expand All @@ -55,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
}
Expand Down Expand Up @@ -123,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 {
Expand Down Expand Up @@ -199,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
}
62 changes: 36 additions & 26 deletions checklist/checklist.go
Expand Up @@ -3,14 +3,18 @@ package checklist
// Checklist is a module for creating generic checklist implementations
// See 'Todo' for an implementation example
type Checklist struct {
Selected int

Items []*ChecklistItem

checkedIcon string
selected int
uncheckedIcon string
}

func NewChecklist() Checklist {
func NewChecklist(checkedIcon, uncheckedIcon string) Checklist {
list := Checklist{
Selected: -1,
checkedIcon: checkedIcon,
selected: -1,
uncheckedIcon: uncheckedIcon,
}

return list
Expand All @@ -20,12 +24,14 @@ func NewChecklist() Checklist {

// Add creates a new item in the checklist
func (list *Checklist) Add(checked bool, text string) {
item := ChecklistItem{
Checked: checked,
Text: text,
}
item := NewChecklistItem(
checked,
text,
list.checkedIcon,
list.uncheckedIcon,
)

list.Items = append([]*ChecklistItem{&item}, list.Items...)
list.Items = append([]*ChecklistItem{item}, list.Items...)
}

// CheckedItems returns a slice of all the checked items
Expand All @@ -43,7 +49,7 @@ func (list *Checklist) CheckedItems() []*ChecklistItem {

// Delete removes the selected item from the checklist
func (list *Checklist) Delete() {
list.Items = append(list.Items[:list.Selected], list.Items[list.Selected+1:]...)
list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...)
list.Prev()
}

Expand All @@ -53,18 +59,18 @@ func (list *Checklist) Demote() {
return
}

j := list.Selected + 1
j := list.selected + 1
if j >= len(list.Items) {
j = 0
}

list.Swap(list.Selected, j)
list.Selected = j
list.Swap(list.selected, j)
list.selected = j
}

// IsSelectable returns true if the checklist has selectable items, false if it does not
func (list *Checklist) IsSelectable() bool {
return list.Selected >= 0 && list.Selected < len(list.Items)
return list.selected >= 0 && list.selected < len(list.Items)
}

// IsUnselectable returns true if the checklist has no selectable items, false if it does
Expand All @@ -74,9 +80,9 @@ func (list *Checklist) IsUnselectable() bool {

// Next selects the next item in the checklist
func (list *Checklist) Next() {
list.Selected = list.Selected + 1
if list.Selected >= len(list.Items) {
list.Selected = 0
list.selected = list.selected + 1
if list.selected >= len(list.Items) {
list.selected = 0
}
}

Expand All @@ -95,9 +101,9 @@ func (list *Checklist) LongestLine() int {

// Prev selects the previous item in the checklist
func (list *Checklist) Prev() {
list.Selected = list.Selected - 1
if list.Selected < 0 {
list.Selected = len(list.Items) - 1
list.selected = list.selected - 1
if list.selected < 0 {
list.selected = len(list.Items) - 1
}
}

Expand All @@ -107,13 +113,17 @@ func (list *Checklist) Promote() {
return
}

j := list.Selected - 1
j := list.selected - 1
if j < 0 {
j = len(list.Items) - 1
}

list.Swap(list.Selected, j)
list.Selected = j
list.Swap(list.selected, j)
list.selected = j
}

func (list *Checklist) Selected() int {
return list.selected
}

// SelectedItem returns the currently-selected checklist item or nil if no item is selected
Expand All @@ -122,13 +132,13 @@ func (list *Checklist) SelectedItem() *ChecklistItem {
return nil
}

return list.Items[list.Selected]
return list.Items[list.selected]
}

func (list *Checklist) SetSelectedByItem(selectableItem *ChecklistItem) {
for idx, item := range list.Items {
if item == selectableItem {
list.Selected = idx
list.selected = idx
break
}
}
Expand Down Expand Up @@ -158,7 +168,7 @@ func (list *Checklist) UncheckedItems() []*ChecklistItem {

// Unselect removes the current select such that no item is selected
func (list *Checklist) Unselect() {
list.Selected = -1
list.selected = -1
}

// Update sets the text of the currently-selected item to the provided text
Expand Down

0 comments on commit 25a21f7

Please sign in to comment.