diff options
| -rw-r--r-- | Cargo.lock | 46 | ||||
| -rw-r--r-- | Cargo.toml | 12 | ||||
| -rw-r--r-- | src/config.rs | 37 | ||||
| -rw-r--r-- | src/main.rs | 26 | ||||
| -rw-r--r-- | src/state.rs | 5 |
5 files changed, 95 insertions, 31 deletions
@@ -3,6 +3,12 @@ version = 3 [[package]] +name = "anstyle" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" + +[[package]] name = "anyhow" version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -62,6 +68,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] +name = "clap" +version = "4.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03aef18ddf7d879c15ce20f04826ef8418101c7e528014c3eeea13321047dca3" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ce6fffb678c9b80a70b6b6de0aad31df727623a70fd9a842c30cd573e2fa98" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_derive" +version = "4.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + +[[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -239,6 +284,7 @@ name = "i3bar-river" version = "0.1.3" dependencies = [ "anyhow", + "clap", "memchr", "nix", "pangocairo", @@ -13,17 +13,17 @@ hyprland = [] [dependencies] anyhow = "1" -toml = { version = "0.7", default-features = false, features = ["parse"] } +memchr = "2" +nix = { version = "0.26", default-features = false, features = ["fs", "poll"] } pangocairo = "0.17" -serde = { version = "1", features = ["derive"] } serde_json = "1" -memchr = "2" +serde = { version = "1", features = ["derive"] } signal-hook = { version = "0.3", default-features = false } -nix = { version = "0.26", default-features = false, features = ["fs", "poll"] } - +toml = { version = "0.7", default-features = false, features = ["parse"] } wayrs-client = "0.11" -wayrs-utils = { version = "0.9", features = ["cursor", "shm_alloc", "seats"] } wayrs-protocols = { version = "0.11", features = ["wlr-layer-shell-unstable-v1", "viewporter", "fractional-scale-v1"] } +wayrs-utils = { version = "0.9", features = ["cursor", "shm_alloc", "seats"] } +clap = { version = "4.3.23", default-features = false, features = ["derive", "std", "help"] } [profile.release] lto = "fat" 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(); |
