diff --git a/src/cli.rs b/src/cli.rs index f11543c..de2568c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,6 +7,7 @@ use clap::{Parser, ValueEnum}; about = "Bilal [A CLI salah time]", after_long_help = "Bugs can be reported on GitHub: https://github.com/azzamsa/bilal/issues" )] +#[derive(Debug)] pub struct Opts { /// A Salah mode to show #[arg(value_enum)] @@ -26,14 +27,14 @@ pub struct Opts { pub color: Color, } -#[derive(Clone, ValueEnum)] +#[derive(Debug, Clone, ValueEnum)] pub enum Mode { All, Next, Current, } -#[derive(Clone, ValueEnum)] +#[derive(Debug, Clone, ValueEnum)] pub enum Color { /// show colors if the output goes to an interactive console (default) Auto, diff --git a/src/config.rs b/src/config.rs index 4ab2819..df40bee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use serde::Deserialize; use crate::error::Error; -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Deserialize)] pub struct Config { pub latitude: f32, pub longitude: f32, @@ -19,7 +19,7 @@ pub struct Config { pub time_format: TimeFormat, } -#[derive(Debug, Clone, PartialEq, Deserialize)] +#[derive(Debug, PartialEq, Deserialize)] pub enum TimeFormat { #[serde(rename = "24H")] H24, diff --git a/src/main.rs b/src/main.rs index 4f66d5b..a3f3669 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::process; +use std::{process, sync::Arc}; use clap::Parser; use miette::Result; @@ -11,7 +11,8 @@ use bilal::{ }; fn run() -> Result<()> { - let opts = Opts::parse(); + let opts = Arc::new(Opts::parse()); + match opts.color { Color::Always => { owo_colors::set_override(true); @@ -29,9 +30,9 @@ fn run() -> Result<()> { owo_colors::set_override(false); } - let config = config::read()?; - let prayers = prayer::all(config.clone())?; - let printer = Printer::new(prayers, opts.json, config); + let config = Arc::new(config::read()?); + let prayers = prayer::all(Arc::clone(&config))?; + let printer = Printer::new(prayers, Arc::clone(&opts), config); match opts.mode { Mode::All => { diff --git a/src/output.rs b/src/output.rs index b8354d5..9cfbbd5 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,10 +1,14 @@ -use std::io::{self, Write}; +use std::{ + io::{self, Write}, + sync::Arc, +}; use owo_colors::{OwoColorize, Stream::Stdout}; use islam::salah::PrayerTimes; use crate::{ + cli::Opts, config::{Config, TimeFormat}, DateTime, }; @@ -12,15 +16,15 @@ use crate::{ #[derive(Debug)] pub struct Printer { prayers: PrayerTimes, - json_format: bool, - config: Config, + opts: Arc, + config: Arc, } impl Printer { - pub const fn new(prayers: PrayerTimes, json_format: bool, config: Config) -> Self { + pub const fn new(prayers: PrayerTimes, opts: Arc, config: Arc) -> Self { Self { prayers, - json_format, + opts, config, } } @@ -75,7 +79,7 @@ impl Printer { }; // JSON - if self.json_format { + if self.opts.json { prayer_fmt = format!( r#"{{"icon": "{}", "state": "{}", "text": "{} {}"}}"#, "bilal", state, "\u{23fa} ", prayer_fmt @@ -105,7 +109,7 @@ impl Printer { // JSON let state = "Info"; - if self.json_format { + if self.opts.json { prayer_fmt = format!( r#"{{"icon": "{}", "state": "{}", "text": "{} {}"}}"#, "bilal", state, "\u{25b6}", prayer_fmt diff --git a/src/prayer.rs b/src/prayer.rs index 1e204c0..346f2c7 100644 --- a/src/prayer.rs +++ b/src/prayer.rs @@ -1,9 +1,11 @@ +use std::sync::Arc; + use islam::salah::{Config as SalahConfig, Location, Madhab, Method, PrayerSchedule, PrayerTimes}; use crate::{config::Config, error::Error}; /// Returns all prayers -pub fn all(config: Config) -> Result { +pub fn all(config: Arc) -> Result { let latitude = config.latitude; let longitude = config.longitude; let method = method(&config.method)?;