Better declaration of PumoPM’s arguments, add verbosity parameter

This commit is contained in:
Lucien Cartier-Tilet 2020-08-10 19:57:40 +02:00
parent a49c88a7c6
commit 5065c61e92
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
4 changed files with 231 additions and 94 deletions

182
Cargo.lock generated
View File

@ -1,14 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "arrayref"
version = "0.3.6"
@ -115,19 +106,36 @@ dependencies = [
[[package]]
name = "clap"
version = "2.33.2"
version = "3.0.0-beta.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10040cdf04294b565d9e0319955430099ec3813a64c952b86a41200ad714ae48"
checksum = "860643c53f980f0d38a5e25dfab6c3c93b2cb3aa1fe192643d17a293c6c41936"
dependencies = [
"ansi_term",
"atty",
"bitflags 1.2.1",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.0.0-beta.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb51c9e75b94452505acd21d929323f5a5c6c4735a852adbd39ef5fb1b014f30"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote 1.0.7",
"syn 1.0.38",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
@ -193,6 +201,24 @@ dependencies = [
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b62f79061a0bc2e046024cb7ba44b08419ed238ecbd9adbd787434b9e8c25"
dependencies = [
"autocfg",
]
[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.15"
@ -202,6 +228,16 @@ dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b45e59b16c76b11bf9738fd5d38879d3bd28ad292d7b313608becb17ae2df9"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -331,12 +367,53 @@ dependencies = [
"objc",
]
[[package]]
name = "os_str_bytes"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ac6fe3538f701e339953a3ebbe4f39941aababa8a3f6964635b24ab526daeac"
[[package]]
name = "pkg-config"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]]
name = "proc-macro-error"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote 1.0.7",
"syn 1.0.38",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de"
dependencies = [
"proc-macro2",
"quote 1.0.7",
"syn 1.0.38",
"syn-mid",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
dependencies = [
"unicode-xid 0.2.1",
]
[[package]]
name = "pumopm"
version = "0.1.0"
@ -352,6 +429,15 @@ version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
@ -383,9 +469,9 @@ dependencies = [
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
@ -399,8 +485,8 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3384590878eb0cab3b128e844412e2d010821e7e091211b9d87324173ada7db8"
dependencies = [
"quote",
"syn",
"quote 0.3.15",
"syn 0.11.11",
]
[[package]]
@ -409,9 +495,31 @@ version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
dependencies = [
"quote",
"quote 0.3.15",
"synom",
"unicode-xid",
"unicode-xid 0.0.4",
]
[[package]]
name = "syn"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4"
dependencies = [
"proc-macro2",
"quote 1.0.7",
"unicode-xid 0.2.1",
]
[[package]]
name = "syn-mid"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a"
dependencies = [
"proc-macro2",
"quote 1.0.7",
"syn 1.0.38",
]
[[package]]
@ -420,7 +528,16 @@ version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
dependencies = [
"unicode-xid",
"unicode-xid 0.0.4",
]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
@ -448,6 +565,12 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicode-segmentation"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.8"
@ -460,6 +583,12 @@ version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "uom"
version = "0.26.0"
@ -476,6 +605,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "void"
version = "1.0.2"
@ -504,6 +639,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -13,4 +13,4 @@ publish = false
[dependencies]
battery = "0.7.5"
notify-rust = "4"
clap = "2.33"
clap = "3.0.0-beta.1"

View File

@ -1,27 +1,38 @@
use notify_rust::{Hint, Notification, Urgency};
pub enum VerbosityLevel {
None = 0,
Some = 1,
Lots = 2,
}
pub struct BatteryState {
manager: battery::Manager,
battery: battery::Battery,
refresh_rate: u64,
low_level: f32,
very_low_level: f32,
critical_level: f32,
refresh_rate: u8,
pub is_triggered_low: bool,
pub is_triggered_very_low: bool,
is_triggered_low: bool,
is_triggered_very_low: bool,
verbosity: VerbosityLevel,
}
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;
pub const DEFAULT_REFRESH: u64 = 5;
impl BatteryState {
pub fn new(
low_level: f32,
very_low_level: f32,
critical_level: f32,
refresh_rate: u8,
refresh_rate: u64,
verbosity: VerbosityLevel,
) -> battery::Result<Self> {
let manager = battery::Manager::new().unwrap();
let battery = match manager.batteries().unwrap().next() {
@ -40,12 +51,16 @@ impl BatteryState {
Ok(Self {
manager,
battery,
refresh_rate,
low_level,
very_low_level,
critical_level,
refresh_rate,
is_triggered_low: false,
is_triggered_very_low: false,
verbosity,
})
}
@ -111,6 +126,9 @@ impl BatteryState {
},
_ => self.reset_levels(),
}
use std::{thread, time::Duration};
thread::sleep(Duration::from_secs(self.refresh_rate));
}
}
@ -121,6 +139,7 @@ impl Default for BatteryState {
DEFAULT_VERY_LOW,
DEFAULT_CRITICAL,
DEFAULT_REFRESH,
VerbosityLevel::None,
)
.unwrap()
}

View File

@ -1,82 +1,56 @@
mod battery_state;
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
const AUTHORS: Option<&'static str> = option_env!("CARGO_PKG_AUTHORS");
// use battery_state::*;
use battery_state::{BatteryState, VerbosityLevel};
use clap::Clap;
use clap::{App, Arg};
use std::{thread, time::Duration};
#[derive(Clap)]
#[clap(
version = "0.1.0",
author = "Lucien Cartier-Tilet <lucien@phundrak.com>"
)]
struct Opts {
#[clap(short, long, default_value = "25")]
low: f32,
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,
}
};
#[clap(short = "L", long, default_value = "15")]
very_low: f32,
#[clap(short, long, default_value = "10")]
critical: f32,
#[clap(short, long = "refresh-rate", default_value = "5")]
refresh_rate: u64,
#[clap(short, long, parse(from_occurrences))]
verbose: i32,
}
fn main() {
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 batterys 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 batterys 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 batterys 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 batterys levels be read (in seconds)")
.takes_value(true))
.get_matches();
let opts: Opts = Opts::parse();
println!("Low battery: {}%", opts.low);
println!("Very low battery: {}%", opts.very_low);
println!("Critical battery: {}%", opts.critical);
println!("Refresh rate: {}s", opts.refresh_rate);
match opts.verbose {
0 => println!("No verbose info"),
1 => println!("Some verbose info"),
_ => println!("Lots of verbose info"),
}
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,
let mut battery = BatteryState::new(
opts.low,
opts.very_low,
opts.critical,
opts.refresh_rate,
match opts.verbose {
0 => VerbosityLevel::None,
1 => VerbosityLevel::Some,
_ => VerbosityLevel::Lots,
},
)
.unwrap();
loop {
thread::sleep(Duration::from_secs(5));
battery.update();
}
}