integrate power_bat into config
This commit is contained in:
parent
ef6d2e2179
commit
90c2a071cd
58
home-manager/progs/eww/config/scripts/power_bat.rs
Executable file
58
home-manager/progs/eww/config/scripts/power_bat.rs
Executable file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env rust-script
|
||||
|
||||
use std::{fmt, fs::read_to_string, str::FromStr};
|
||||
|
||||
const BASE_PATH: &str = "/sys/class/power_supply/BAT1/";
|
||||
const CURRENT_NOW_PATH: &str = "current_now";
|
||||
const VOLTAGE_NOW_PATH: &str = "voltage_now";
|
||||
const STATUS_PATH: &str = "status";
|
||||
const FACTOR: f32 = 1e6_f32;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Status {
|
||||
Charging,
|
||||
Discharging,
|
||||
NotCharging,
|
||||
}
|
||||
|
||||
impl FromStr for Status {
|
||||
type Err = &'static str;
|
||||
|
||||
fn from_str(input: &str) -> Result<Status, Self::Err> {
|
||||
match input {
|
||||
"Charging" => Ok(Status::Charging),
|
||||
"Discharging" => Ok(Status::Discharging),
|
||||
"Not charging" => Ok(Status::NotCharging),
|
||||
_ => Err("unknown state"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Status {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_and_trim_into<T: FromStr<Err = impl fmt::Debug>>(path: &str) -> T {
|
||||
let mut content = read_to_string(BASE_PATH.to_owned() + path).unwrap();
|
||||
content.pop();
|
||||
T::from_str(&content).unwrap()
|
||||
}
|
||||
|
||||
fn fetch_bat_info(path: &str) -> f32 {
|
||||
let value: f32 = fetch_and_trim_into(path);
|
||||
value / FACTOR
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let current_now: f32 = fetch_bat_info(CURRENT_NOW_PATH);
|
||||
let voltage_now: f32 = fetch_bat_info(VOLTAGE_NOW_PATH);
|
||||
let watts: f32 = current_now * voltage_now;
|
||||
let status: Status = fetch_and_trim_into(STATUS_PATH);
|
||||
|
||||
println!(
|
||||
"voltage: {:.4}\ncurrent: {:.4}\nwatts: {:.4}\nstatus: {}",
|
||||
voltage_now, current_now, watts, status
|
||||
);
|
||||
}
|
||||
@ -92,8 +92,7 @@
|
||||
|
||||
(defpoll time :interval "1s" `date +'{"long":"%a %b %e %H:%M:%S %Z %Y","hour":"%H","minute":"%M"}'`)
|
||||
|
||||
; TODO: make this a package or something, not using rust-script
|
||||
(defpoll powerstats :interval "2s" `~/Documents/power_bat.rs`)
|
||||
(defpoll powerstats :interval "2s" `power_bat`)
|
||||
|
||||
(defwidget battery []
|
||||
(box :space-evenly false
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
zsh
|
||||
bluez
|
||||
brightnessctl
|
||||
(callPackage ./power_bat.nix {})
|
||||
];
|
||||
|
||||
programs.eww = {
|
||||
|
||||
4
home-manager/progs/eww/power_bat.nix
Normal file
4
home-manager/progs/eww/power_bat.nix
Normal file
@ -0,0 +1,4 @@
|
||||
{ pkgs, lib, ... }:
|
||||
pkgs.writeShellScriptBin "power_bat" ''
|
||||
exec ${lib.getExe pkgs.rust-script} ${./config/scripts/power_bat.rs} "$@"
|
||||
''
|
||||
Loading…
x
Reference in New Issue
Block a user