Files
akula/src/widgets/progress.rs
2025-03-31 23:58:06 +02:00

84 lines
2.4 KiB
Rust

use ratatui::{
layout::{Constraint, Direction, Layout},
style::{Style, Stylize},
text::Text,
widgets::{Block, BorderType, Borders, Gauge, Widget},
};
use crate::player::Player;
pub struct Progress<'a> {
player: &'a Player,
}
impl<'a> Progress<'a> {
pub fn new(player: &'a Player) -> Self {
Self { player }
}
}
impl Widget for &Progress<'_> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let ratio = self.player.ratio_played();
let pos = self.player.current_pos_duration().as_secs();
if let Some(song) = self
.player
.songs_list
.lock()
.unwrap()
.get(self.player.current_playing)
{
Block::default()
.title(format!(
"{} - {}",
song.title.clone(),
song.artist.clone().unwrap_or_default()
))
.borders(Borders::all())
.border_type(BorderType::Rounded)
.render(area, buf);
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![
Constraint::Min(0),
Constraint::Length(1),
Constraint::Min(0),
])
.split(area);
let inner_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints(vec![
Constraint::Max(25),
Constraint::Min(0),
Constraint::Max(25),
])
.split(layout[1]);
Text::from(format!(
"{:0>2}:{:0>2}:{:0>2} ",
pos / (60 * 60),
pos / 60,
pos % 60
))
.right_aligned()
.render(inner_layout[0], buf);
let pos = song.duration.map(|d| d.to_duration().as_secs() ).unwrap_or_default();
Text::from(format!(
" {:0>2}:{:0>2}:{:0>2}",
pos / (60 * 60),
pos / 60,
pos % 60
))
.left_aligned()
.render(inner_layout[2], buf);
Gauge::default()
.label("")
.gauge_style(Style::new().cyan())
.ratio(ratio)
.render(inner_layout[1], buf);
}
}
}