From e9857e5c112f0d974e4f51f7b015007ac4a303c0 Mon Sep 17 00:00:00 2001 From: talwat <83217276+talwat@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:33:59 +0200 Subject: [PATCH] feat: adjust ui --- Cargo.lock | 1 + Cargo.toml | 1 + src/player/ui.rs | 43 ++++++++++++++++++++++++++++--------------- src/tracks.rs | 2 ++ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67a1d81..d228c91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -981,6 +981,7 @@ dependencies = [ "crossterm", "eyre", "futures", + "itertools", "rand", "reqwest", "rodio", diff --git a/Cargo.toml b/Cargo.toml index 1ac437c..c26e3ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,4 @@ bytes = "1.7.2" scraper = "0.20.0" rodio = { version = "0.19.0", features = ["mp3"], default-features = false } crossterm = "0.28.1" +itertools = "0.13.0" diff --git a/src/player/ui.rs b/src/player/ui.rs index 9af5b62..db4bf8f 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -2,9 +2,9 @@ use std::{io::stderr, sync::Arc, time::Duration}; use super::Player; use crossterm::{ - cursor::{MoveTo, MoveToColumn, MoveUp}, + cursor::{MoveToColumn, MoveUp, Show}, style::Print, - terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, + terminal::{Clear, ClearType}, }; use tokio::{ sync::mpsc::Sender, @@ -16,35 +16,52 @@ use super::Messages; async fn interface(queue: Arc) -> eyre::Result<()> { const WIDTH: usize = 25; + const PROGRESS_WIDTH: usize = WIDTH - 4; loop { // We can get away with only redrawing every 0.25 seconds // since it's just an audio player. - sleep(Duration::from_secs_f32(1.0 / 60.0)).await; + sleep(Duration::from_secs_f32(0.25)).await; crossterm::execute!(stderr(), Clear(ClearType::FromCursorDown))?; - let main = match queue.current.load().as_ref() { + let mut main = match queue.current.load().as_ref() { Some(x) => { if queue.sink.is_paused() { - format!("paused {}\r\n", x.format_name()) + format!("paused {}", x.format_name()) } else { - format!("playing {}\r\n", x.format_name()) + format!("playing {}", x.format_name()) } } - None => "loading...\r\n".to_owned(), + None => "loading...".to_owned(), }; + main.push_str("\r\n"); + + let mut filled = 0; + if let Some(current) = queue.current.load().as_ref() { + if let Some(duration) = current.duration { + let elapsed = queue.sink.get_pos().as_secs() as f32 / duration.as_secs() as f32; + filled = (elapsed * PROGRESS_WIDTH as f32).round() as usize; + } + }; + + let progress = format!( + " [{}{}] ", + "/".repeat(filled as usize), + " ".repeat(PROGRESS_WIDTH - filled) + ); let bar = ["[s]kip", "[p]ause", "[q]uit"]; crossterm::execute!(stderr(), MoveToColumn(0), Print(main))?; - crossterm::execute!(stderr(), Print(bar.join(" ")))?; - crossterm::execute!(stderr(), MoveToColumn(0), MoveUp(1))?; + crossterm::execute!(stderr(), Print(progress), Print("\r\n"))?; + crossterm::execute!(stderr(), Print(bar.join(" ")), Print("\r\n"))?; + crossterm::execute!(stderr(), MoveToColumn(0), MoveUp(3))?; } } pub async fn start(queue: Arc, sender: Sender) -> eyre::Result<()> { crossterm::terminal::enable_raw_mode()?; - crossterm::execute!(stderr(), EnterAlternateScreen, MoveTo(0, 0))?; + //crossterm::execute!(stderr(), EnterAlternateScreen, MoveTo(0, 0))?; task::spawn(interface(queue.clone())); @@ -73,11 +90,7 @@ pub async fn start(queue: Arc, sender: Sender) -> eyre::Result } } - crossterm::execute!( - stderr(), - Clear(ClearType::FromCursorDown), - LeaveAlternateScreen - )?; + crossterm::execute!(stderr(), Clear(ClearType::FromCursorDown), Show)?; crossterm::terminal::disable_raw_mode()?; Ok(()) diff --git a/src/tracks.rs b/src/tracks.rs index 769ff8c..eace3eb 100644 --- a/src/tracks.rs +++ b/src/tracks.rs @@ -42,6 +42,8 @@ impl TrackInfo { /// as well as some basic information about it. pub struct Track { pub info: TrackInfo, + + /// TODO: Make decoding lazy, since decoded files take up more memory than raw ones. pub data: Data, }