diff --git a/README.md b/README.md index f107b79..371f3bd 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a Rust implementation of cinnamon’s [Seasonal Hours Clock](https://git ## 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] @@ -24,6 +24,10 @@ You can pass `-n` or `--now` to the app to only print the current hour’s name. 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). I’m not sure if it can be useful; i only did this to show off my Rust skills ^_^ + ## Features ### Seasonal hours! diff --git a/example-config.toml b/example-config.toml new file mode 100644 index 0000000..53c5647 --- /dev/null +++ b/example-config.toml @@ -0,0 +1,4 @@ +[seasonal-clock] +# This is somewhere arond Hungary +latitude = 47.65 +longitude = 19.28 diff --git a/src/config.rs b/src/config.rs index a046d96..7cdd834 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use std::fs; +use std::path::PathBuf; use serde::Deserialize; @@ -14,17 +15,23 @@ struct CompleteConfig { seasonal_clock: Config, } -pub fn get_config() -> Option { - let xdg_dirs = xdg::BaseDirectories::new().unwrap(); - let config_path = xdg_dirs - .place_config_file("seasonal-clock.toml") - .expect("cannot create configuration directory"); +pub fn get_config(filename: Option) -> Option { + let config_path = match filename { + Some(v) => PathBuf::from(v), + None => { + 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 = fs::read_to_string(config_path); if let Ok(..) = data { let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap(); Some(complete_config.seasonal_clock) } else { + eprintln!("Could not parse config file {:?}", data); None } } diff --git a/src/main.rs b/src/main.rs index 44ca5ee..7718ddc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,10 @@ struct Args { /// Print the current hour’s name and exit #[clap(short, long)] now: bool, + + /// Use FILE as the configuration file + #[clap(short, long, value_name = "FILE")] + config: Option, } sctk::default_environment!(SeasonalClock, desktop); @@ -32,7 +36,8 @@ fn main() { println!("{}", get_current_hour_name()); std::process::exit(0); } - let config = get_config(); + + let config = get_config(args.config); run_windowed(&config); }