From 85e9e99d8736a9074418f0d02d4e6ecd97629858 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sat, 30 Nov 2019 17:13:41 +0100 Subject: [PATCH] rebind + change playlist update command to update entire library --- README.md | 2 +- src/command.rs | 15 +---- src/commands.rs | 15 ++--- src/library.rs | 134 +++++++++++++++++++++----------------------- src/ui/statusbar.rs | 11 +++- 5 files changed, 80 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index ac30a0f..dc10bff 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ have them configurable. * `Backspace` closes the current view * `Shift-p` toggles playback of a track * `Shift-s` stops a track -* `Shift-r` updates the playlist cache +* `Shift-u` updates the library cache (tracks, artists, albums, playlists) * `<` and `>` play the previous or next track * `,` and `.` to rewind or skip forward * `r` to toggle repeat mode diff --git a/src/command.rs b/src/command.rs index ca3f8a0..f5e4ce4 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,11 +2,6 @@ use queue::RepeatSetting; use std::collections::HashMap; use std::iter::FromIterator; -#[derive(Clone, Serialize, Deserialize, Debug)] -pub enum PlaylistCommands { - Update, -} - #[derive(Clone, Serialize, Deserialize, Debug)] pub enum SeekInterval { Forward, @@ -50,13 +45,13 @@ pub enum SeekDirection { pub enum Command { Quit, TogglePlay, - Playlists(PlaylistCommands), Stop, Previous, Next, Clear, Queue, Play, + UpdateLibrary, Save, SaveQueue, Delete, @@ -121,6 +116,7 @@ pub fn parse(input: &str) -> Option { "clear" => Some(Command::Clear), "queue" => Some(Command::Queue), "play" => Some(Command::Play), + "update" => Some(Command::UpdateLibrary), "delete" => Some(Command::Delete), "back" => Some(Command::Back), "open" => args @@ -210,13 +206,6 @@ pub fn parse(input: &str) -> Option { .map(|amount| Command::Seek(SeekDirection::Absolute(amount))), }), "focus" => args.get(0).map(|target| Command::Focus(target.to_string())), - "playlists" => args - .get(0) - .and_then(|action| match *action { - "update" => Some(PlaylistCommands::Update), - _ => None, - }) - .map(Command::Playlists), "save" => args.get(0).map(|target| match *target { "queue" => Command::SaveQueue, _ => Command::Save, diff --git a/src/commands.rs b/src/commands.rs index 750e443..9acc9c8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -2,9 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use command::{ - Command, GotoMode, MoveMode, PlaylistCommands, SeekDirection, ShiftMode, TargetMode, -}; +use command::{Command, GotoMode, MoveMode, SeekDirection, ShiftMode, TargetMode}; use cursive::event::{Event, Key}; use cursive::traits::View; use cursive::views::ViewRef; @@ -82,10 +80,8 @@ impl CommandManager { self.queue.clear(); Ok(None) } - Command::Playlists(mode) => { - match mode { - PlaylistCommands::Update => self.library.update_playlists(), - } + Command::UpdateLibrary => { + self.library.update_library(); Ok(None) } Command::TogglePlay => { @@ -193,10 +189,7 @@ impl CommandManager { kb.insert("q".into(), Command::Quit); kb.insert("Shift+p".into(), Command::TogglePlay); - kb.insert( - "Shift+r".into(), - Command::Playlists(PlaylistCommands::Update), - ); + kb.insert("Shift+u".into(), Command::UpdateLibrary); kb.insert("Shift+s".into(), Command::Stop); kb.insert("<".into(), Command::Previous); kb.insert(">".into(), Command::Next); diff --git a/src/library.rs b/src/library.rs index bb1e04e..7477ee3 100644 --- a/src/library.rs +++ b/src/library.rs @@ -28,7 +28,7 @@ pub struct Library { pub albums: Arc>>, pub artists: Arc>>, pub playlists: Arc>>, - is_done: Arc>, + pub is_done: Arc>, user_id: Option, ev: EventManager, spotify: Arc, @@ -51,71 +51,7 @@ impl Library { use_nerdfont, }; - { - let library = library.clone(); - thread::spawn(move || { - let t_tracks = { - let library = library.clone(); - thread::spawn(move || { - library - .load_cache(config::cache_path(CACHE_TRACKS), library.tracks.clone()); - library.fetch_tracks(); - library - .save_cache(config::cache_path(CACHE_TRACKS), library.tracks.clone()); - }) - }; - - let t_albums = { - let library = library.clone(); - thread::spawn(move || { - library - .load_cache(config::cache_path(CACHE_ALBUMS), library.albums.clone()); - library.fetch_albums(); - library - .save_cache(config::cache_path(CACHE_ALBUMS), library.albums.clone()); - }) - }; - - let t_artists = { - let library = library.clone(); - thread::spawn(move || { - library - .load_cache(config::cache_path(CACHE_ARTISTS), library.artists.clone()); - library.fetch_artists(); - }) - }; - - let t_playlists = { - let library = library.clone(); - thread::spawn(move || { - library.load_cache( - config::cache_path(CACHE_PLAYLISTS), - library.playlists.clone(), - ); - library.fetch_playlists(); - library.save_cache( - config::cache_path(CACHE_PLAYLISTS), - library.playlists.clone(), - ); - }) - }; - - t_tracks.join().unwrap(); - t_artists.join().unwrap(); - - library.populate_artists(); - library.save_cache(config::cache_path(CACHE_ARTISTS), library.artists.clone()); - - t_albums.join().unwrap(); - t_playlists.join().unwrap(); - - let mut is_done = library.is_done.write().unwrap(); - *is_done = true; - - library.ev.trigger(); - }); - } - + library.update_library(); library } @@ -208,7 +144,8 @@ impl Library { debug!("saving {} tracks to {}", tracks.len(), id); self.spotify.overwrite_playlist(id, &tracks); - self.update_playlists(); + self.fetch_playlists(); + self.save_cache(config::cache_path(CACHE_PLAYLISTS), self.playlists.clone()); } pub fn save_playlist(&self, name: &str, tracks: &[Track]) { @@ -219,9 +156,66 @@ impl Library { } } - pub fn update_playlists(&self) { - self.fetch_playlists(); - self.save_cache(config::cache_path(CACHE_PLAYLISTS), self.playlists.clone()); + pub fn update_library(&self) { + *self.is_done.write().unwrap() = false; + + let library = self.clone(); + thread::spawn(move || { + let t_tracks = { + let library = library.clone(); + thread::spawn(move || { + library.load_cache(config::cache_path(CACHE_TRACKS), library.tracks.clone()); + library.fetch_tracks(); + library.save_cache(config::cache_path(CACHE_TRACKS), library.tracks.clone()); + }) + }; + + let t_albums = { + let library = library.clone(); + thread::spawn(move || { + library.load_cache(config::cache_path(CACHE_ALBUMS), library.albums.clone()); + library.fetch_albums(); + library.save_cache(config::cache_path(CACHE_ALBUMS), library.albums.clone()); + }) + }; + + let t_artists = { + let library = library.clone(); + thread::spawn(move || { + library.load_cache(config::cache_path(CACHE_ARTISTS), library.artists.clone()); + library.fetch_artists(); + }) + }; + + let t_playlists = { + let library = library.clone(); + thread::spawn(move || { + library.load_cache( + config::cache_path(CACHE_PLAYLISTS), + library.playlists.clone(), + ); + library.fetch_playlists(); + library.save_cache( + config::cache_path(CACHE_PLAYLISTS), + library.playlists.clone(), + ); + }) + }; + + t_tracks.join().unwrap(); + t_artists.join().unwrap(); + + library.populate_artists(); + library.save_cache(config::cache_path(CACHE_ARTISTS), library.artists.clone()); + + t_albums.join().unwrap(); + t_playlists.join().unwrap(); + + let mut is_done = library.is_done.write().unwrap(); + *is_done = true; + + library.ev.trigger(); + }); } fn fetch_playlists(&self) { diff --git a/src/ui/statusbar.rs b/src/ui/statusbar.rs index e6c49b1..1330c2d 100644 --- a/src/ui/statusbar.rs +++ b/src/ui/statusbar.rs @@ -89,6 +89,12 @@ impl View for StatusBar { printer.print((1, 1), &state_icon); }); + let updating = if !*self.library.is_done.read().unwrap() { + if self.use_nerdfont { "\u{f9e5} " } else { "[U] " } + } else { + "" + }; + let repeat = if self.use_nerdfont { match self.queue.get_repeat() { RepeatSetting::None => "", @@ -137,7 +143,8 @@ impl View for StatusBar { "" }; - let right = repeat.to_string() + let right = updating.to_string() + + repeat + shuffle + saved + &format!("{} / {} ", formatted_elapsed, t.duration_str()); @@ -153,7 +160,7 @@ impl View for StatusBar { printer.print((0, 0), &"━".repeat(duration_width + 1)); }); } else { - let right = repeat.to_string() + shuffle; + let right = updating.to_string() + repeat + shuffle; let offset = HAlign::Right.get_offset(right.width(), printer.size.x); printer.with_color(style, |printer| {