Skip to content

Commit

Permalink
Keep playing available tracks if no specific track selected
Browse files Browse the repository at this point in the history
  • Loading branch information
guptarohit committed Jul 8, 2023
1 parent 24c0498 commit 622d75c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mfp"
description = "A command-line utility for playing music mixes for programming & focus (from musicforprogramming.net), unlocking the flow state!"
version = "0.1.0"
version = "0.2.0"
authors = ["Rohit Gupta"]
edition = "2021"
readme = "README.md"
Expand Down
24 changes: 18 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ use rodio::Source;
use utils::parse_duration;
use utils::play_audio_from_url;

fn play_random_episode(rss_feed: &Mfp, volume: u8) {
fn play_random_episodes(rss_feed: &mut Mfp, volume: u8) {
let mut rng = thread_rng();

if let Some(random_episode) = rss_feed.items.choose(&mut rng) {
play_episode(random_episode, volume);
} else {
if rss_feed.items.is_empty() {
eprintln!("No Tracks found");
return;
}

loop {
if let Some(random_episode) = rss_feed.items.choose_mut(&mut rng) {
play_episode(random_episode, volume);
let played_title = random_episode.title.clone();
rss_feed
.items
.retain(|episode| episode.title != played_title);
} else {
println!("All tracks have been played 🎶");
return;
}
}
}

Expand All @@ -43,7 +55,7 @@ fn main() {
return eprintln!("Volume must be between 0 and 9");
}

let rss_feed = Mfp::new().expect("Failed to fetch RSS data");
let mut rss_feed = Mfp::new().expect("Failed to fetch RSS data");
let total_tracks = rss_feed.items.len();

if let Some(requested_track_number) = args.track_number {
Expand All @@ -56,6 +68,6 @@ fn main() {
play_episode(episode, args.volume);
}
} else {
play_random_episode(&rss_feed, args.volume);
play_random_episodes(&mut rss_feed, args.volume);
}
}

0 comments on commit 622d75c

Please sign in to comment.