diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 37 | ||||
| -rw-r--r-- | src/main.rs | 26 | ||||
| -rw-r--r-- | src/state.rs | 5 |
3 files changed, 43 insertions, 25 deletions
diff --git a/src/config.rs b/src/config.rs index 5b657d0..f6ff47d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use serde::{de, Deserialize}; use std::collections::HashMap; use std::fs::read_to_string; use std::ops::Deref; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{env, fmt}; #[derive(Deserialize, Debug)] @@ -94,19 +94,21 @@ impl Default for Config { } impl Config { - pub fn new() -> Result<Self> { - Ok(match config_dir() { - Some(mut config_path) => { - config_path.push("i3bar-river"); - config_path.push("config.toml"); - if config_path.exists() { - let config = - read_to_string(config_path).context("Failed to read configuration")?; - toml::from_str(&config).context("Failed to deserialize configuration")? - } else { - eprintln!("Using default configuration"); - Self::default() - } + pub fn new(path: Option<&Path>) -> Result<Self> { + let buf; + + let path = match path { + Some(path) => Some(path), + None => { + buf = config_path(); + buf.as_deref() + } + }; + + Ok(match path { + Some(config_path) => { + let config = read_to_string(config_path).context("Failed to read configuration")?; + toml::from_str(&config).context("Failed to deserialize configuration")? } None => { eprintln!("Could not find the configuration path"); @@ -130,6 +132,13 @@ fn config_dir() -> Option<PathBuf> { .or_else(|| Some(PathBuf::from(env::var_os("HOME")?).join(".config"))) } +fn config_path() -> Option<PathBuf> { + let mut path = config_dir()?; + path.push("i3bar-river"); + path.push("config.toml"); + path.exists().then_some(path) +} + #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] #[serde(rename_all = "lowercase")] pub enum Position { diff --git a/src/main.rs b/src/main.rs index a4af365..d2f69d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,27 +17,35 @@ mod text; mod utils; mod wm_info_provider; -use signal_hook::consts::*; - use std::io::ErrorKind; use std::os::unix::io::AsRawFd; +use std::path::PathBuf; +use clap::Parser; +use nix::errno::Errno; +use nix::fcntl::OFlag; +use nix::poll::{poll, PollFd, PollFlags}; +use signal_hook::consts::*; use wayrs_client::{Connection, IoMode}; -use nix::{ - errno::Errno, - fcntl::OFlag, - poll::{poll, PollFd, PollFlags}, -}; - use state::State; +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + /// The path to a config file. + #[arg(short, long, value_name = "FILE")] + config: Option<PathBuf>, +} + fn main() -> anyhow::Result<()> { + let args = Cli::parse(); + let (sig_read, sig_write) = nix::unistd::pipe2(OFlag::O_NONBLOCK | OFlag::O_CLOEXEC)?; signal_hook::low_level::pipe::register(SIGUSR1, sig_write)?; let (mut conn, globals) = Connection::connect_and_collect_globals()?; - let mut state = State::new(&mut conn, &globals); + let mut state = State::new(&mut conn, &globals, args.config.as_deref()); conn.flush(IoMode::Blocking)?; let mut fds = Vec::with_capacity(3); diff --git a/src/state.rs b/src/state.rs index f98e037..dcf7fbe 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,6 +5,7 @@ use crate::wm_info_provider::*; use std::fmt::Display; use std::os::unix::io::{AsRawFd, RawFd}; +use std::path::Path; use wayrs_client::global::{GlobalExt, Globals, GlobalsExt}; use wayrs_client::proxy::Proxy; @@ -52,10 +53,10 @@ struct Pointer { } impl State { - pub fn new(conn: &mut Connection<Self>, globals: &Globals) -> Self { + pub fn new(conn: &mut Connection<Self>, globals: &Globals, config_path: Option<&Path>) -> Self { let mut error = Ok(()); - let config = Config::new() + let config = Config::new(config_path) .map_err(|e| error = Err(e)) .unwrap_or_default(); |
