Skip to content

Commit 5491fda

Browse files
authored
Add tracks_playback_limit config option (#219)
Resolves #215
1 parent 975eafe commit 5491fda

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

docs/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ All configuration files should be placed inside the application's configuration
2323
| ------------------------------------ | ----------------------------------------------------------------------------- | ---------------------------------------------------------- |
2424
| `client_id` | the Spotify client's ID | `65b708073fc0480ea92a077233ca87bd` |
2525
| `client_port` | the port that the application's client is running on to handle CLI commands | `8080` |
26+
| `tracks_playback_limit` | the limit for the number of tracks played in a **tracks** playback | `50` |
2627
| `playback_format` | the format of the text in the playback's window | `{track} • {artists}\n{album}\n{metadata}` |
2728
| `notify_format` | the format of a notification (`notify` feature only) | `{ summary = "{track} • {artists}", body = "{album}" }` |
2829
| `copy_command` | the command used to execute a copy-to-clipboard action | `xclip -sel c` (Linux), `pbcopy` (MacOS), `clip` (Windows) |

examples/app.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
theme = "default"
22
client_id = "65b708073fc0480ea92a077233ca87bd"
33
client_port = 8080
4+
tracks_playback_limit = 50
45
playback_format = "{track} • {artists}\n{album}\n{metadata}"
56
notify_format = { summary = "{track} • {artists}", body = "{album}" }
67
# the default `copy_command` is based on the OS

spotify_player/src/config/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct AppConfig {
3131
#[cfg(feature = "notify")]
3232
pub notify_format: NotifyFormat,
3333

34+
pub tracks_playback_limit: usize,
35+
3436
// session configs
3537
pub proxy: Option<String>,
3638
pub ap_port: Option<u16>,
@@ -134,6 +136,8 @@ impl Default for AppConfig {
134136

135137
client_port: 8080,
136138

139+
tracks_playback_limit: 50,
140+
137141
playback_format: String::from("{track} • {artists}\n{album}\n{metadata}"),
138142
#[cfg(feature = "notify")]
139143
notify_format: NotifyFormat {

spotify_player/src/event/window.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,14 @@ pub fn handle_command_for_track_table_window(
162162
let id = rand::thread_rng().gen_range(0..tracks.len());
163163

164164
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
165-
base_playback.uri_offset(tracks[id].id.uri()),
165+
base_playback
166+
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
166167
)))?;
167168
}
168169
Command::ChooseSelected => {
169170
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
170-
base_playback.uri_offset(tracks[id].id.uri()),
171+
base_playback
172+
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
171173
)))?;
172174
}
173175
Command::ShowActionsOnSelectedItem => {

spotify_player/src/state/consant.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
pub use super::*;
22
use once_cell::sync::Lazy;
33

4-
pub const PLAYBACK_TRACKS_LIMIT: usize = 200;
5-
64
pub static USER_TOP_TRACKS_ID: Lazy<TracksId> =
75
Lazy::new(|| TracksId::new("tracks:user-top-tracks", "Top Tracks"));
86

spotify_player/src/state/model.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,24 +411,24 @@ impl TracksId {
411411

412412
impl Playback {
413413
/// creates new playback with a specified offset based on the current playback
414-
pub fn uri_offset(&self, uri: String) -> Self {
414+
pub fn uri_offset(&self, uri: String, limit: usize) -> Self {
415415
match self {
416416
Playback::Context(id, _) => {
417417
Playback::Context(id.clone(), Some(rspotify_model::Offset::Uri(uri)))
418418
}
419419
Playback::URIs(ids, _) => {
420-
let ids = if ids.len() < super::PLAYBACK_TRACKS_LIMIT {
420+
let ids = if ids.len() < limit {
421421
ids.clone()
422422
} else {
423423
let pos = ids
424424
.iter()
425425
.position(|id| id.uri() == uri)
426426
.unwrap_or_default();
427-
let l = pos.saturating_sub(super::PLAYBACK_TRACKS_LIMIT / 2);
428-
let r = std::cmp::min(l + super::PLAYBACK_TRACKS_LIMIT, ids.len());
427+
let l = pos.saturating_sub(limit / 2);
428+
let r = std::cmp::min(l + limit, ids.len());
429429
// For a list with too many tracks, to avoid payload limit when making the `start_playback`
430430
// API request, we restrict the range of tracks to be played, which is based on the
431-
// playing track's position (if any) and the application's limit (PLAYBACK_TRACKS_LIMIT).
431+
// playing track's position (if any) and the application's limit (`app_config.tracks_playback_limit`).
432432
// Related issue: https://github.com/aome510/spotify-player/issues/78
433433
ids[l..r].to_vec()
434434
};

0 commit comments

Comments
 (0)