From 0ef844a3f2fbcc4c1f38bcf9abd1509090caa823 Mon Sep 17 00:00:00 2001 From: talwat <83217276+talwat@users.noreply.github.com> Date: Fri, 4 Oct 2024 00:09:39 +0200 Subject: [PATCH] feat: improve slightly media key support --- src/play.rs | 2 +- src/player.rs | 8 ++++++-- src/player/ui.rs | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/play.rs b/src/play.rs index b47842b..2dee2dd 100644 --- a/src/play.rs +++ b/src/play.rs @@ -13,7 +13,7 @@ use crate::Args; pub async fn play(args: Args) -> eyre::Result<()> { let (tx, rx) = mpsc::channel(8); - let player = Arc::new(Player::new().await?); + let player = Arc::new(Player::new(args.alternate).await?); let audio = task::spawn(Player::play(Arc::clone(&player), tx.clone(), rx)); tx.send(Messages::Init).await?; diff --git a/src/player.rs b/src/player.rs index 957a771..67c0498 100644 --- a/src/player.rs +++ b/src/player.rs @@ -106,8 +106,12 @@ impl Player { } /// Initializes the entire player, including audio devices & sink. - pub async fn new() -> eyre::Result { - let (_stream, handle) = if cfg!(target_os = "linux") { + /// + /// `silent` can control whether alsa's output should be redirected, + /// but this option is only applicable on Linux, as on MacOS & Windows + /// it will never be silent. + pub async fn new(silent: bool) -> eyre::Result { + let (_stream, handle) = if silent && cfg!(target_os = "linux") { Self::silent_get_output_stream()? } else { OutputStream::try_default()? diff --git a/src/player/ui.rs b/src/player/ui.rs index a371e22..c0a10ea 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -14,7 +14,10 @@ use crate::Args; use super::Player; use crossterm::{ cursor::{Hide, MoveTo, MoveToColumn, MoveUp, Show}, - event::{self, KeyCode, KeyModifiers, KeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, + event::{ + self, KeyCode, KeyModifiers, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, + PushKeyboardEnhancementFlags, + }, style::{Print, Stylize}, terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -105,11 +108,12 @@ pub async fn start(queue: Arc, sender: Sender, args: Args) -> } terminal::enable_raw_mode()?; + let enhancement = terminal::supports_keyboard_enhancement()?; - if terminal::supports_keyboard_enhancement()? { + if enhancement { crossterm::execute!( stdout(), - PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES,) + PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES) )?; } @@ -173,6 +177,11 @@ pub async fn start(queue: Arc, sender: Sender, args: Args) -> } crossterm::execute!(stdout(), Clear(ClearType::FromCursorDown), Show)?; + + if enhancement { + crossterm::execute!(stdout(), PopKeyboardEnhancementFlags)?; + } + terminal::disable_raw_mode()?; Ok(())