From d2915316dac965942e57da77c63f6d50eaa80aa0 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Fri, 21 Feb 2020 23:33:08 +0100 Subject: [PATCH] various non-functional code cleanups --- src/authentication.rs | 2 +- src/command.rs | 10 +++++++--- src/library.rs | 2 +- src/main.rs | 8 ++------ src/mpris.rs | 2 -- src/queue.rs | 6 +++++- src/spotify.rs | 5 ++--- src/ui/album.rs | 6 +----- src/ui/artist.rs | 5 ++--- src/ui/layout.rs | 2 +- src/ui/library.rs | 2 +- src/ui/listview.rs | 8 ++------ src/ui/playlists.rs | 2 +- src/ui/search.rs | 28 ++++++++++++++-------------- src/ui/statusbar.rs | 2 +- 15 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/authentication.rs b/src/authentication.rs index 525735a..77676f5 100644 --- a/src/authentication.rs +++ b/src/authentication.rs @@ -15,7 +15,7 @@ pub fn create_credentials(path: &Path) -> Result { "Cannot read config file from {}\n", path.to_str().unwrap() )); - let info_view = Dialog::around(TextView::new_with_content(info_buf.clone())) + let info_view = Dialog::around(TextView::new_with_content(info_buf)) .button("Login", move |s| { let login_view = Dialog::new() .title("Spotify login") diff --git a/src/command.rs b/src/command.rs index 63cdfdd..b862092 100644 --- a/src/command.rs +++ b/src/command.rs @@ -116,7 +116,7 @@ impl fmt::Display for Command { format!("repeat {}", param) } Command::Shuffle(on) => { - let param = on.map(|x| if x == true { "on" } else { "off" }); + let param = on.map(|x| if x { "on" } else { "off" }); format!("shuffle {}", param.unwrap_or("")) } Command::Share(mode) => format!("share {}", mode), @@ -190,7 +190,9 @@ pub fn parse(input: &str) -> Option { _ => None, }) .map(Command::Open), - "search" => args.get(0).map(|query| Command::Search(query.to_string())), + "search" => args + .get(0) + .map(|query| Command::Search((*query).to_string())), "shift" => { let amount = args.get(1).and_then(|amount| amount.parse().ok()); @@ -268,7 +270,9 @@ pub fn parse(input: &str) -> Option { .ok() .map(|amount| Command::Seek(SeekDirection::Absolute(amount))), }), - "focus" => args.get(0).map(|target| Command::Focus(target.to_string())), + "focus" => args + .get(0) + .map(|target| Command::Focus((*target).to_string())), "save" => args.get(0).map(|target| match *target { "queue" => Command::SaveQueue, _ => Command::Save, diff --git a/src/library.rs b/src/library.rs index 4fe6194..7943c7f 100644 --- a/src/library.rs +++ b/src/library.rs @@ -766,7 +766,7 @@ impl Library { { let mut store = self.playlists.write().unwrap(); if !store.iter().any(|p| p.id == playlist.id) { - store.insert(0, playlist.clone()); + store.insert(0, playlist); } } diff --git a/src/main.rs b/src/main.rs index c0f5f87..a4343f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -215,11 +215,8 @@ fn main() { let helpview = ui::help::HelpView::new(cmd_manager.keybindings().clone()); - let status = ui::statusbar::StatusBar::new( - queue.clone(), - library.clone(), - cfg.use_nerdfont.unwrap_or(false), - ); + let status = + ui::statusbar::StatusBar::new(queue.clone(), library, cfg.use_nerdfont.unwrap_or(false)); let mut layout = ui::layout::Layout::new(status, &event_manager, theme) .view("search", search.with_name("search"), "Search") @@ -246,7 +243,6 @@ fn main() { { let ev = event_manager.clone(); - let cmd_manager = cmd_manager.clone(); layout.cmdline.set_on_submit(move |s, cmd| { { let mut main = s.find_name::("main").unwrap(); diff --git a/src/mpris.rs b/src/mpris.rs index 1db2391..01089a6 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -367,7 +367,6 @@ fn run_dbus_server(spotify: Arc, queue: Arc, rx: mpsc::Receiver< }; let method_previous = { - let queue = queue.clone(); let spotify = spotify.clone(); f.method("Previous", (), move |m| { if spotify.get_current_progress() < Duration::from_secs(5) { @@ -388,7 +387,6 @@ fn run_dbus_server(spotify: Arc, queue: Arc, rx: mpsc::Receiver< }; let method_rewind = { - let spotify = spotify.clone(); f.method("Rewind", (), move |m| { spotify.seek_relative(-5000); Ok(vec![m.msg.method_return()]) diff --git a/src/queue.rs b/src/queue.rs index 5d984cb..ca93397 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -229,7 +229,11 @@ impl Queue { self.play(index, false, false); } else if repeat == RepeatSetting::RepeatPlaylist && q.len() > 0 { let random_order = self.random_order.read().unwrap(); - self.play(random_order.as_ref().map(|o| o[0]).unwrap_or(0), false, false); + self.play( + random_order.as_ref().map(|o| o[0]).unwrap_or(0), + false, + false, + ); } else { self.spotify.stop(); } diff --git a/src/spotify.rs b/src/spotify.rs index b0b8b0e..cc494f2 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -222,7 +222,6 @@ impl Spotify { let (tx, rx) = mpsc::unbounded(); { - let events = events.clone(); thread::spawn(move || { Self::worker(events, rx, player_config, credentials, user_tx, volume) }); @@ -731,7 +730,7 @@ impl Spotify { self.volume.load(Ordering::Relaxed) as u16 } - fn to_log_scale(volume: u16) -> u16 { + fn log_scale(volume: u16) -> u16 { // https://www.dr-lex.be/info-stuff/volumecontrols.html#ideal2 // a * exp(b * x) const A: f64 = 1.0 / 1000.0; @@ -752,7 +751,7 @@ impl Spotify { info!("setting volume to {}", volume); self.volume.store(volume, Ordering::Relaxed); self.channel - .unbounded_send(WorkerCommand::SetVolume(Self::to_log_scale(volume))) + .unbounded_send(WorkerCommand::SetVolume(Self::log_scale(volume))) .unwrap(); } } diff --git a/src/ui/album.rs b/src/ui/album.rs index 4f2a194..245b0b6 100644 --- a/src/ui/album.rs +++ b/src/ui/album.rs @@ -50,11 +50,7 @@ impl AlbumView { .tab( "artists", "Artists", - ListView::new( - Arc::new(RwLock::new(artists)), - queue.clone(), - library.clone(), - ), + ListView::new(Arc::new(RwLock::new(artists)), queue, library), ); Self { album, tabs } diff --git a/src/ui/artist.rs b/src/ui/artist.rs index d07d32c..ea19d3c 100644 --- a/src/ui/artist.rs +++ b/src/ui/artist.rs @@ -51,7 +51,6 @@ impl ArtistView { let related: Arc>> = Arc::new(RwLock::new(Vec::new())); { let related = related.clone(); - let spotify = spotify.clone(); let id = artist.id.clone(); let library = library.clone(); thread::spawn(move || { @@ -83,7 +82,7 @@ impl ArtistView { tabs.add_tab( "top_tracks", "Top 10", - ListView::new(top_tracks.clone(), queue.clone(), library.clone()), + ListView::new(top_tracks, queue.clone(), library.clone()), ); tabs.add_tab( @@ -99,7 +98,7 @@ impl ArtistView { tabs.add_tab( "related", "Related Artists", - ListView::new(related.clone(), queue.clone(), library.clone()), + ListView::new(related, queue, library), ); Self { artist, tabs } diff --git a/src/ui/layout.rs b/src/ui/layout.rs index 8cdefab..1fcc6ae 100644 --- a/src/ui/layout.rs +++ b/src/ui/layout.rs @@ -242,7 +242,7 @@ impl View for Layout { if position.y < self.last_size.y.saturating_sub(2 + cmdline_height) { if let Some(ref id) = self.focus { let screen = self.views.get_mut(id).unwrap(); - screen.view.on_event(event.clone()); + screen.view.on_event(event); } } else if position.y < self.last_size.y - cmdline_height { self.statusbar.on_event( diff --git a/src/ui/library.rs b/src/ui/library.rs index 3163d6d..59a28c7 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -37,7 +37,7 @@ impl LibraryView { .tab( "playlists", "Playlists", - PlaylistsView::new(queue.clone(), library.clone()), + PlaylistsView::new(queue, library.clone()), ); Self { tabs } diff --git a/src/ui/listview.rs b/src/ui/listview.rs index dc54b80..29f553d 100644 --- a/src/ui/listview.rs +++ b/src/ui/listview.rs @@ -387,14 +387,10 @@ impl ViewExt for ListView { let queue = self.queue.clone(); let library = self.library.clone(); let target: Option> = match mode { - TargetMode::Current => { - self.queue.get_current().and_then(|t| Some(t.as_listitem())) - } + TargetMode::Current => self.queue.get_current().map(|t| t.as_listitem()), TargetMode::Selected => { let content = self.content.read().unwrap(); - content - .get(self.selected) - .and_then(|t| Some(t.as_listitem())) + content.get(self.selected).map(|t| t.as_listitem()) } }; diff --git a/src/ui/playlists.rs b/src/ui/playlists.rs index 8207874..2b7646f 100644 --- a/src/ui/playlists.rs +++ b/src/ui/playlists.rs @@ -21,7 +21,7 @@ pub struct PlaylistsView { impl PlaylistsView { pub fn new(queue: Arc, library: Arc) -> Self { Self { - list: ListView::new(library.playlists.clone(), queue.clone(), library.clone()), + list: ListView::new(library.playlists.clone(), queue, library.clone()), library, } } diff --git a/src/ui/search.rs b/src/ui/search.rs index 52c7da2..2acd7d4 100644 --- a/src/ui/search.rs +++ b/src/ui/search.rs @@ -73,8 +73,7 @@ impl SearchView { let pagination_albums = list_albums.get_pagination().clone(); let list_artists = ListView::new(results_artists.clone(), queue.clone(), library.clone()); let pagination_artists = list_artists.get_pagination().clone(); - let list_playlists = - ListView::new(results_playlists.clone(), queue.clone(), library.clone()); + let list_playlists = ListView::new(results_playlists.clone(), queue, library); let pagination_playlists = list_playlists.get_pagination().clone(); let tabs = TabView::new() @@ -272,19 +271,20 @@ impl SearchView { let total_items = handler(&spotify, &results, &query, 0, false) as usize; // register paginator if the API has more than one page of results - if paginator.is_some() && total_items > results.read().unwrap().len() { - let mut paginator = paginator.unwrap(); - let ev = ev.clone(); + if let Some(mut paginator) = paginator { + if total_items > results.read().unwrap().len() { + let ev = ev.clone(); - // paginator callback - let cb = move |items: Arc>>| { - let offset = items.read().unwrap().len(); - handler(&spotify, &results, &query, offset, true); - ev.trigger(); - }; - paginator.set(total_items, Box::new(cb)); - } else if let Some(mut p) = paginator { - p.clear() + // paginator callback + let cb = move |items: Arc>>| { + let offset = items.read().unwrap().len(); + handler(&spotify, &results, &query, offset, true); + ev.trigger(); + }; + paginator.set(total_items, Box::new(cb)); + } else { + paginator.clear() + } } ev.trigger(); }); diff --git a/src/ui/statusbar.rs b/src/ui/statusbar.rs index ff1732e..f960366 100644 --- a/src/ui/statusbar.rs +++ b/src/ui/statusbar.rs @@ -125,7 +125,7 @@ impl View for StatusBar { let volume = format!( " [{}%]", - (self.spotify.volume() as f64 / 0xffff as f64 * 100.0) as u16 + (self.spotify.volume() as f64 / 65535_f64 * 100.0) as u16 ); printer.with_color(style_bar_bg, |printer| {