swayrbar: Add -c/--config-file command line option

main
Tassilo Horn 3 years ago
parent 90f43c0ec9
commit 6b87e5e51d
  1. 17
      Cargo.lock
  2. 20
      swayr/src/shared/cfg.rs
  3. 1
      swayrbar/Cargo.toml
  4. 24
      swayrbar/src/bar.rs
  5. 8
      swayrbar/src/bin/swayrbar.rs

17
Cargo.lock generated

@ -78,9 +78,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.1.17" version = "3.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47582c09be7c8b32c0ab3a6181825ababb713fde6fff20fc573a3870dd45c6a0" checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b"
dependencies = [ dependencies = [
"atty", "atty",
"bitflags", "bitflags",
@ -95,9 +95,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "3.1.7" version = "3.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error", "proc-macro-error",
@ -641,6 +641,7 @@ version = "0.2.1"
dependencies = [ dependencies = [
"battery", "battery",
"chrono", "chrono",
"clap",
"directories", "directories",
"env_logger", "env_logger",
"log", "log",
@ -657,9 +658,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.92" version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" checksum = "04066589568b72ec65f42d65a1a52436e954b168773148893c020269563decf2"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -668,9 +669,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.23.11" version = "0.23.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bf915673a340ee41f2fc24ad1286c75ea92026f04b65a0d0e5132d80b95fc61" checksum = "56b1e20ee77901236c389ff74618a899ff5fd34719a7ff0fd1d64f0acca5179a"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"core-foundation-sys 0.8.3", "core-foundation-sys 0.8.3",

@ -74,7 +74,7 @@ where
+ "I've created a fresh config for use with wofi for you in " + "I've created a fresh config for use with wofi for you in "
+ &path.to_string_lossy() + &path.to_string_lossy()
+ ". Adapt it to your needs." + ". Adapt it to your needs."
}else{ } else {
"Welcome to swayrbar! ".to_owned() "Welcome to swayrbar! ".to_owned()
+ "I've created a fresh config for for you in " + "I've created a fresh config for for you in "
+ &path.to_string_lossy() + &path.to_string_lossy()
@ -88,14 +88,28 @@ where
.spawn() .spawn()
.ok(); .ok();
log::debug!("Created new config in {}.", path.to_string_lossy()); log::debug!("Created new config in {}.", path.to_string_lossy());
}
load_config_file(&path)
}
pub fn load_config_file<T>(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 { } 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() let mut file = OpenOptions::new()
.read(true) .read(true)
.write(false) .write(false)
.create(false) .create(false)
.open(path) .open(config_file)
.unwrap(); .unwrap();
let mut buf: String = String::new(); let mut buf: String = String::new();
file.read_to_string(&mut buf).unwrap(); file.read_to_string(&mut buf).unwrap();

@ -9,6 +9,7 @@ authors = ["Tassilo Horn <tsdh@gnu.org>"]
license = "GPL-3.0+" license = "GPL-3.0+"
[dependencies] [dependencies]
clap = {version = "3.0.0", features = ["derive"] }
battery = "0.7.8" battery = "0.7.8"
chrono = "0.4" chrono = "0.4"
directories = "4.0" directories = "4.0"

@ -21,6 +21,7 @@ use crate::module::{BarModuleFn, NameInstanceAndReason, RefreshReason};
use env_logger::Env; use env_logger::Env;
use serde_json; use serde_json;
use std::io; use std::io;
use std::path::Path;
use std::process as p; use std::process as p;
use std::sync::mpsc::sync_channel; use std::sync::mpsc::sync_channel;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
@ -30,11 +31,30 @@ use std::{sync::Arc, thread};
use swaybar_types as sbt; use swaybar_types as sbt;
use swayipc as si; 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<String>,
}
pub fn start(opts: Opts) {
env_logger::Builder::from_env(Env::default().default_filter_or("warn")) env_logger::Builder::from_env(Env::default().default_filter_or("warn"))
.init(); .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 refresh_interval = config.refresh_interval;
let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(create_modules(config)); let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(create_modules(config));
let mods_for_input = mods.clone(); let mods_for_input = mods.clone();

@ -15,8 +15,10 @@
//! The `swayrbar` binary. //! The `swayrbar` binary.
use clap::Parser;
use swayrbar::bar::Opts;
fn main() { fn main() {
// TODO: We need a config file cmd line option so that each bar can have let opts: Opts = Opts::parse();
// its own config. swayrbar::bar::start(opts);
swayrbar::bar::start()
} }

Loading…
Cancel
Save