Add low battery alerts
This commit is contained in:
parent
e4e93df5d6
commit
371908748c
1 changed files with 37 additions and 3 deletions
|
@ -3,9 +3,12 @@ package providers
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/codemicro/bar/internal/i3bar"
|
"github.com/codemicro/bar/internal/i3bar"
|
||||||
)
|
)
|
||||||
|
@ -29,6 +32,7 @@ type Battery struct {
|
||||||
name string
|
name string
|
||||||
previousWasBackgroundWarning bool
|
previousWasBackgroundWarning bool
|
||||||
isAlert bool
|
isAlert bool
|
||||||
|
previousValue string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBattery(deviceName string, fullThreshold, okThreshold, warningThreshold float32) i3bar.BlockGenerator {
|
func NewBattery(deviceName string, fullThreshold, okThreshold, warningThreshold float32) i3bar.BlockGenerator {
|
||||||
|
@ -120,17 +124,31 @@ func (g *Battery) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := g.getState()
|
state, err := g.getState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
percentageString := fmt.Sprintf("%.1f", percentage)
|
||||||
|
shortPercentageString := strings.Split(percentageString, ".")[0]
|
||||||
|
if g.previousValue != shortPercentageString && state == batteryStateDischarging {
|
||||||
|
switch shortPercentageString {
|
||||||
|
case "20":
|
||||||
|
_ = g.SendNotification("Battery is getting low", "At 20% and discharging. Consider plugging in.", "low")
|
||||||
|
case "12":
|
||||||
|
_ = g.SendNotification("Battery is getting lower", "At 12% and discharging. Plug in now.", "normal")
|
||||||
|
case "5":
|
||||||
|
_ = g.SendNotification("Battery is really low", "At 5% and discharging. PLUG IN NOW.", "critical")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.previousValue = shortPercentageString
|
||||||
|
|
||||||
block := &i3bar.Block{
|
block := &i3bar.Block{
|
||||||
Name: g.name,
|
Name: g.name,
|
||||||
Instance: g.DeviceName,
|
Instance: g.DeviceName,
|
||||||
FullText: fmt.Sprintf("%s %.1f%%", state, percentage),
|
FullText: fmt.Sprintf("%s %s%%", state, percentageString),
|
||||||
ShortText: fmt.Sprintf("%.1f%%", percentage),
|
ShortText: percentageString,
|
||||||
}
|
}
|
||||||
|
|
||||||
if percentage < g.WarningThreshold && g.WarningThreshold != 0 {
|
if percentage < g.WarningThreshold && g.WarningThreshold != 0 {
|
||||||
|
@ -173,3 +191,19 @@ func (g *Battery) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
|
||||||
func (g *Battery) GetNameAndInstance() (string, string) {
|
func (g *Battery) GetNameAndInstance() (string, string) {
|
||||||
return g.name, g.DeviceName
|
return g.name, g.DeviceName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Battery) SendNotification(summary, message, priority string) error {
|
||||||
|
fname, err := exec.LookPath("notify-send")
|
||||||
|
if err == nil {
|
||||||
|
fname, err = filepath.Abs(fname)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
process, err := os.StartProcess(fname, []string{fname, "--urgency", priority, summary, message}, &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_ = process.Release()
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue