From 7ff167368d32cf64e3dd8647efe7061f8e9802ce Mon Sep 17 00:00:00 2001 From: Jens Krause <47693+sectore@users.noreply.github.com> Date: Tue, 4 Feb 2025 17:39:50 +0100 Subject: [PATCH] `--features sound` (#63) to enable `sound` notification for local builds only. Needed to avoid endless issues by building the app for different platforms. Sound support can be hard. --- Cargo.toml | 7 +++++-- README.md | 8 +++++++- src/app.rs | 15 ++++++++++++--- src/args.rs | 8 ++++++-- src/main.rs | 4 +++- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7422513..576f3af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,5 +31,8 @@ notify-rust = "4.11.4" rodio = { version = "0.20.1", features = [ "symphonia-mp3", "symphonia-wav", -], default-features = false } -thiserror = "2.0.11" +], default-features = false, optional = true } +thiserror = { version = "2.0.11", optional = true } + +[features] +sound = ["dep:rodio", "dep:thiserror"] diff --git a/README.md b/README.md index 3620041..c43a778 100644 --- a/README.md +++ b/README.md @@ -77,11 +77,17 @@ Options: --menu Open the menu. -r, --reset Reset stored values to default values. -n, --notification Toggle desktop notifications on or off. Experimental. [possible values: on, off] - --sound Path to sound file (.mp3 or .wav) to play as notification. Experimental. -h, --help Print help -V, --version Print version ``` +Extra option (if `--features sound` is enabled by local build only): + +```sh + --sound Path to sound file (.mp3 or .wav) to play as notification. Experimental. +``` + + # Installation ## Cargo diff --git a/src/app.rs b/src/app.rs index 70d6072..5a84234 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,6 @@ use crate::{ common::{AppEditMode, AppTime, AppTimeFormat, Content, Notification, Style}, constants::TICK_VALUE_MS, events::{self, TuiEventHandler}, - sound::Sound, storage::AppStorage, terminal::Terminal, widgets::{ @@ -15,6 +14,10 @@ use crate::{ timer::{Timer, TimerState}, }, }; + +#[cfg(feature = "sound")] +use crate::sound::Sound; + use color_eyre::Result; use ratatui::{ buffer::Buffer, @@ -37,6 +40,7 @@ pub struct App { content: Content, mode: Mode, notification: Notification, + #[allow(dead_code)] // w/ `--features sound` available only sound_path: Option, app_time: AppTime, countdown: CountdownState, @@ -51,7 +55,6 @@ pub struct AppArgs { pub style: Style, pub with_decis: bool, pub notification: Notification, - pub sound_path: Option, pub show_menu: bool, pub app_time_format: AppTimeFormat, pub content: Content, @@ -65,6 +68,7 @@ pub struct AppArgs { pub elapsed_value_countdown: Duration, pub current_value_timer: Duration, pub app_tx: events::AppEventTx, + pub sound_path: Option, } pub struct FromAppArgs { @@ -83,7 +87,6 @@ impl From for App { with_decis: args.decis || stg.with_decis, show_menu: args.menu || stg.show_menu, notification: args.notification.unwrap_or(stg.notification), - sound_path: args.sound, app_time_format: stg.app_time_format, content: args.mode.unwrap_or(stg.content), style: args.style.unwrap_or(stg.style), @@ -100,6 +103,10 @@ impl From for App { elapsed_value_countdown: stg.elapsed_value_countdown, current_value_timer: stg.current_value_timer, app_tx, + #[cfg(feature = "sound")] + sound_path: args.sound, + #[cfg(not(feature = "sound"))] + sound_path: None, }) } } @@ -242,11 +249,13 @@ impl App { Ok(()) }; + #[allow(unused_variables)] // `app` is used by `--features sound` only // Closure to handle `AppEvent`'s let handle_app_events = |app: &mut Self, event: events::AppEvent| -> Result<()> { match event { events::AppEvent::ClockDone => { debug!("AppEvent::ClockDone"); + #[cfg(feature = "sound")] if let Some(path) = app.sound_path.clone() { _ = Sound::new(path).and_then(|sound| sound.play()).or_else( |err| -> Result<()> { diff --git a/src/args.rs b/src/args.rs index 681c95a..5a48076 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,9 +1,11 @@ use crate::{ common::{Content, Notification, Style}, - duration, sound, - sound::SoundError, + duration, }; +#[cfg(feature = "sound")] +use crate::{sound, sound::SoundError}; use clap::Parser; +#[cfg(feature = "sound")] use std::path::PathBuf; use std::time::Duration; @@ -48,6 +50,7 @@ pub struct Args { )] pub notification: Option, + #[cfg(feature = "sound")] #[arg( long, value_enum, @@ -58,6 +61,7 @@ pub struct Args { pub sound: Option, } +#[cfg(feature = "sound")] /// Custom parser for sound file fn sound_file_parser(s: &str) -> Result { let path = PathBuf::from(s); diff --git a/src/main.rs b/src/main.rs index 18fca9f..34fd765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,12 +8,14 @@ mod logging; mod args; mod duration; -mod sound; mod storage; mod terminal; mod utils; mod widgets; +#[cfg(feature = "sound")] +mod sound; + use app::{App, FromAppArgs}; use args::Args; use clap::Parser;