Add default state and command-line arguments
It is now possible to manually set the different levels with command-line arguments.
This commit is contained in:
@@ -1,21 +1,33 @@
|
||||
use notify_rust::{Notification, Hint, Urgency};
|
||||
use notify_rust::{Hint, Notification, Urgency};
|
||||
|
||||
pub struct BatteryState {
|
||||
manager: battery::Manager,
|
||||
battery: battery::Battery,
|
||||
low_level: f32,
|
||||
very_low_level: f32,
|
||||
pub is_triggered_low: bool,
|
||||
manager: battery::Manager,
|
||||
battery: battery::Battery,
|
||||
low_level: f32,
|
||||
very_low_level: f32,
|
||||
critical_level: f32,
|
||||
refresh_rate: u8,
|
||||
pub is_triggered_low: bool,
|
||||
pub is_triggered_very_low: bool,
|
||||
}
|
||||
|
||||
pub const DEFAULT_LOW: f32 = 25_f32;
|
||||
pub const DEFAULT_VERY_LOW: f32 = 15_f32;
|
||||
pub const DEFAULT_CRITICAL: f32 = 10_f32;
|
||||
pub const DEFAULT_REFRESH: u8 = 5_u8;
|
||||
|
||||
impl BatteryState {
|
||||
pub fn new(low_level: f32, very_low_level: f32) -> battery::Result<Self> {
|
||||
pub fn new(
|
||||
low_level: f32,
|
||||
very_low_level: f32,
|
||||
critical_level: f32,
|
||||
refresh_rate: u8,
|
||||
) -> battery::Result<Self> {
|
||||
let manager = battery::Manager::new().unwrap();
|
||||
let battery = match manager.batteries().unwrap().next() {
|
||||
Some(Ok(battery)) => battery,
|
||||
Some(Err(e)) => {
|
||||
eprintln!("An error occured: {:?}", e);
|
||||
eprintln!("An error occured: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
None => {
|
||||
@@ -30,17 +42,19 @@ impl BatteryState {
|
||||
battery,
|
||||
low_level,
|
||||
very_low_level,
|
||||
is_triggered_low: false,
|
||||
critical_level,
|
||||
refresh_rate,
|
||||
is_triggered_low: false,
|
||||
is_triggered_very_low: false,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_charge(&self) -> f32 {
|
||||
self.battery.state_of_charge().value * 100.0
|
||||
self.battery.state_of_charge().value * 100_f32
|
||||
}
|
||||
|
||||
fn reset_levels(&mut self) {
|
||||
self.is_triggered_low = false;
|
||||
self.is_triggered_low = false;
|
||||
self.is_triggered_very_low = false;
|
||||
}
|
||||
|
||||
@@ -52,7 +66,8 @@ impl BatteryState {
|
||||
None => String::from("unknown"),
|
||||
};
|
||||
let level = self.battery.state_of_charge().value * 100.0;
|
||||
let message = format!("Battery level is low! {} left ({}%)", time, level);
|
||||
let message =
|
||||
format!("Battery level is low! {} left ({}%)", time, level);
|
||||
Notification::new()
|
||||
.summary("Low Battery")
|
||||
.body(message.as_str())
|
||||
@@ -71,7 +86,10 @@ impl BatteryState {
|
||||
None => String::from("unknown"),
|
||||
};
|
||||
let level = self.battery.state_of_charge().value * 100.0;
|
||||
let message = format!("Battery level is very low! {} left ({}%)", time, level);
|
||||
let message = format!(
|
||||
"Battery level is very low! {} left ({}%)",
|
||||
time, level
|
||||
);
|
||||
Notification::new()
|
||||
.summary("Low Battery")
|
||||
.body(message.as_str())
|
||||
@@ -86,12 +104,10 @@ impl BatteryState {
|
||||
self.manager.refresh(&mut self.battery).unwrap();
|
||||
use battery::State::{Discharging, Empty};
|
||||
match self.battery.state() {
|
||||
Discharging | Empty => {
|
||||
match self.get_charge() {
|
||||
x if x < self.very_low_level => self.trigger_very_low(),
|
||||
x if x < self.low_level => self.trigger_low(),
|
||||
_ => {},
|
||||
}
|
||||
Discharging | Empty => match self.get_charge() {
|
||||
x if x < self.very_low_level => self.trigger_very_low(),
|
||||
x if x < self.low_level => self.trigger_low(),
|
||||
_ => {}
|
||||
},
|
||||
_ => self.reset_levels(),
|
||||
}
|
||||
@@ -100,6 +116,12 @@ impl BatteryState {
|
||||
|
||||
impl Default for BatteryState {
|
||||
fn default() -> Self {
|
||||
Self::new(15 as f32, 10 as f32).unwrap()
|
||||
Self::new(
|
||||
DEFAULT_LOW,
|
||||
DEFAULT_VERY_LOW,
|
||||
DEFAULT_CRITICAL,
|
||||
DEFAULT_REFRESH,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
108
src/main.rs
108
src/main.rs
@@ -1,46 +1,82 @@
|
||||
mod battery_state;
|
||||
|
||||
use std::{io, thread};
|
||||
use std::{process::Command, time::Duration};
|
||||
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
|
||||
const AUTHORS: Option<&'static str> = option_env!("CARGO_PKG_AUTHORS");
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
macro_rules! get_arg_value {
|
||||
($args:ident, $arg:literal, $type:ty, $default:ident) => {
|
||||
match $args.value_of($arg) {
|
||||
Some(value) => match value.parse::<$type>() {
|
||||
Ok(ret) => ret,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Error parsing {}, using default. Error: {}",
|
||||
value, e
|
||||
);
|
||||
$default
|
||||
}
|
||||
},
|
||||
None => $default,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut battery = battery_state::BatteryState::default();
|
||||
let arguments = App::new("PumoPM")
|
||||
.version(VERSION.unwrap_or("unknown"))
|
||||
.author(AUTHORS.unwrap_or("Lucien Cartier-Tilet <lucien@phundrak.com>"))
|
||||
.about("Tiny custom power manager")
|
||||
.arg(Arg::with_name("low-battery")
|
||||
.short("l")
|
||||
.long("low")
|
||||
.value_name("LOW")
|
||||
.help("Level at which the battery’s level is considered low")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("very-low-battery")
|
||||
.short("L")
|
||||
.long("very-low")
|
||||
.value_name("VERY LOW")
|
||||
.help("Level at which the battery’s level is considered very low")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("critical-battery")
|
||||
.short("c")
|
||||
.long("critical")
|
||||
.value_name("CRITICAL")
|
||||
.help("Level at which the battery’s level is considered critical")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("refresh-rate")
|
||||
.short("r")
|
||||
.long("refresh-rate")
|
||||
.value_name("REFRESH RATE")
|
||||
.help("How often should the battery’s levels be read (in seconds)")
|
||||
.takes_value(true))
|
||||
.get_matches();
|
||||
|
||||
use battery_state::{
|
||||
DEFAULT_CRITICAL, DEFAULT_LOW, DEFAULT_REFRESH, DEFAULT_VERY_LOW,
|
||||
};
|
||||
let low_battery =
|
||||
get_arg_value!(arguments, "low-battery", f32, DEFAULT_LOW);
|
||||
let very_low_battery =
|
||||
get_arg_value!(arguments, "very-low-battery", f32, DEFAULT_VERY_LOW);
|
||||
let critical_battery =
|
||||
get_arg_value!(arguments, "critical-battery", f32, DEFAULT_CRITICAL);
|
||||
let refresh_rate =
|
||||
get_arg_value!(arguments, "refresh-rate", u8, DEFAULT_REFRESH);
|
||||
|
||||
// let mut battery = battery_state::BatteryState::new(low_battery);
|
||||
let mut battery = battery_state::BatteryState::new(
|
||||
low_battery,
|
||||
very_low_battery,
|
||||
critical_battery,
|
||||
refresh_rate,
|
||||
)
|
||||
.unwrap();
|
||||
loop {
|
||||
thread::sleep(Duration::from_secs(5));
|
||||
battery.update();
|
||||
}
|
||||
|
||||
// loop {
|
||||
// thread::sleep(Duration::from_secs(5));
|
||||
// manager.refresh(&mut battery)?;
|
||||
// let charge = battery.state_of_charge().value;
|
||||
|
||||
// // let charge = charge.value as f32 * 100.0;
|
||||
|
||||
// // Notification::new()
|
||||
// // .summary("Battery charge")
|
||||
// // .body(format!("Current battery level is {}", charge).as_str())
|
||||
// // .hint(Hint::Category("battery".to_owned()))
|
||||
// // .urgency(Urgency::Low)
|
||||
// // .show()
|
||||
// // .unwrap();
|
||||
|
||||
// // let result = match charge.value as f32 * 100.0 {
|
||||
// // x if x < 5.0 => Command::new("sh")
|
||||
// // .arg("systemctl")
|
||||
// // .arg("hibernate")
|
||||
// // .output(),
|
||||
// // x if x < 10.0 => {
|
||||
// // Notification::new()
|
||||
// // .summary("Battery very low")
|
||||
// // .body(format!("Current battery level is {}", x).as_str())
|
||||
// // .hint(Hint::Category("battery".to_owned()))
|
||||
// // .urgency(Urgency::Critical)
|
||||
// // .show().unwrap();
|
||||
// // },
|
||||
// // _ => Ok(()),
|
||||
// // };
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user