From e3d32127f2764ff08a4f71969e1b0d137b21b039 Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Sat, 15 Jul 2023 15:05:10 -0400 Subject: [PATCH 1/2] add `tracks_playback_limit` config option which determines the number of tracks in a `uris` playback --- spotify_player/src/config/mod.rs | 4 ++++ spotify_player/src/event/window.rs | 6 ++++-- spotify_player/src/state/consant.rs | 2 -- spotify_player/src/state/model.rs | 8 ++++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/spotify_player/src/config/mod.rs b/spotify_player/src/config/mod.rs index 2a1098e1..9cf177b1 100644 --- a/spotify_player/src/config/mod.rs +++ b/spotify_player/src/config/mod.rs @@ -31,6 +31,8 @@ pub struct AppConfig { #[cfg(feature = "notify")] pub notify_format: NotifyFormat, + pub tracks_playback_limit: usize, + // session configs pub proxy: Option, pub ap_port: Option, @@ -134,6 +136,8 @@ impl Default for AppConfig { client_port: 8080, + tracks_playback_limit: 50, + playback_format: String::from("{track} • {artists}\n{album}\n{metadata}"), #[cfg(feature = "notify")] notify_format: NotifyFormat { diff --git a/spotify_player/src/event/window.rs b/spotify_player/src/event/window.rs index e2269c78..3350de1d 100644 --- a/spotify_player/src/event/window.rs +++ b/spotify_player/src/event/window.rs @@ -162,12 +162,14 @@ pub fn handle_command_for_track_table_window( let id = rand::thread_rng().gen_range(0..tracks.len()); client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback( - base_playback.uri_offset(tracks[id].id.uri()), + base_playback + .uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit), )))?; } Command::ChooseSelected => { client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback( - base_playback.uri_offset(tracks[id].id.uri()), + base_playback + .uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit), )))?; } Command::ShowActionsOnSelectedItem => { diff --git a/spotify_player/src/state/consant.rs b/spotify_player/src/state/consant.rs index b5869add..f5ae46af 100644 --- a/spotify_player/src/state/consant.rs +++ b/spotify_player/src/state/consant.rs @@ -1,8 +1,6 @@ pub use super::*; use once_cell::sync::Lazy; -pub const PLAYBACK_TRACKS_LIMIT: usize = 200; - pub static USER_TOP_TRACKS_ID: Lazy = Lazy::new(|| TracksId::new("tracks:user-top-tracks", "Top Tracks")); diff --git a/spotify_player/src/state/model.rs b/spotify_player/src/state/model.rs index b778bb86..6adad09b 100644 --- a/spotify_player/src/state/model.rs +++ b/spotify_player/src/state/model.rs @@ -411,21 +411,21 @@ impl TracksId { impl Playback { /// creates new playback with a specified offset based on the current playback - pub fn uri_offset(&self, uri: String) -> Self { + pub fn uri_offset(&self, uri: String, limit: usize) -> Self { match self { Playback::Context(id, _) => { Playback::Context(id.clone(), Some(rspotify_model::Offset::Uri(uri))) } Playback::URIs(ids, _) => { - let ids = if ids.len() < super::PLAYBACK_TRACKS_LIMIT { + let ids = if ids.len() < limit { ids.clone() } else { let pos = ids .iter() .position(|id| id.uri() == uri) .unwrap_or_default(); - let l = pos.saturating_sub(super::PLAYBACK_TRACKS_LIMIT / 2); - let r = std::cmp::min(l + super::PLAYBACK_TRACKS_LIMIT, ids.len()); + let l = pos.saturating_sub(limit / 2); + let r = std::cmp::min(l + limit, ids.len()); // For a list with too many tracks, to avoid payload limit when making the `start_playback` // API request, we restrict the range of tracks to be played, which is based on the // playing track's position (if any) and the application's limit (PLAYBACK_TRACKS_LIMIT). From d7b9b3efc2148e9eab23b4686e3b987742525497 Mon Sep 17 00:00:00 2001 From: Thang Pham Date: Sat, 15 Jul 2023 15:14:28 -0400 Subject: [PATCH 2/2] update config document/example --- docs/config.md | 1 + examples/app.toml | 1 + spotify_player/src/state/model.rs | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index f4bd3ba0..99c5267f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -23,6 +23,7 @@ All configuration files should be placed inside the application's configuration | ------------------------------------ | ----------------------------------------------------------------------------- | ---------------------------------------------------------- | | `client_id` | the Spotify client's ID | `65b708073fc0480ea92a077233ca87bd` | | `client_port` | the port that the application's client is running on to handle CLI commands | `8080` | +| `tracks_playback_limit` | the limit for the number of tracks played in a **tracks** playback | `50` | | `playback_format` | the format of the text in the playback's window | `{track} • {artists}\n{album}\n{metadata}` | | `notify_format` | the format of a notification (`notify` feature only) | `{ summary = "{track} • {artists}", body = "{album}" }` | | `copy_command` | the command used to execute a copy-to-clipboard action | `xclip -sel c` (Linux), `pbcopy` (MacOS), `clip` (Windows) | diff --git a/examples/app.toml b/examples/app.toml index 31248d60..143ab1d8 100644 --- a/examples/app.toml +++ b/examples/app.toml @@ -1,6 +1,7 @@ theme = "default" client_id = "65b708073fc0480ea92a077233ca87bd" client_port = 8080 +tracks_playback_limit = 50 playback_format = "{track} • {artists}\n{album}\n{metadata}" notify_format = { summary = "{track} • {artists}", body = "{album}" } # the default `copy_command` is based on the OS diff --git a/spotify_player/src/state/model.rs b/spotify_player/src/state/model.rs index 6adad09b..11ec59ea 100644 --- a/spotify_player/src/state/model.rs +++ b/spotify_player/src/state/model.rs @@ -428,7 +428,7 @@ impl Playback { let r = std::cmp::min(l + limit, ids.len()); // For a list with too many tracks, to avoid payload limit when making the `start_playback` // API request, we restrict the range of tracks to be played, which is based on the - // playing track's position (if any) and the application's limit (PLAYBACK_TRACKS_LIMIT). + // playing track's position (if any) and the application's limit (`app_config.tracks_playback_limit`). // Related issue: https://github.com/aome510/spotify-player/issues/78 ids[l..r].to_vec() };