From 3f083d3ee5a80231eebb6218a04403cd8e378b39 Mon Sep 17 00:00:00 2001 From: 409 Date: Sat, 29 Jun 2024 22:19:53 +0200 Subject: [PATCH] feat: --silent flag to disable notifications --- src/main.rs | 15 +++++++++++---- src/mixer.rs | 54 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index b56bd93..b1fb6fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,26 @@ mod instructions; pub mod mixer; +pub mod playerctl; pub mod pulseaudio; pub mod utils; -pub mod playerctl; use mixer::Mixer; use pulseaudio::PulseInstruction; -use std::sync::mpsc::channel; +use std::{env, sync::mpsc::channel}; fn main() { - let mainloop = pulse::mainloop::standard::Mainloop::new().expect("Error getting PulseAudio main loop"); + let mainloop = + pulse::mainloop::standard::Mainloop::new().expect("Error getting PulseAudio main loop"); + + let args: Vec = env::args().collect(); + let silent_mode = match args.iter().nth(1) { + Some(arg) => arg == "--silent", + None => false, + }; let (pulse_ix_tx, pulse_ix_rx) = channel::(); - let mut mixer = Mixer::new(mainloop, pulse_ix_tx); + let mut mixer = Mixer::new(mainloop, pulse_ix_tx, silent_mode); mixer.run(pulse_ix_rx); } diff --git a/src/mixer.rs b/src/mixer.rs index ebae9c6..ce1be16 100644 --- a/src/mixer.rs +++ b/src/mixer.rs @@ -42,10 +42,15 @@ pub struct Mixer { selected_index: Arc>>, mainloop: Mainloop, context: pulse::context::Context, + silent_mode: bool, } impl Mixer { - pub fn new(mut mainloop: Mainloop, pulse_ix_tx: Sender) -> Self { + pub fn new( + mut mainloop: Mainloop, + pulse_ix_tx: Sender, + silent_mode: bool, + ) -> Self { let mut context = pulse::context::Context::new(&mainloop, "Mixrs").expect("Error creating pulse context"); @@ -105,6 +110,7 @@ impl Mixer { selected_index, mainloop, context, + silent_mode, } } @@ -423,15 +429,18 @@ impl Mixer { .set_sink_input_volume( *sink_index, &volume, - Some(Box::new(move |success| { - if success { - let volume = volume_to_percentage(volume); - let _ = send_notification_with_progress( - &format!("{sink_name}: {}%", volume), - volume, - ); - } - })), + match self.silent_mode { + true => None, + false => Some(Box::new(move |success| { + if success { + let volume = volume_to_percentage(volume); + let _ = send_notification_with_progress( + &format!("{sink_name}: {}%", volume), + volume, + ); + } + })), + }, ); } @@ -466,19 +475,26 @@ impl Mixer { .set_sink_input_volume( *sink_index, &volume, - Some(Box::new(move |success| { - if success { - let volume = volume_to_percentage(volume); - let _ = send_notification_with_progress( - &format!("{sink_name}: {}%", volume), - volume, - ); - } - })), + match self.silent_mode { + true => None, + false => Some(Box::new(move |success| { + if success { + let volume = volume_to_percentage(volume); + let _ = send_notification_with_progress( + &format!("{sink_name}: {}%", volume), + volume, + ); + } + })), + }, ); } pub fn get_current(&self) { + if self.silent_mode { + return; + } + let index_lock = self.selected_index.lock().unwrap(); let Some(index) = *index_lock else {