diff --git a/src/player.rs b/src/player.rs index 6632f9c..957a771 100644 --- a/src/player.rs +++ b/src/player.rs @@ -39,7 +39,7 @@ pub enum Messages { /// Pauses the [Sink]. This will also unpause it if it is paused. Pause, - /// Change the volume of playback + /// Change the volume of playback. ChangeVolume(f32), } diff --git a/src/player/ui.rs b/src/player/ui.rs index 6ac8dd1..a371e22 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -14,7 +14,7 @@ use crate::Args; use super::Player; use crossterm::{ cursor::{Hide, MoveTo, MoveToColumn, MoveUp, Show}, - event::{self, KeyCode, KeyModifiers}, + event::{self, KeyCode, KeyModifiers, KeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, style::{Print, Stylize}, terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -100,12 +100,19 @@ async fn interface(player: Arc) -> eyre::Result<()> { pub async fn start(queue: Arc, sender: Sender, args: Args) -> eyre::Result<()> { crossterm::execute!(stdout(), Hide)?; - terminal::enable_raw_mode()?; - if args.alternate { crossterm::execute!(stdout(), EnterAlternateScreen, MoveTo(0, 0))?; } + terminal::enable_raw_mode()?; + + if terminal::supports_keyboard_enhancement()? { + crossterm::execute!( + stdout(), + PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES,) + )?; + } + task::spawn(interface(Arc::clone(&queue))); loop { @@ -119,7 +126,7 @@ pub async fn start(queue: Arc, sender: Sender, args: Args) -> KeyCode::Right => Messages::ChangeVolume(0.01), KeyCode::Down => Messages::ChangeVolume(-0.1), KeyCode::Left => Messages::ChangeVolume(-0.01), - KeyCode::Char(character) => match character { + KeyCode::Char(character) => match character.to_ascii_lowercase() { // Ctrl+C 'c' if event.modifiers == KeyModifiers::CONTROL => break, @@ -137,6 +144,18 @@ pub async fn start(queue: Arc, sender: Sender, args: Args) -> '-' | '_' => Messages::ChangeVolume(-0.1), _ => continue, }, + // Media keys + KeyCode::Media(media) => match media { + event::MediaKeyCode::Play => Messages::Pause, + event::MediaKeyCode::Pause => Messages::Pause, + event::MediaKeyCode::PlayPause => Messages::Pause, + event::MediaKeyCode::Stop => Messages::Pause, + event::MediaKeyCode::TrackNext => Messages::Next, + event::MediaKeyCode::LowerVolume => Messages::ChangeVolume(-0.1), + event::MediaKeyCode::RaiseVolume => Messages::ChangeVolume(0.1), + event::MediaKeyCode::MuteVolume => Messages::ChangeVolume(-1.0), + _ => continue, + }, _ => continue, }; diff --git a/src/player/ui/components.rs b/src/player/ui/components.rs index f7ce03c..95bb4eb 100644 --- a/src/player/ui/components.rs +++ b/src/player/ui/components.rs @@ -102,11 +102,10 @@ pub fn action(player: &Arc, width: usize) -> String { } } +/// Creates the bottom controls bar, and also spaces it properly. pub fn controls(width: usize) -> String { let controls = [["[s]", "kip"], ["[p]", "ause"], ["[q]", "uit"]]; - let len: usize = controls.concat().iter().map(|x| x.len()).sum(); - let controls = controls.map(|x| format!("{}{}", x[0].bold(), x[1])); controls.join(&" ".repeat((width - len) / (controls.len() - 1)))