Add playback display

This commit is contained in:
Muaz Ahmad 2024-11-29 15:19:06 +05:00
parent 3122e2559e
commit 6656727021
2 changed files with 30 additions and 3 deletions

View file

@ -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

View file

@ -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);
}
}