Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for automatic reloading the config when it has been changed #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/atotto/clipboard"
"github.com/godbus/dbus"
"github.com/muesli/streamdeck"
"github.com/fsnotify/fsnotify"
)

// Deck is a set of widgets.
Expand All @@ -24,11 +25,12 @@ type Deck struct {
}

// LoadDeck loads a deck configuration.
func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
path, err := expandPath(base, deck)
func LoadDeck(dev *streamdeck.Device, base string, deckName string) (*Deck, error) {
path, err := expandPath(base, deckName)
if err != nil {
return nil, err
}
currentDeck = path
fmt.Println("Loading deck:", path)

dc, err := LoadConfig(path)
Expand Down Expand Up @@ -70,6 +72,33 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
d.Widgets = append(d.Widgets, w)
}

watcher, err := fsnotify.NewWatcher()
err = watcher.Add(path)

go func() {
for {
select {
case event := <-watcher.Events:
if currentDeck == path {
fmt.Println("Change: %s: %s", event.Op, event.Name)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful, Println doesn't expect formatting directives like %s. You'll have to use Printf here and finish it with a \n for a newline.

d, err := LoadDeck(dev, base, deckName)
if err != nil {
fatal(err)
}
err = dev.Clear()
if err != nil {
fatal(err)
}

deck = d
deck.updateWidgets()
return
}
}
}
}()


return &d, nil
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/atotto/clipboard v0.1.4
github.com/bendahl/uinput v1.4.1
github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b
github.com/fsnotify/fsnotify v1.4.7
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/godbus/dbus v4.1.0+incompatible
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b h1:/wqXgpZNTP8qV1dPEApjJXlDQd5N/F9U/WEvy5SawUI=
github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

var (
deck *Deck
currentDeck string

dbusConn *dbus.Conn
keyboard uinput.Keyboard
Expand Down