From 339c11253f6ac50c43797ce2398b944ebaabc065 Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Mon, 13 Feb 2023 01:58:34 +0200 Subject: [PATCH] Cerate `load_config_and_parse_args` --- Cargo.lock | 44 +++++++++++++++++++++++------- Cargo.toml | 6 +++- src/arguments.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/main.rs | 4 ++- 4 files changed, 113 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d01348..8077525 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,8 +240,8 @@ checksum = "e37668cb35145dcfaa1931a5f37fde375eeae8068b4c0d2f289da28a270b2d2c" dependencies = [ "directories", "serde", + "serde_yaml", "thiserror", - "toml", ] [[package]] @@ -808,6 +808,12 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.1.4" @@ -1323,6 +1329,12 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + [[package]] name = "scoped_threadpool" version = "0.1.9" @@ -1372,6 +1384,18 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_yaml" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "sha1" version = "0.10.5" @@ -1588,15 +1612,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" -[[package]] -name = "toml" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" -dependencies = [ - "serde", -] - [[package]] name = "toml_datetime" version = "0.5.1" @@ -1936,6 +1951,15 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "zbus" version = "3.8.0" diff --git a/Cargo.toml b/Cargo.toml index f358251..0d16e4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -confy = "0.5.1" serde = "1.0.152" id3 = "1.6.0" lrc = { version = "0.1.7", optional = true } @@ -19,6 +18,11 @@ typed-builder = "0.12.0" version = "4.1.4" features = ["wrap_help", "cargo", "usage", "derive", "suggestions", "color"] +[dependencies.confy] +version = "0.5.1" +default-features = false +features = ["yaml_conf"] + [features] lyrics = ["lrc"] diff --git a/src/arguments.rs b/src/arguments.rs index 97fc259..e314bf1 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -177,3 +177,74 @@ impl Default for Arguments { } } } + +impl Arguments { + pub fn load_config_and_parse_args() -> Self { + // load config file + let cfg: Self = match confy::load("cmus-notify", "config") { + Ok(cfg) => cfg, + Err(err) => { + eprintln!("Failed to load config: {}", err); + Self::default() + } + }; + + // parse the args + let mut args = Arguments::parse(); + + // Combine the config and args(the args will override the config) + if args.timeout == NOTIFICATION_TIMEOUT { + args.timeout = cfg.timeout; + } + if args.persistent == false { + args.persistent = cfg.persistent; + } + if args.show_track_cover == true { + args.show_track_cover = cfg.show_track_cover; + } + args.notification_static_icon = args.notification_static_icon.or(cfg.notification_static_icon); + args.cover_path = args.cover_path.or(cfg.cover_path); + #[cfg(feature = "lyrics")] + if args.lyrics_path == None { + args.lyrics_path = cfg.lyrics_path; + } + + if args.depth == DEFAULT_MAX_DEPTH { + args.depth = cfg.depth; + } + if args.app_name == NOTIFICATION_APP_NAME { + args.app_name = cfg.app_name; + } + if args.summary == NOTIFICATION_SUMMARY { + args.summary = cfg.summary; + } + if args.body == NOTIFICATION_BODY { + args.body = cfg.body; + } + args.cmus_remote_bin_path = args.cmus_remote_bin_path.or(cfg.cmus_remote_bin_path); + args.cmus_socket_address = args.cmus_socket_address.or(cfg.cmus_socket_address); + args.cmus_socket_password = args.cmus_socket_password.or(cfg.cmus_socket_password); + if args.interval == DEFAULT_INTERVAL_TIME { + args.interval = cfg.interval; + } + if args.link == false { + args.link = cfg.link; + } + if args.force_use_external_cover == false { + args.force_use_external_cover = cfg.force_use_external_cover; + } + #[cfg(feature = "lyrics")] + if args.force_use_external_lyrics == false { + args.force_use_external_lyrics = cfg.force_use_external_lyrics; + } + if args.no_use_external_cover == false { + args.no_use_external_cover = cfg.no_use_external_cover; + } + #[cfg(feature = "lyrics")] + if args.no_use_external_lyrics == false { + args.no_use_external_lyrics = cfg.no_use_external_lyrics; + } + + args + } +} diff --git a/src/bin/main.rs b/src/bin/main.rs index ba671cd..da6ceac 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -4,6 +4,7 @@ use cmus_notify::{ arguments, cmus::{self, query::CmusQueryResponse, CmusError}, }; +use cmus_notify::arguments::Arguments; macro_rules! sleep { ($time: expr) => { @@ -12,7 +13,8 @@ macro_rules! sleep { } fn main() { - let args = arguments::Arguments::parse(); + // Load the configs + let settings = Arguments::load_config_and_parse_args(); // Build the command, or use the default. (to speed up the main loop, because we don't need to build it every time) let mut query_command = cmus::build_query_command(