Initial commit
Signed-off-by: Marko Korhonen <marko.korhonen@reekynet.com>
This commit is contained in:
commit
5f81577b83
7 changed files with 398 additions and 0 deletions
88
src/main.rs
Normal file
88
src/main.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
extern crate clap;
|
||||
|
||||
mod cli;
|
||||
mod config;
|
||||
mod fs;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::{thread, time};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
resume_file_path: PathBuf,
|
||||
idle_level: i32,
|
||||
dim_speed: u64,
|
||||
resume_speed: u64,
|
||||
}
|
||||
|
||||
fn transition(w_brightness: i32, speed: time::Duration) {
|
||||
let c_brightness = get_brightness();
|
||||
println!("Transitioning from {}% to {}%", c_brightness, w_brightness);
|
||||
|
||||
if c_brightness < w_brightness {
|
||||
for n in c_brightness..w_brightness {
|
||||
if n != c_brightness {
|
||||
set_brightness(n);
|
||||
thread::sleep(speed);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for n in (w_brightness..c_brightness).rev() {
|
||||
if n != c_brightness {
|
||||
set_brightness(n);
|
||||
thread::sleep(speed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_brightness(brightness: i32) {
|
||||
Command::new("light")
|
||||
.args(&["-S", &brightness.to_string()])
|
||||
.spawn()
|
||||
.expect("Failed to execute command 'light'");
|
||||
}
|
||||
|
||||
fn get_brightness() -> i32 {
|
||||
let output = Command::new("light")
|
||||
.arg("-G")
|
||||
.output()
|
||||
.expect("Failed to execute command 'light'");
|
||||
|
||||
let string = String::from_utf8_lossy(&output.stdout);
|
||||
let float: f32 = string.trim().parse().unwrap();
|
||||
float.round() as i32
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = cli::get_args();
|
||||
let conf: Config;
|
||||
|
||||
if args.is_present("config") {
|
||||
let config_path = PathBuf::from(args.value_of("config").unwrap());
|
||||
conf = config::load_user(config_path);
|
||||
} else {
|
||||
conf = config::load_xdg();
|
||||
}
|
||||
let dim_speed = time::Duration::from_millis(conf.dim_speed);
|
||||
let resume_speed = time::Duration::from_millis(conf.resume_speed);
|
||||
|
||||
if args.is_present("dim") {
|
||||
let current_brightness = get_brightness().to_string();
|
||||
match fs::write(&conf.resume_file_path, current_brightness) {
|
||||
Ok(()) => println!("Current brightness written to resume file"),
|
||||
Err(err) => eprintln!("Error writing brightness to resume file: {}", err),
|
||||
}
|
||||
transition(conf.idle_level, dim_speed);
|
||||
}
|
||||
if args.is_present("resume") {
|
||||
let old_brightness: i32 = fs::read(&conf.resume_file_path).unwrap().parse().unwrap();
|
||||
transition(old_brightness, resume_speed);
|
||||
match fs::remove(&conf.resume_file_path) {
|
||||
Ok(()) => println!("Resume file removed"),
|
||||
Err(err) => eprintln!("Failed to remove resume file: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue