Merge branch 'dev' of ReekyMarko/lqsd into master

This commit is contained in:
Marko Korhonen 2020-01-21 21:54:50 +02:00 committed by Gitea
commit 425cfb3078
4 changed files with 73 additions and 28 deletions

View file

@ -10,12 +10,13 @@ USAGE:
FLAGS: FLAGS:
-d, --dim Dims the screen to idle level set in configuration -d, --dim Dims the screen to idle level set in configuration
-h, --help Prints help information
-r, --resume Sets the backlight to the value it was before dimming -r, --resume Sets the backlight to the value it was before dimming
--copy-config Copies the default config file to $XDG_CONFIG_HOME/lqsd
-h, --help Prints help information
-V, --version Prints version information -V, --version Prints version information
OPTIONS: OPTIONS:
-c, --config <config> Sets a custom config file --config <FILE> Sets a custom config file
``` ```
The configuration file resides at `~/.config/lqsd/config.toml`. There you can set these values: The configuration file resides at `~/.config/lqsd/config.toml`. There you can set these values:

View file

@ -12,6 +12,7 @@ pub fn get_args() -> clap::ArgMatches<'static> {
.long("dim") .long("dim")
.short("d") .short("d")
.takes_value(false) .takes_value(false)
.display_order(1)
.help("Dims the screen to idle level set in configuration"), .help("Dims the screen to idle level set in configuration"),
) )
.arg( .arg(
@ -19,13 +20,22 @@ pub fn get_args() -> clap::ArgMatches<'static> {
.long("resume") .long("resume")
.short("r") .short("r")
.takes_value(false) .takes_value(false)
.display_order(2)
.help("Sets the backlight to the value it was before dimming"), .help("Sets the backlight to the value it was before dimming"),
) )
.arg(
Arg::with_name("copy-config")
.long("copy-config")
.takes_value(false)
.display_order(3)
.help("Copies the default config file to $XDG_CONFIG_HOME/lqsd"),
)
.arg( .arg(
Arg::with_name("config") Arg::with_name("config")
.long("config") .long("config")
.short("c")
.takes_value(true) .takes_value(true)
.number_of_values(1)
.value_name("FILE")
.help("Sets a custom config file"), .help("Sets a custom config file"),
) )
.get_matches() .get_matches()

View file

@ -6,6 +6,7 @@ use std::path::PathBuf;
use xdg::BaseDirectories; use xdg::BaseDirectories;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct Config { pub struct Config {
pub resume_file: PathBuf, pub resume_file: PathBuf,
pub idle_level: i32, pub idle_level: i32,
@ -24,39 +25,53 @@ impl Default for Config {
} }
} }
fn xdg_config() -> PathBuf { fn read_parse(path: PathBuf) -> Config {
let mut _toml = String::from("");
match fs::read(&path) {
Ok(result) => _toml = result,
Err(err) => {
eprintln!("Failed to read config: {}", err);
println!("Using default config");
return Config::default();
}
}
match toml::from_str(&_toml) {
Ok(result) => return result,
Err(err) => {
eprintln!("Failed to read config: {}", err);
println!("Using default config");
return Config::default();
}
}
}
fn xdg_path() -> PathBuf {
BaseDirectories::with_prefix("lqsd") BaseDirectories::with_prefix("lqsd")
.expect("cannot create configuration directory") .expect("cannot create configuration directory")
.place_config_file("config.toml") .place_config_file("config.toml")
.unwrap() .unwrap()
} }
pub fn load_xdg() -> Config { pub fn copy_config() {
let path = xdg_config(); let path = xdg_path();
if !path.exists() {
println!(
"{} does not exist, writing default configuration",
path.display()
);
match fs::write(&path, toml::to_string(&Config::default()).unwrap()) { match fs::write(&path, toml::to_string(&Config::default()).unwrap()) {
Ok(()) => println!("Default config saved to {}", path.display()), Ok(()) => println!("Default config saved to {}", path.display()),
Err(err) => eprintln!("Failed to write default config: {}", err), Err(err) => eprintln!("Failed to write default config: {}", err),
}; };
}
pub fn load_xdg() -> Config {
let path = xdg_path();
if !path.exists() {
Config::default() Config::default()
} else { } else {
let toml = fs::read(&path).unwrap(); read_parse(xdg_path())
let config: Config = toml::from_str(&toml).unwrap();
config
} }
} }
pub fn load_user(path: PathBuf) -> Config { pub fn load_user(path: PathBuf) -> Config {
if !path.exists() { read_parse(path)
panic!("{} does not exist", path.display());
} else {
let toml = fs::read(&path).unwrap();
let config: Config = toml::from_str(&toml).unwrap();
config
}
} }

View file

@ -58,23 +58,42 @@ fn main() {
} else { } else {
conf = config::load_xdg(); conf = config::load_xdg();
} }
let dim_speed = time::Duration::from_millis(conf.dim_speed); let dim_speed = time::Duration::from_millis(conf.dim_speed);
let resume_speed = time::Duration::from_millis(conf.resume_speed); let resume_speed = time::Duration::from_millis(conf.resume_speed);
if args.is_present("dim") { if args.is_present("dim") {
let current_brightness = get_brightness().to_string(); let current_brightness = get_brightness().to_string();
match fs::write(&conf.resume_file, current_brightness) { match fs::write(&conf.resume_file, current_brightness) {
Ok(()) => println!("Current brightness written to resume file"), Ok(()) => println!(
"Current brightness written to resume file at {}",
conf.resume_file.display()
),
Err(err) => eprintln!("Error writing brightness to resume file: {}", err), Err(err) => eprintln!("Error writing brightness to resume file: {}", err),
} }
transition(conf.idle_level, dim_speed); transition(conf.idle_level, dim_speed);
} }
if args.is_present("resume") { if args.is_present("resume") {
let old_brightness: i32 = fs::read(&conf.resume_file).unwrap().parse().unwrap(); let old_brightness: i32;
match fs::read(&conf.resume_file) {
Ok(result) => old_brightness = result.parse().unwrap(),
Err(err) => {
eprintln!("Failed to read resume_file: {}", err);
old_brightness = 100;
}
}
transition(old_brightness, resume_speed); transition(old_brightness, resume_speed);
match fs::remove(&conf.resume_file) { match fs::remove(&conf.resume_file) {
Ok(()) => println!("Resume file removed"), Ok(()) => println!("Resume file removed"),
Err(err) => eprintln!("Failed to remove resume file: {}", err), Err(err) => eprintln!("Failed to remove resume file: {}", err),
} }
} }
if args.is_present("copy-config") {
config::copy_config();
}
} }