diff --git a/Cargo.lock b/Cargo.lock index 0473a2b..ac83692 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,9 +78,9 @@ dependencies = [ [[package]] name = "clap" -version = "3.1.17" +version = "3.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47582c09be7c8b32c0ab3a6181825ababb713fde6fff20fc573a3870dd45c6a0" +checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b" dependencies = [ "atty", "bitflags", @@ -95,9 +95,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.1.7" +version = "3.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" +checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c" dependencies = [ "heck", "proc-macro-error", @@ -641,6 +641,7 @@ version = "0.2.1" dependencies = [ "battery", "chrono", + "clap", "directories", "env_logger", "log", @@ -657,9 +658,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" +checksum = "04066589568b72ec65f42d65a1a52436e954b168773148893c020269563decf2" dependencies = [ "proc-macro2", "quote", @@ -668,9 +669,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.23.11" +version = "0.23.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bf915673a340ee41f2fc24ad1286c75ea92026f04b65a0d0e5132d80b95fc61" +checksum = "56b1e20ee77901236c389ff74618a899ff5fd34719a7ff0fd1d64f0acca5179a" dependencies = [ "cfg-if", "core-foundation-sys 0.8.3", diff --git a/swayr/src/shared/cfg.rs b/swayr/src/shared/cfg.rs index 5985710..68593b0 100644 --- a/swayr/src/shared/cfg.rs +++ b/swayr/src/shared/cfg.rs @@ -74,7 +74,7 @@ where + "I've created a fresh config for use with wofi for you in " + &path.to_string_lossy() + ". Adapt it to your needs." - }else{ + } else { "Welcome to swayrbar! ".to_owned() + "I've created a fresh config for for you in " + &path.to_string_lossy() @@ -88,14 +88,28 @@ where .spawn() .ok(); log::debug!("Created new config in {}.", path.to_string_lossy()); + } + + load_config_file(&path) +} + +pub fn load_config_file(config_file: &Path) -> T +where + T: Serialize + DeserializeOwned + Default, +{ + if !config_file.exists() { + panic!( + "Config file {} does not exist.", + config_file.to_string_lossy() + ); } else { - log::debug!("Loaded config from {}.", path.to_string_lossy()); + log::debug!("Loading config from {}.", config_file.to_string_lossy()); } let mut file = OpenOptions::new() .read(true) .write(false) .create(false) - .open(path) + .open(config_file) .unwrap(); let mut buf: String = String::new(); file.read_to_string(&mut buf).unwrap(); diff --git a/swayrbar/Cargo.toml b/swayrbar/Cargo.toml index 6800d50..16c4e92 100644 --- a/swayrbar/Cargo.toml +++ b/swayrbar/Cargo.toml @@ -9,6 +9,7 @@ authors = ["Tassilo Horn "] license = "GPL-3.0+" [dependencies] +clap = {version = "3.0.0", features = ["derive"] } battery = "0.7.8" chrono = "0.4" directories = "4.0" diff --git a/swayrbar/src/bar.rs b/swayrbar/src/bar.rs index 16a2561..ea51ce4 100644 --- a/swayrbar/src/bar.rs +++ b/swayrbar/src/bar.rs @@ -21,6 +21,7 @@ use crate::module::{BarModuleFn, NameInstanceAndReason, RefreshReason}; use env_logger::Env; use serde_json; use std::io; +use std::path::Path; use std::process as p; use std::sync::mpsc::sync_channel; use std::sync::mpsc::Receiver; @@ -30,11 +31,30 @@ use std::{sync::Arc, thread}; use swaybar_types as sbt; use swayipc as si; -pub fn start() { +#[derive(clap::Parser)] +#[clap(about, version, author)] +pub struct Opts { + #[clap( + short = 'c', + long, + help = "Path to a config.toml configuration file. +If not specified, the default config ~/.config/swayrbar/config.toml or +/etc/xdg/swayrbar/config.toml is used." + )] + config_file: Option, +} + +pub fn start(opts: Opts) { env_logger::Builder::from_env(Env::default().default_filter_or("warn")) .init(); - let config = config::load_config(); + let config = match opts.config_file { + None => config::load_config(), + Some(config_file) => { + let path = Path::new(&config_file); + crate::shared::cfg::load_config_file(path) + } + }; let refresh_interval = config.refresh_interval; let mods: Arc>> = Arc::new(create_modules(config)); let mods_for_input = mods.clone(); diff --git a/swayrbar/src/bin/swayrbar.rs b/swayrbar/src/bin/swayrbar.rs index 582173a..2ddd056 100644 --- a/swayrbar/src/bin/swayrbar.rs +++ b/swayrbar/src/bin/swayrbar.rs @@ -15,8 +15,10 @@ //! The `swayrbar` binary. +use clap::Parser; +use swayrbar::bar::Opts; + fn main() { - // TODO: We need a config file cmd line option so that each bar can have - // its own config. - swayrbar::bar::start() + let opts: Opts = Opts::parse(); + swayrbar::bar::start(opts); }