diff --git a/src/player/tui.rs b/src/player/tui.rs index cd04709..0f43e11 100644 --- a/src/player/tui.rs +++ b/src/player/tui.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use image::DynamicImage; use layout::Flex; use ratatui::{ @@ -7,7 +9,7 @@ use ratatui::{ }; use ratatui_image::{picker::Picker, protocol::StatefulProtocol, StatefulImage}; -use crate::utils::Error; +use crate::utils::{format_duration, Error}; use super::{current::Metadata, Player}; @@ -58,10 +60,16 @@ impl Root { StatefulImage::new(None).render(area, buf, &mut self.image_state); } fn render_track_info(&self, area: Rect, buf: &mut Buffer) { - let layout_track = Layout::vertical([Constraint::Max(2), Constraint::Max(1)]); - let [title, artist] = layout_track.areas(area); + let layout_track = Layout::vertical([ + Constraint::Max(2), + Constraint::Max(1), + Constraint::Fill(1), + Constraint::Max(1), + ]); + let [title, artist, _, time] = layout_track.areas(area); self.render_title(title, buf); self.render_artist(artist, buf); + self.render_time(time, buf); } fn render_title(&self, area: Rect, buf: &mut Buffer) { Paragraph::new(self.metadata.name.clone()) @@ -74,6 +82,14 @@ impl Root { Paragraph::new(self.metadata.artist.clone().unwrap()).render(area, buf); } } + fn render_time(&self, area: Rect, buf: &mut Buffer) { + Paragraph::new(format!( + "{} / {}", + format_duration(self.metadata.current_time), + format_duration(self.metadata.duration), + )) + .render(area, buf); + } pub fn update_cover(&mut self, cover: DynamicImage) { self.metadata.set_cover(cover); self.image_state = self diff --git a/src/utils.rs b/src/utils.rs index 54ac3bf..5f2e176 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -22,3 +22,14 @@ pub fn default_cover() -> DynamicImage { .unwrap() .thumbnail(200, 200) } + +pub fn format_duration(secs: u32) -> String { + let rem = secs; + let (hours, rem) = (rem / 3600, rem % 3600); + let (minutes, rem) = (rem / 60, rem % 60); + if hours > 0 { + return format!("{}:{:0>2}:{:0>2}", hours, minutes, rem); + } else { + return format!("{:0>2}:{:0>2}", minutes, rem); + } +}