feat: add -c,--change command

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2024-03-26 10:12:48 +01:00
parent da1e7bc912
commit 5419c2a93a
Signed by: cafkafk
GPG key ID: 26C542FD97F965CE
3 changed files with 48 additions and 2 deletions

2
Cargo.lock generated
View file

@ -177,7 +177,7 @@ checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "lir"
version = "0.1.0"
version = "0.0.1"
dependencies = [
"clap",
"clap_complete",

View file

@ -16,7 +16,14 @@ pub fn build_cli() -> Command {
.author(crate_authors!("\n"))
//.arg(arg!(--init ... "Init config.yaml"))
.arg(
arg!(-p --percentage [percentage] "Set brightness by percentage")
arg!(-p --percentage <percentage> "Set brightness by percentage.")
.required(false)
.value_parser(value_parser!(i32)),
)
.arg(
arg!(change: -c --change <change> "Change brightness by percentage.")
.required(false)
.allow_hyphen_values(true)
.value_parser(value_parser!(i32)),
)
}

View file

@ -25,6 +25,18 @@ fn get_max_brightness() -> std::io::Result<i32> {
.expect("max brightnesss does not cleanly parse as i32"))
}
fn get_current_brightness() -> std::io::Result<i32> {
let mut current_brightness_file =
File::open("/sys/class/backlight/intel_backlight/brightness")?;
let mut current_brightness_buffer = String::new();
// POP removes trailing newline
current_brightness_file.read_to_string(&mut current_brightness_buffer)?;
current_brightness_buffer.pop();
Ok(current_brightness_buffer
.parse::<i32>()
.expect("max brightnesss does not cleanly parse as i32"))
}
fn set_brightness_percentage(brightness: i32) -> std::io::Result<()> {
let percentage = (get_max_brightness().expect("max brightnesss does not cleanly parse as i32")
/ 100)
@ -37,6 +49,31 @@ fn set_brightness_percentage(brightness: i32) -> std::io::Result<()> {
Ok(())
}
/// Changes the brightness by percentage relative to max brightness
fn change_brightness_percentage(brightness: i32) -> std::io::Result<()> {
let max_brightness = get_max_brightness()?;
let current_brightness = get_current_brightness()?;
let delta_brightness = (max_brightness / 100) * brightness.abs();
trace!("{}", delta_brightness);
trace!("{}", brightness);
trace!("{}", max_brightness);
trace!("{}", current_brightness);
let mut new_brightness = current_brightness + delta_brightness * brightness.signum();
if new_brightness > max_brightness {
new_brightness = max_brightness;
}
trace!("{}", new_brightness);
fs::write(
"/sys/class/backlight/intel_backlight/brightness",
new_brightness.to_string(),
)?;
Ok(())
}
fn main() -> std::io::Result<()> {
pretty_env_logger::init();
@ -44,6 +81,8 @@ fn main() -> std::io::Result<()> {
if let Some(brightness) = matches.get_one::<i32>("percentage") {
set_brightness_percentage(*brightness)?;
} else if let Some(brightness) = matches.get_one::<i32>("change") {
change_brightness_percentage(*brightness)?;
} else {
set_brightness_percentage(
get_max_brightness().expect("max brightnesss does not cleanly parse as i32"),