From ec81b998c3c2fadffb3ad385b9519c23f38351b8 Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@lyda.ie> Date: Sun, 2 Apr 2023 20:38:56 +0100 Subject: [PATCH] Make clicking work; warn if battery low. --- bar/run.go | 10 +++++++--- config/sample.yaml | 20 ++++++++++++++++++-- modules/battery.go | 15 +++++++++------ modules/date.go | 3 --- modules/modules.go | 5 ++++- modules/text.go | 3 --- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/bar/run.go b/bar/run.go index 292cbbc..2676125 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 b0a4e4a..bf3fe00 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 6e04540..71de37f 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 f0c6698..fdac6a6 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 249210e..93a8d22 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 4c3be95..2c7e356 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 } -- GitLab