#battery #freebsd #linux-macos #si-units #macos #linux #windows

starship-battery

Cross-platform information about the notebook batteries

7 unstable releases (3 breaking)

0.10.0 Sep 6, 2024
0.9.1 Jul 25, 2024
0.8.3 Apr 8, 2024
0.8.2 Aug 5, 2023
0.7.9 Nov 9, 2021

#49 in Operating systems

Download history 4832/week @ 2024-07-25 4321/week @ 2024-08-01 4107/week @ 2024-08-08 3703/week @ 2024-08-15 3347/week @ 2024-08-22 3095/week @ 2024-08-29 3393/week @ 2024-09-05 4511/week @ 2024-09-12 3745/week @ 2024-09-19 3229/week @ 2024-09-26 3280/week @ 2024-10-03 3504/week @ 2024-10-10 4698/week @ 2024-10-17 3393/week @ 2024-10-24 4286/week @ 2024-10-31 4460/week @ 2024-11-07

17,370 downloads per month
Used in 7 crates (6 directly)

ISC license

155KB
3.5K SLoC

battery

Latest Version Latest Version Build Status Minimum rustc version ISC licensed

Rust crate providing cross-platform information about the notebook batteries.

Table of contents

Overview

battery provides a cross-platform unified API to a notebook batteries state.

Its main goal is to wrap the OS-specific interfaces, cover all the hacks and legacy cases and get the batteries information (such as state of charge, energy rate, voltage and temperature) as a typed values, recalculated as necessary to be returned as a SI measurement units.

Supported platforms

  • Linux 2.6.39+
  • MacOS 10.10+
  • iOS
  • Windows 7+
  • FreeBSD
  • DragonFlyBSD
  • NetBSD

Do note that iOS implementation uses IOKit bindings, your application might be automatically rejected by Apple based on that fact. Use it on your own risk.

Install

As a prerequisite, battery crate requires at least Rustc version 1.69 or greater.

Add the following line into a Cargo.toml:

[dependencies]
battery = "0.8.3"

Examples

fn main() -> Result<(), battery::Error> {
    let manager = battery::Manager::new()?;

    for (idx, maybe_battery) in manager.batteries()?.enumerate() {
        let battery = maybe_battery?;
        println!("Battery #{}:", idx);
        println!("Vendor: {:?}", battery.vendor());
        println!("Model: {:?}", battery.model());
        println!("State: {:?}", battery.state());
        println!("Time to full charge: {:?}", battery.time_to_full());
        println!("");
    }

    Ok(())
}

See the battery/examples/ folder in the repository for additional examples.

Users

This an incomplete list of the battery crate users. If you are using it too, send me a message and I'll add your project here!

starship

starship is a Rust port of the minimalistic, powerful, and extremely customizable prompt Spaceship ZSH.
It is using the battery crate to show the the current battery level and status in a shell prompt.

Here is what @matchai says:

I really appreciate how easily we were able to get your library up and running! Battery APIs were a headache for us in predecessors of this project 😅

And there is this tweet also!

Dependencies

~1–10MB
~105K SLoC