Merge branch 'dev' of ReekyMarko/lqsd into master
This commit is contained in:
commit
425cfb3078
4 changed files with 73 additions and 28 deletions
11
README.md
11
README.md
|
@ -9,13 +9,14 @@ USAGE:
|
|||
lqsd [FLAGS] [OPTIONS]
|
||||
|
||||
FLAGS:
|
||||
-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
|
||||
-V, --version Prints version information
|
||||
-d, --dim Dims the screen to idle level set in configuration
|
||||
-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
|
||||
|
||||
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:
|
||||
|
||||
|
|
12
src/cli.rs
12
src/cli.rs
|
@ -12,6 +12,7 @@ pub fn get_args() -> clap::ArgMatches<'static> {
|
|||
.long("dim")
|
||||
.short("d")
|
||||
.takes_value(false)
|
||||
.display_order(1)
|
||||
.help("Dims the screen to idle level set in configuration"),
|
||||
)
|
||||
.arg(
|
||||
|
@ -19,13 +20,22 @@ pub fn get_args() -> clap::ArgMatches<'static> {
|
|||
.long("resume")
|
||||
.short("r")
|
||||
.takes_value(false)
|
||||
.display_order(2)
|
||||
.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::with_name("config")
|
||||
.long("config")
|
||||
.short("c")
|
||||
.takes_value(true)
|
||||
.number_of_values(1)
|
||||
.value_name("FILE")
|
||||
.help("Sets a custom config file"),
|
||||
)
|
||||
.get_matches()
|
||||
|
|
|
@ -6,6 +6,7 @@ use std::path::PathBuf;
|
|||
use xdg::BaseDirectories;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
pub resume_file: PathBuf,
|
||||
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")
|
||||
.expect("cannot create configuration directory")
|
||||
.place_config_file("config.toml")
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn copy_config() {
|
||||
let path = xdg_path();
|
||||
match fs::write(&path, toml::to_string(&Config::default()).unwrap()) {
|
||||
Ok(()) => println!("Default config saved to {}", path.display()),
|
||||
Err(err) => eprintln!("Failed to write default config: {}", err),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn load_xdg() -> 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()) {
|
||||
Ok(()) => println!("Default config saved to {}", path.display()),
|
||||
Err(err) => eprintln!("Failed to write default config: {}", err),
|
||||
};
|
||||
Config::default()
|
||||
} else {
|
||||
let toml = fs::read(&path).unwrap();
|
||||
let config: Config = toml::from_str(&toml).unwrap();
|
||||
config
|
||||
read_parse(xdg_path())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_user(path: PathBuf) -> Config {
|
||||
if !path.exists() {
|
||||
panic!("{} does not exist", path.display());
|
||||
} else {
|
||||
let toml = fs::read(&path).unwrap();
|
||||
let config: Config = toml::from_str(&toml).unwrap();
|
||||
config
|
||||
}
|
||||
read_parse(path)
|
||||
}
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -58,23 +58,42 @@ fn main() {
|
|||
} 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, 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),
|
||||
}
|
||||
transition(conf.idle_level, dim_speed);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
match fs::remove(&conf.resume_file) {
|
||||
Ok(()) => println!("Resume file removed"),
|
||||
Err(err) => eprintln!("Failed to remove resume file: {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
if args.is_present("copy-config") {
|
||||
config::copy_config();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue