Skip to content
Snippets Groups Projects
Unverified Commit b7059fb6 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Notice changes to the config.

parent fbb570c5
Branches
No related tags found
No related merge requests found
...@@ -4,9 +4,11 @@ package bar ...@@ -4,9 +4,11 @@ package bar
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.ie.suberic.net/kevin/i3going-on/modules" "gitlab.ie.suberic.net/kevin/i3going-on/modules"
...@@ -15,23 +17,34 @@ import ( ...@@ -15,23 +17,34 @@ import (
// Run is essentially the main program. // Run is essentially the main program.
func Run(cmd *cobra.Command, args []string) { func Run(cmd *cobra.Command, args []string) {
// Get our config file.
viper.BindPFlags(cmd.Flags()) viper.BindPFlags(cmd.Flags())
cfg := readConfig(viper.GetString("config")) cfg := readConfig(viper.GetString("config"))
clicks := make(chan modules.Click, 1) // Watch our config file for changes.
watcher, err := fsnotify.NewWatcher()
cobra.CheckErr(err)
configDir := filepath.Dir(viper.GetString("config"))
watcher.Add(configDir)
defer watcher.Close()
// Handle the user clicking things.
clicks := make(chan modules.Click, 1)
go handleCommands(clicks) go handleCommands(clicks)
fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n", fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n",
unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT")) unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT"))
separator := "[" separator := "["
for { for {
// Render the ststus line.
line := make([]string, 0, 5) line := make([]string, 0, 5)
for _, module := range cfg.Modules { for _, module := range cfg.Modules {
line = append(line, module.Render()) line = append(line, module.Render())
} }
fmt.Printf("%s\n[%s]", separator, strings.Join(line, ",")) fmt.Printf("%s\n[%s]", separator, strings.Join(line, ","))
separator = "," separator = ","
// Wait for the refresh time, a click or a config file change.
select { select {
case click := <-clicks: case click := <-clicks:
found := false found := false
...@@ -45,6 +58,10 @@ func Run(cmd *cobra.Command, args []string) { ...@@ -45,6 +58,10 @@ func Run(cmd *cobra.Command, args []string) {
fmt.Print(`,[{"full_text": "ERROR!"},{"name": "post", "full_text": "post", "color": "#11ff11"},{"name": "date", "full_text": "22-12-01 22:54"}]`) fmt.Print(`,[{"full_text": "ERROR!"},{"name": "post", "full_text": "post", "color": "#11ff11"},{"name": "date", "full_text": "22-12-01 22:54"}]`)
} }
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
case configEvent := <-watcher.Events:
if configEvent.Name == viper.GetString("config") && !configEvent.Has(fsnotify.Remove) {
cfg = readConfig(viper.GetString("config"))
}
case <-time.After(time.Duration(cfg.Refresh) * time.Second): case <-time.After(time.Duration(cfg.Refresh) * time.Second):
} }
} }
......
...@@ -3,13 +3,15 @@ module gitlab.ie.suberic.net/kevin/i3going-on ...@@ -3,13 +3,15 @@ module gitlab.ie.suberic.net/kevin/i3going-on
go 1.19 go 1.19
require ( require (
github.com/fsnotify/fsnotify v1.6.0
github.com/spf13/cobra v1.6.1 github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0 github.com/spf13/viper v1.14.0
golang.org/x/sys v0.0.0-20220908164124-27713097b956 golang.org/x/sys v0.0.0-20220908164124-27713097b956
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
) )
require ( require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect github.com/magiconair/properties v1.8.6 // indirect
...@@ -23,6 +25,4 @@ require ( ...@@ -23,6 +25,4 @@ require (
github.com/subosito/gotenv v1.4.1 // indirect github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/text v0.4.0 // indirect golang.org/x/text v0.4.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment