Skip to content

Commit

Permalink
Implement configuration loading from a file at ~/.projektor/config.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
yamnikov-oleg committed Feb 3, 2017
1 parent 1ddac2e commit 88af5b9
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 21 deletions.
103 changes: 103 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
)

type ProjektorConfig struct {
EnabledCategories struct {
History bool
Apps bool
URL bool
Commands bool
Files bool
}
History struct {
Capacity int
}
}

var (
ConfigFilePath = path.Join(AppDir, "config.yaml")
Config *ProjektorConfig = MustLoadConfig()
)

func DefaultConfig() *ProjektorConfig {
c := &ProjektorConfig{}

c.EnabledCategories.History = true
c.EnabledCategories.Apps = true
c.EnabledCategories.URL = true
c.EnabledCategories.Commands = true
c.EnabledCategories.Files = true

c.History.Capacity = 40

return c
}

func OpenConfig() (*ProjektorConfig, error) {
f, err := os.Open(ConfigFilePath)
if err != nil {
return nil, err
}
defer f.Close()

contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}

config := &ProjektorConfig{}
err = yaml.Unmarshal(contents, config)
if err != nil {
return nil, err
}

return config, nil
}

func CreateConfig() (*ProjektorConfig, error) {
err := os.MkdirAll(AppDir, 0700)
if err != nil {
return nil, err
}

f, err := os.Create(ConfigFilePath)
if err != nil {
return nil, err
}
defer f.Close()

config := DefaultConfig()
buf, err := yaml.Marshal(config)
if err != nil {
return nil, err
}

_, err = f.Write(buf)
if err != nil {
return nil, err
}

return config, nil
}

func MustLoadConfig() *ProjektorConfig {
config, err := OpenConfig()
if err == nil {
return config
}

errduring("opening config file at %q", err, "Attempting to create one", ConfigFilePath)
config, err = CreateConfig()
if err == nil {
return config
}

errduring("creating config file at %q", err, "Using default options", ConfigFilePath)
return DefaultConfig()
}
8 changes: 2 additions & 6 deletions hist_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type HistRecord struct {

type HistoryWarehouse []HistRecord

const (
HistCapacity = 40
)

var (
HistFilepath = AppDir + "/history.yaml"
// Older records are first, newer are at the end
Expand Down Expand Up @@ -57,8 +53,8 @@ func LoadHistory() {

// Truncate to proper size
hl := len(History)
if hl > HistCapacity {
History = History[hl-HistCapacity:]
if hl > Config.History.Capacity {
History = History[hl-Config.History.Capacity:]
}
}

Expand Down
46 changes: 31 additions & 15 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,44 @@ func (UiPointer) Grab() {
}
}

type Category struct {
Name string
Search EntrySearchFunc
}

func EnabledCategories() []Category {
cats := []Category{}

if Config.EnabledCategories.History {
cats = append(cats, Category{"History", SearchHistEntries})
}
if Config.EnabledCategories.Apps {
cats = append(cats, Category{"Apps", SearchAppEntries})
}
if Config.EnabledCategories.URL {
cats = append(cats, Category{"URL", SearchUrlEntries})
}
if Config.EnabledCategories.Commands {
cats = append(cats, Category{"Commands", SearchCmdEntries})
}
if Config.EnabledCategories.Files {
cats = append(cats, Category{"Files", SearchFileEntries})
}

return cats
}

func UpdateSearchResults() {
Ui.TreeView.Clear()
text := strings.TrimSpace(Ui.SearchEntry.GetText())

type catSf struct {
cat string
fn EntrySearchFunc
}

searchFuncs := []catSf{
{"History", SearchHistEntries},
{"Apps", SearchAppEntries},
{"URL", SearchUrlEntries},
{"Commands", SearchCmdEntries},
{"Files", SearchFileEntries},
}
cats := EnabledCategories()

for _, s := range searchFuncs {
list := s.fn(text)
for _, s := range cats {
list := s.Search(text)
for i, entry := range list {
if i == 0 {
Ui.TreeView.AppendLaunchEntry(entry, s.cat)
Ui.TreeView.AppendLaunchEntry(entry, s.Name)
} else {
Ui.TreeView.AppendLaunchEntry(entry, "")
}
Expand Down

0 comments on commit 88af5b9

Please sign in to comment.