batteryalert

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT, MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package batteryalert decides when a battery deserves a notification and when the power profile follows the battery: pure functions over consecutive UPower readings, so every transition is a table test.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PeripheralIcon

func PeripheralIcon(p upower.Peripheral) string

PeripheralIcon is the themed icon for the peripheral's kind.

func PeripheralName

func PeripheralName(p upower.Peripheral) string

PeripheralName is the model UPower reports, or the kind's name when it reports none.

func Watch

func Watch(ctx context.Context, battery upower.Source, profiles powerprofiles.Source, notifier Notifier, config func() Config) error

Watch follows the battery and the peripherals until ctx ends: each PropertiesChanged, DeviceAdded and DeviceRemoved is read and stepped through Next and the saver. Nothing runs between signals. profiles may be nil, which leaves the power profile alone.

Types

type Config

type Config struct {
	Notify    bool
	ChargedAt uint8
	SaverAt   SaverAt
}

Config is what the watcher reads from the user's configuration on every change, so a reload applies to the next reading.

type Level

type Level uint8

Level is what an alert announces.

const (
	LevelLow Level = iota + 1
	LevelCritical
	LevelCharged
)

Alert levels.

func Next

func Next(prev, cur Reading, chargedAt uint8) (Level, bool)

Next is the alert the step from prev to cur raises, if any. Low and Critical follow UPower's WarningLevel, so the thresholds are UPower's PercentageLow and PercentageCritical: one alert on entering each, none while staying, and since plugging in clears the warning, unplugging again re-arms both. Charged fires once when a battery charging crosses chargedAt; zero turns it off.

type Notifier

type Notifier interface {
	SendCritical(ctx context.Context, appName, summary, body, appIcon string) (uint32, error)
}

Notifier posts a critical notification; desktopnotify's Sender is one.

type Reading

type Reading struct {
	Warning    upower.WarningLevel
	Percentage float64
	State      upower.DeviceState
}

Reading is the part of a UPower device the alerts follow.

func FromDevice

func FromDevice(d upower.Device) Reading

FromDevice is the DisplayDevice's reading.

func FromPeripheral

func FromPeripheral(p upower.Peripheral) Reading

FromPeripheral is a peripheral's reading.

func (Reading) OnAC

func (r Reading) OnAC() bool

OnAC reports a battery being charged or held full: what plugging in looks like from the device.

type Saver

type Saver struct {
	Engaged bool
	Restore string
}

Saver is the automatic power-saver's memory: whether it switched, and the profile it switched away from ("" when power-saver was already active, so there is nothing to give back).

func (Saver) Step

func (s Saver) Step(at SaverAt, cur Reading, active string) (Saver, string, bool)

Step is the saver after cur with active the current profile, and the profile to switch to, if any: power-saver on reaching at while on battery, the remembered profile once the battery is on AC again.

type SaverAt

type SaverAt uint8

SaverAt is the warning level that switches to power-saver.

const (
	SaverNever SaverAt = iota
	SaverLow
	SaverCritical
)

Saver thresholds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL