feat: --get flag

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2024-08-23 19:27:27 +02:00
parent 5419c2a93a
commit d6ed51acbb
Signed by: cafkafk
GPG key ID: 26C542FD97F965CE
2 changed files with 14 additions and 1 deletions

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 Christina Sørensen
// SPDX-FileCopyrightText: 2023-2024 Christina Sørensen
// SPDX-FileContributor: Christina Sørensen
//
// SPDX-License-Identifier: AGPL-3.0-only
@ -26,4 +26,5 @@ pub fn build_cli() -> Command {
.allow_hyphen_values(true)
.value_parser(value_parser!(i32)),
)
.arg(arg!(get: --get "Get current brightness.").required(false))
}

View file

@ -37,6 +37,13 @@ fn get_current_brightness() -> std::io::Result<i32> {
.expect("max brightnesss does not cleanly parse as i32"))
}
fn get_current_brightness_percentage() -> std::io::Result<f32> {
let max_brightness = get_max_brightness()?;
let current_brightness = get_current_brightness()?;
Ok((current_brightness as f32 / max_brightness as f32) * 100.)
}
fn set_brightness_percentage(brightness: i32) -> std::io::Result<()> {
let percentage = (get_max_brightness().expect("max brightnesss does not cleanly parse as i32")
/ 100)
@ -83,6 +90,11 @@ fn main() -> std::io::Result<()> {
set_brightness_percentage(*brightness)?;
} else if let Some(brightness) = matches.get_one::<i32>("change") {
change_brightness_percentage(*brightness)?;
} else if matches.get_flag("get") {
println!(
"{}%",
get_current_brightness_percentage().expect("failed to get current brightness") as i32
);
} else {
set_brightness_percentage(
get_max_brightness().expect("max brightnesss does not cleanly parse as i32"),