diff --git a/src/player.rs b/src/player.rs index cfef3fb..ea2189b 100644 --- a/src/player.rs +++ b/src/player.rs @@ -221,7 +221,7 @@ impl Player { // Notify the background downloader that there's an empty spot // in the buffer. - itx.send(()).await?; + Downloader::notify(&itx).await?; // Notify the audio server that the next song has actually been downloaded. tx.send(Messages::NewSong).await? @@ -249,11 +249,11 @@ impl Player { mut rx: Receiver, ) -> eyre::Result<()> { // `itx` is used to notify the `Downloader` when it needs to download new tracks. - let (downloader, itx) = Downloader::new(player.clone()); - let downloader = downloader.start().await; + let downloader = Downloader::new(player.clone()); + let (itx, downloader) = downloader.start().await; // Start buffering tracks immediately. - itx.send(()).await?; + Downloader::notify(&itx).await?; // Set the initial sink volume to the one specified. player.sink.set_volume(properties.volume as f32 / 100.0); diff --git a/src/player/downloader.rs b/src/player/downloader.rs index f93ff8a..33ba97c 100644 --- a/src/player/downloader.rs +++ b/src/player/downloader.rs @@ -21,32 +21,45 @@ pub struct Downloader { /// The internal reciever, which is used by the downloader to know /// when to begin downloading more tracks. rx: Receiver<()>, + + /// A copy of the internal sender, which can be useful for keeping + /// track of it. + tx: Sender<()>, } impl Downloader { + /// Uses a sender recieved from [Sender] to notify the + /// download thread that it should resume downloading. + pub async fn notify(sender: &Sender<()>) -> Result<(), mpsc::error::SendError<()>> { + sender.send(()).await + } + /// Initializes the [Downloader]. /// /// This also sends a [`Sender`] which can be used to notify /// when the downloader needs to begin downloading more tracks. - pub fn new(player: Arc) -> (Self, Sender<()>) { + pub fn new(player: Arc) -> Self { let (tx, rx) = mpsc::channel(8); - (Self { player, rx }, tx) + Self { player, rx, tx } } /// Actually starts & consumes the [Downloader]. - pub async fn start(mut self) -> JoinHandle<()> { - task::spawn(async move { - // Loop through each update notification. - while self.rx.recv().await == Some(()) { - // For each update notification, we'll push tracks until the buffer is completely full. - while self.player.tracks.read().await.len() < BUFFER_SIZE { - let Ok(track) = Track::random(&self.player.client).await else { - continue; - }; + pub async fn start(mut self) -> (Sender<()>, JoinHandle<()>) { + ( + self.tx, + task::spawn(async move { + // Loop through each update notification. + while self.rx.recv().await == Some(()) { + // For each update notification, we'll push tracks until the buffer is completely full. + while self.player.tracks.read().await.len() < BUFFER_SIZE { + let Ok(track) = Track::random(&self.player.client).await else { + continue; + }; - self.player.tracks.write().await.push_back(track); + self.player.tracks.write().await.push_back(track); + } } - } - }) + }), + ) } } diff --git a/src/player/ui.rs b/src/player/ui.rs index 29f0d50..d06deac 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -114,7 +114,15 @@ async fn input(sender: Sender) -> eyre::Result<()> { /// has been displayed for, so that it's only displayed for a certain amount of frames. async fn interface(player: Arc, minimalist: bool) -> eyre::Result<()> { loop { - let action = components::action(&player, WIDTH); + let action = format!( + "{} {}", + components::action(&player, WIDTH - 2), + player + .tracks + .try_read() + .and_then(|x| Ok(x.len().to_string())) + .unwrap_or(String::from("?")) + ); let timer = VOLUME_TIMER.load(Ordering::Relaxed); let volume = player.sink.volume();