power

package
v2.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package power reports whether the device is running on mains power or a battery, and how much of that battery is left. The updater uses it to refuse an install that could lose power part-way through.

Index

Constants

This section is empty.

Variables

View Source
var ErrDetailUnsupported = errors.New("battery detail is not supported on this platform")

ErrDetailUnsupported is returned by ReadDetail on a build that has no way to ask the hardware about its batteries.

Functions

This section is empty.

Types

type Battery added in v2.20.0

type Battery struct {
	// Percent is the remaining charge, 0-100, or nil when it cannot be read.
	Percent *int
	ID      string
	State   ChargeState
	// TimeRemaining is the estimated time until empty. Zero means unknown.
	TimeRemaining time.Duration
}

Battery is one battery the device itself runs on.

type ChargeState added in v2.20.0

type ChargeState string

ChargeState is what a battery is doing with the power available to it. The empty value means the state could not be read.

const (
	ChargeCharging    ChargeState = "charging"
	ChargeDischarging ChargeState = "discharging"
	ChargeFull        ChargeState = "full"
	// ChargeNotCharging is a battery on external power that is neither filling
	// nor full, which is what a charge limit or a weak charger produces.
	ChargeNotCharging ChargeState = "notCharging"
)

type Detail added in v2.20.0

type Detail struct {
	// Percent is the lowest readable charge across Batteries, the one that
	// decides when the device dies.
	Percent *int
	// Source is SourceBattery or SourceExternal. It is empty when the supply
	// could not be determined.
	Source    Source
	State     ChargeState
	Batteries []Battery
	// TimeRemaining is the shortest estimate across discharging batteries.
	TimeRemaining time.Duration
	// Present reports whether the device has a battery of its own.
	Present bool
}

Detail is the battery reading a status display needs. Unlike Status it keeps the charge level while the device is on external power, and it never folds an unreadable battery into a single fail-safe answer: each field that could not be read is left empty.

func ReadDetail added in v2.20.0

func ReadDetail() (Detail, error)

ReadDetail reports every battery the device runs on from the kernel's power-supply directory.

type Source

type Source string

Source is where the device is drawing power from.

const (
	// SourceNoBattery means the hardware has no battery at all, so it is
	// running on whatever mains supply it always runs on.
	SourceNoBattery Source = "noBattery"
	// SourceExternal means a charger or dock is supplying power. The battery
	// percentage does not matter while this is true.
	SourceExternal Source = "external"
	// SourceBattery means the device is discharging and Percent is its
	// remaining charge.
	SourceBattery Source = "battery"
	// SourceUnknown means the hardware may have a battery but its state could
	// not be read. Callers must treat this as "could lose power at any
	// moment", not as "probably fine".
	SourceUnknown Source = "unknown"
)

type Status

type Status struct {
	Source Source
	// Percent is the remaining charge, 0-100. It is only meaningful when
	// Source is SourceBattery.
	Percent int
}

Status is a snapshot of where the device's power is coming from.

func Read

func Read() (Status, error)

Read reports the device's power state from the kernel's power-supply directory.

Jump to

Keyboard shortcuts

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