diff --git a/bar/run.go b/bar/run.go index 292cbbc7d9ca4b768e4b113b3e3a9133d18601f1..26761256584ef2f57ead9d72df06aa20fda2bd92 100644 --- a/bar/run.go +++ b/bar/run.go @@ -4,6 +4,7 @@ package bar import ( "fmt" + "os/exec" "path/filepath" "strings" "time" @@ -51,13 +52,16 @@ func Run(cmd *cobra.Command, args []string) { for _, module := range cfg.Modules { if click.Name == module.Name { found = true - fmt.Print(`,[{"full_text": "CLICK!"}]`) + if module.OnClick != "" { + cmd := exec.Command("/bin/sh", "-c", module.OnClick) + cmd.Run() + } } } if !found { - fmt.Print(`,[{"full_text": "ERROR!"}]`) + fmt.Printf(`,[{"full_text": "You clicked '%s'. Huh?"}]%c`, click.Name, '\n') + 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")) diff --git a/config/sample.yaml b/config/sample.yaml index b0a4e4a04a674c623c2a93384cfdaa03426325cd..bf3fe00903f9e1d523afebbbce2522c980bb60cb 100644 --- a/config/sample.yaml +++ b/config/sample.yaml @@ -1,10 +1,26 @@ --- refresh: 5 modules: + - module: battery + params: + warncmd: + - notify-send + - -u + - critical + - -t + - 60000 + - -a + - battery + - -i + - battery + - -c + - battery + - "Battery Low" + - "Battery is low - put to sleep or plug in power." - module: text - name: post + name: red params: - text: "post" + text: "red" color: "#11ff11" on-click: xdg-open https://mastodon.ie/ - module: date diff --git a/modules/battery.go b/modules/battery.go index 6e045400fb953d5fc45a30053da8e89183857b21..71de37fa1c44364f22da07b14df764a0419d4d8d 100644 --- a/modules/battery.go +++ b/modules/battery.go @@ -5,6 +5,7 @@ package modules import ( "fmt" "math" + "os/exec" "time" "gitlab.com/lyda/battery" @@ -19,9 +20,11 @@ type battTS struct { type BatteryMod struct { name string onclick string + WarnCmd []string `yaml:"warncmd"` + warnTime int64 battStats []battTS Battery int `yaml:"battery"` - StatusEmp string `yaml:"status-chr"` + StatusEmp string `yaml:"status-emp"` StatusChr string `yaml:"status-chr"` StatusBat string `yaml:"status-bat"` StatusUnk string `yaml:"status-unk"` @@ -39,9 +42,7 @@ func NewBattery(m *Module) *BatteryMod { name: m.Name, onclick: m.OnClick, } - if b.name == "" { - b.name = "battery" - } + b.warnTime = 0 return b } @@ -91,8 +92,6 @@ func (b *BatteryMod) OnClick() string { // Render renders the module. func (b *BatteryMod) Render() string { - // TODO: Alerting. - now := time.Now() batt, err := battery.Get(b.Battery) if err != nil { @@ -129,6 +128,10 @@ func (b *BatteryMod) Render() string { color := b.ColorOK percent := 100 * (batt.Current / batt.Design) if percent < 20 { + if len(b.WarnCmd) != 0 && time.Now().Unix()-b.warnTime > 60 { + cmd := exec.Command(b.WarnCmd[0], b.WarnCmd[1:]...) + cmd.Run() + } color = b.Color20 } if percent < 10 { diff --git a/modules/date.go b/modules/date.go index f0c6698e17c4cbda6f89ad42aedc8b21c479bb3a..fdac6a65f1c2882082a8f08fb9a4a2280f897eea 100644 --- a/modules/date.go +++ b/modules/date.go @@ -23,9 +23,6 @@ func NewDate(m *Module) *DateMod { name: m.Name, onclick: m.OnClick, } - if d.name == "" { - d.name = "date" - } return d } diff --git a/modules/modules.go b/modules/modules.go index 249210e92f89b51b568d40e44c6e6c21eb0b8b1f..93a8d228654e7b881e4236837b4fc5d3833f143c 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -25,7 +25,7 @@ type Module struct { Params ParamsInterface `yaml:"-"` } -// M is an intermidiate type to avoid name collisions. +// M is an intermediate type to avoid name collisions. type M Module // Params is the structure for module specific params. @@ -44,6 +44,9 @@ func (m *Module) UnmarshalYAML(node *yaml.Node) error { if strings.ContainsAny(m.Name, "{}") { return fmt.Errorf("module name field can't contain '{' or '}' characters") } + if m.Name == "" { + m.Name = m.Module + } switch params.Module { case "date": m.Params = NewDate(m) diff --git a/modules/text.go b/modules/text.go index 4c3be9586e09a78e8754ede8618687b2b9c029b1..2c7e356d4f38caadecc85417714d461c445bfda6 100644 --- a/modules/text.go +++ b/modules/text.go @@ -20,9 +20,6 @@ func NewText(m *Module) *TextMod { name: m.Name, onclick: m.OnClick, } - if t.name == "" { - t.name = "text" - } return t }