diff --git a/bar/run.go b/bar/run.go index 6c457c2a032a0aff44bb795f922aab932bebbee2..b970949ceb1f37f59ece133f1b05c4e68a205913 100644 --- a/bar/run.go +++ b/bar/run.go @@ -4,9 +4,11 @@ package bar import ( "fmt" + "path/filepath" "strings" "time" + "github.com/fsnotify/fsnotify" "github.com/spf13/cobra" "github.com/spf13/viper" "gitlab.ie.suberic.net/kevin/i3going-on/modules" @@ -15,23 +17,34 @@ import ( // Run is essentially the main program. func Run(cmd *cobra.Command, args []string) { + // Get our config file. viper.BindPFlags(cmd.Flags()) 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) fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n", unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT")) separator := "[" for { + // Render the ststus line. line := make([]string, 0, 5) for _, module := range cfg.Modules { line = append(line, module.Render()) } fmt.Printf("%s\n[%s]", separator, strings.Join(line, ",")) separator = "," + + // Wait for the refresh time, a click or a config file change. select { case click := <-clicks: found := false @@ -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"}]`) } 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): } } diff --git a/go.mod b/go.mod index 6db6b94c0ad99a1d688a6e71a63ddf0a7e4f7f72..f9199722981ed1a04056815a87acd432cde4920a 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,15 @@ module gitlab.ie.suberic.net/kevin/i3going-on go 1.19 require ( + github.com/fsnotify/fsnotify v1.6.0 github.com/spf13/cobra v1.6.1 github.com/spf13/viper v1.14.0 golang.org/x/sys v0.0.0-20220908164124-27713097b956 + gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/magiconair/properties v1.8.6 // indirect @@ -23,6 +25,4 @@ require ( github.com/subosito/gotenv v1.4.1 // indirect golang.org/x/text v0.4.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 )