From 4897d979313cc0ad7c8a8acd1a459b4f5aa3cbd2 Mon Sep 17 00:00:00 2001 From: Rasmus Larsen Date: Thu, 9 May 2019 16:04:07 +0200 Subject: [PATCH] Move URL handling to relevant structs and share selected song --- src/album.rs | 6 ++++++ src/artist.rs | 6 ++++++ src/commands.rs | 21 +++++++++++---------- src/playlist.rs | 7 +++++++ src/track.rs | 6 ++++++ src/traits.rs | 1 + src/ui/listview.rs | 21 +++++++++++++++++++++ 7 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/album.rs b/src/album.rs index 9d5699b..93016d4 100644 --- a/src/album.rs +++ b/src/album.rs @@ -199,6 +199,12 @@ impl ListItem for Album { Some(AlbumView::new(queue, library, self).as_boxed_view_ext()) } + fn share_url(&self) -> Option { + self.id + .clone() + .map(|id| format!("https://open.spotify.com/album/{}", id)) + } + fn artist(&self) -> Option { Some(Artist::new( self.artist_ids[0].clone(), diff --git a/src/artist.rs b/src/artist.rs index 698bf2d..603ab24 100644 --- a/src/artist.rs +++ b/src/artist.rs @@ -193,4 +193,10 @@ impl ListItem for Artist { fn open(&self, queue: Arc, library: Arc) -> Option> { Some(ArtistView::new(queue, library, self).as_boxed_view_ext()) } + + fn share_url(&self) -> Option { + self.id + .clone() + .map(|id| format!("https://open.spotify.com/artist/{}", id)) + } } diff --git a/src/commands.rs b/src/commands.rs index 84d50dc..94af581 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -10,7 +10,7 @@ use clipboard::{ClipboardContext, ClipboardProvider}; use library::Library; use queue::{Queue, RepeatSetting}; use spotify::Spotify; -use traits::ViewExt; +use traits::{ListItem, ViewExt}; use ui::layout::Layout; type CommandCb = dyn Fn(&mut Cursive, &[String]) -> Result, String>; @@ -202,14 +202,14 @@ impl CommandManager { let queue = queue.clone(); self.register_command( "share", - Some(Box::new(move |_, _| { - if let Some(url) = queue - .get_current() - .and_then(|t| t.id) - .map(|id| format!("https://open.spotify.com/track/{}", id)) - { - let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); - ctx.set_contents(url).unwrap(); + Some(Box::new(move |_, args| { + if let Some(url) = args.get(0).and_then(|source| match source.as_str() { + "current" => queue.get_current().and_then(|t| t.share_url()), + _ => None, + }) { + ClipboardProvider::new() + .and_then(|mut ctx: ClipboardContext| ctx.set_contents(url)) + .unwrap(); } Ok(None) @@ -341,7 +341,8 @@ impl CommandManager { kb.insert(",".into(), "seek -500".into()); kb.insert("r".into(), "repeat".into()); kb.insert("z".into(), "shuffle".into()); - kb.insert("x".into(), "share".into()); + kb.insert("x".into(), "share current".into()); + kb.insert("Shift+x".into(), "share selected".into()); kb.insert("F1".into(), "focus queue".into()); kb.insert("F2".into(), "focus search".into()); diff --git a/src/playlist.rs b/src/playlist.rs index bd32498..401f3df 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -166,4 +166,11 @@ impl ListItem for Playlist { fn open(&self, queue: Arc, library: Arc) -> Option> { Some(PlaylistView::new(queue, library, self).as_boxed_view_ext()) } + + fn share_url(&self) -> Option { + Some(format!( + "https://open.spotify.com/user/{}/{}", + self.owner_id, self.id + )) + } } diff --git a/src/track.rs b/src/track.rs index 1267e3b..ecff57b 100644 --- a/src/track.rs +++ b/src/track.rs @@ -189,6 +189,12 @@ impl ListItem for Track { None } + fn share_url(&self) -> Option { + self.id + .clone() + .map(|id| format!("https://open.spotify.com/track/{}", id)) + } + fn album(&self, queue: Arc) -> Option { let spotify = queue.get_spotify(); diff --git a/src/traits.rs b/src/traits.rs index 62bd0a4..aae2c70 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -18,6 +18,7 @@ pub trait ListItem: Sync + Send + 'static { fn queue(&mut self, queue: Arc); fn toggle_saved(&mut self, library: Arc); fn open(&self, queue: Arc, library: Arc) -> Option>; + fn share_url(&self) -> Option; fn album(&self, _queue: Arc) -> Option { None diff --git a/src/ui/listview.rs b/src/ui/listview.rs index 50da0f3..00f4b69 100644 --- a/src/ui/listview.rs +++ b/src/ui/listview.rs @@ -9,6 +9,7 @@ use cursive::view::ScrollBase; use cursive::{Cursive, Printer, Rect, Vec2}; use unicode_width::UnicodeWidthStr; +use clipboard::{ClipboardContext, ClipboardProvider}; use commands::CommandResult; use library::Library; use queue::Queue; @@ -314,6 +315,26 @@ impl ViewExt for ListView { } } + if cmd == "share" { + return args.get(0).map_or_else( + || Err("wrong number of parameters".to_string()), + |source| match source.as_str() { + "selected" => { + if let Some(url) = self.content.read().ok().and_then(|content| { + content.get(self.selected).and_then(ListItem::share_url) + }) { + ClipboardProvider::new() + .and_then(|mut ctx: ClipboardContext| ctx.set_contents(url)) + .unwrap(); + } + + Ok(CommandResult::Consumed(None)) + } + _ => Ok(CommandResult::Ignored), + }, + ); + } + if cmd == "move" { if let Some(dir) = args.get(0) { let amount: usize = args