Add a -c/--config option to use a config file different from the default

This commit is contained in:
Gergely Polonkai 2022-05-24 17:29:50 +02:00
parent b63d4cbac8
commit 09e3dbcb9e
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
4 changed files with 27 additions and 7 deletions

View File

@ -4,7 +4,7 @@ This is a Rust implementation of cinnamons [Seasonal Hours Clock](https://git
## Configuration ## Configuration
Create a file `$XDG_CONFIG_HOME/seasonal-clock.toml` (where `$XDG_CONFIG_HOME` is `~/.config` on Linux systems) with the following content: Create a file `$XDG_CONFIG_HOME/seasonal-clock.toml` (where `$XDG_CONFIG_HOME` is `~/.config` on Linux systems) with the following content (or use/modify [example-config.toml](example-config.toml)):
``` ```
[seasonal-clock] [seasonal-clock]
@ -24,6 +24,10 @@ You can pass `-n` or `--now` to the app to only print the current hours name.
This can be used in scripts, or in i3/sway panels, for example. This can be used in scripts, or in i3/sway panels, for example.
### Use a different config
Pass `-c FILE` or `--config FILE` to use `FILE` as the configuration file (instead of the default). Im not sure if it can be useful; i only did this to show off my Rust skills ^_^
## Features ## Features
### Seasonal hours! ### Seasonal hours!

4
example-config.toml Normal file
View File

@ -0,0 +1,4 @@
[seasonal-clock]
# This is somewhere arond Hungary
latitude = 47.65
longitude = 19.28

View File

@ -1,4 +1,5 @@
use std::fs; use std::fs;
use std::path::PathBuf;
use serde::Deserialize; use serde::Deserialize;
@ -14,17 +15,23 @@ struct CompleteConfig {
seasonal_clock: Config, seasonal_clock: Config,
} }
pub fn get_config() -> Option<Config> { pub fn get_config(filename: Option<String>) -> Option<Config> {
let xdg_dirs = xdg::BaseDirectories::new().unwrap(); let config_path = match filename {
let config_path = xdg_dirs Some(v) => PathBuf::from(v),
.place_config_file("seasonal-clock.toml") None => {
.expect("cannot create configuration directory"); let xdg_dirs = xdg::BaseDirectories::new().unwrap();
xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory")
}
};
let data: std::io::Result<String> = fs::read_to_string(config_path); let data: std::io::Result<String> = fs::read_to_string(config_path);
if let Ok(..) = data { if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap(); let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock) Some(complete_config.seasonal_clock)
} else { } else {
eprintln!("Could not parse config file {:?}", data);
None None
} }
} }

View File

@ -21,6 +21,10 @@ struct Args {
/// Print the current hours name and exit /// Print the current hours name and exit
#[clap(short, long)] #[clap(short, long)]
now: bool, now: bool,
/// Use FILE as the configuration file
#[clap(short, long, value_name = "FILE")]
config: Option<String>,
} }
sctk::default_environment!(SeasonalClock, desktop); sctk::default_environment!(SeasonalClock, desktop);
@ -32,7 +36,8 @@ fn main() {
println!("{}", get_current_hour_name()); println!("{}", get_current_hour_name());
std::process::exit(0); std::process::exit(0);
} }
let config = get_config();
let config = get_config(args.config);
run_windowed(&config); run_windowed(&config);
} }