From a4fcc89c1880a5fc655cc8533ce393d7c2ecf061 Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@lyda.ie> Date: Sun, 4 Dec 2022 20:57:07 +0000 Subject: [PATCH] More initial pass at actions. --- go.mod | 1 + go.sum | 2 ++ modules/actions.go | 9 --------- modules/actions/actions.go | 13 +++++++++++++ modules/actions/notify.go | 39 ++++++++++++++++++++++++++++++++++++++ modules/battery.go | 4 ++-- 6 files changed, 57 insertions(+), 11 deletions(-) delete mode 100644 modules/actions.go create mode 100644 modules/actions/actions.go create mode 100644 modules/actions/notify.go diff --git a/go.mod b/go.mod index 381786f..4657944 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( ) require ( + github.com/codegoalie/golibnotify v0.1.0 // indirect github.com/distatus/battery v0.10.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect diff --git a/go.sum b/go.sum index d003e20..0a843e4 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/codegoalie/golibnotify v0.1.0 h1:klJMgE+elOsuTLxFvZN+0q1XFgYGr6aT5RyIBb2dZ0c= +github.com/codegoalie/golibnotify v0.1.0/go.mod h1:+v6J4ss13rISdS08ENaHtBlxYOyIAPXVEUM4KyRYpoY= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/modules/actions.go b/modules/actions.go deleted file mode 100644 index 2d140c4..0000000 --- a/modules/actions.go +++ /dev/null @@ -1,9 +0,0 @@ -// Package modules implements all the modules. -// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie> -package modules - -// Action defines a module. -type Action struct { - Kind string `yaml:"kind"` - Args []string `yaml:"args"` -} diff --git a/modules/actions/actions.go b/modules/actions/actions.go new file mode 100644 index 0000000..99cdff9 --- /dev/null +++ b/modules/actions/actions.go @@ -0,0 +1,13 @@ +// Package actions implements module actions. +// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie> +package actions + +// Action defines an action. +type Action struct { + Kind string `yaml:"kind"` + Args []string `yaml:"args"` +} + +type Actioner interface { + Act(args []string) *error +} diff --git a/modules/actions/notify.go b/modules/actions/notify.go new file mode 100644 index 0000000..6e1642c --- /dev/null +++ b/modules/actions/notify.go @@ -0,0 +1,39 @@ +// Package actions handle the actions +// Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie> +package actions + +import ( + "strings" + + "github.com/codegoalie/golibnotify" +) + +type NotifyAction struct { + notifier *golibnotify.SimpleNotifier +} + +func NewNotifyAction() *NotifyAction { + return &NotifyAction{ + notifier: golibnotify.NewSimpleNotifier("i3going-on"), + } +} + +// Act shows the notification. +func (a *NotifyAction) Act(args []string) *error { + var err error + if len(args) == 0 { + err = a.notifier.Show("Notification", "Something happened...", "") + } else if len(args) == 1 { + err = a.notifier.Show("Notification", args[0], "") + } else if len(args) == 2 { + err = a.notifier.Show(args[0], args[1], "") + } else if len(args) == 3 { + err = a.notifier.Show(args[0], args[1], args[2]) + } else { + err = a.notifier.Show(args[0], strings.Join(args[1:len(args)-2], " "), args[len(args)-1]) + } + if err != nil { + return &err + } + return nil +} diff --git a/modules/battery.go b/modules/battery.go index b3c249a..24fdf1b 100644 --- a/modules/battery.go +++ b/modules/battery.go @@ -104,7 +104,7 @@ func (b *BatteryMod) Render() string { if delta >= (60 * time.Second) { discharge := math.Abs(batt.Current - b.battStats[0].charge) if discharge > 0 { - timeLeft := time.Duration(batt.Current/discharge) * delta + timeLeft := time.Duration((batt.Full-batt.Current)/discharge) * delta left = fmt.Sprintf("%dh%dm", int64(timeLeft.Hours()), int64(timeLeft.Minutes()-(timeLeft.Hours()*60))) b.battStats = b.battStats[1:] } else { @@ -126,7 +126,7 @@ func (b *BatteryMod) Render() string { } color := b.ColorOK - percent := 100 * (batt.Current / batt.Full) + percent := 100 * (batt.Current / batt.Design) if percent < 20 { if percent < 10 { color = b.Color10 -- GitLab