From ca62bbb3e2455d4d832b4b359e7247deebf7f5c1 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Thu, 24 Jul 2025 16:12:07 +0200 Subject: fix(crates/yt/{commands/playlist,videos/format_video}): Correctly calculate watch percent Previously, they were using u64, which obviously only returned `0%`. --- crates/yt/src/commands/playlist/implm.rs | 9 ++------- crates/yt/src/videos/format_video.rs | 14 ++++---------- crates/yt/src/videos/mod.rs | 10 +++++++++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/crates/yt/src/commands/playlist/implm.rs b/crates/yt/src/commands/playlist/implm.rs index 98a8e64..ce4b394 100644 --- a/crates/yt/src/commands/playlist/implm.rs +++ b/crates/yt/src/commands/playlist/implm.rs @@ -49,13 +49,8 @@ impl PlaylistCommand { if is_focused { output.push_str(" ("); - if let Some(duration) = video.duration.as_secs() { - let watch_progress: f64 = f64::from( - u32::try_from(video.watch_progress.as_secs()).expect("No overflow"), - ); - let duration = f64::from(u32::try_from(duration).expect("No overflow")); - - write!(output, "{:0.0}%", (watch_progress / duration) * 100.0)?; + if let Some(percent) = video.watch_progress_percent_fmt() { + write!(output, "{}", percent.to_string(app))?; } else { write!(output, "{}", video.watch_progress_fmt().to_string(app))?; } diff --git a/crates/yt/src/videos/format_video.rs b/crates/yt/src/videos/format_video.rs index 9e86205..b457559 100644 --- a/crates/yt/src/videos/format_video.rs +++ b/crates/yt/src/videos/format_video.rs @@ -33,21 +33,15 @@ impl Video { let thumbnail_url = self.thumbnail_url_fmt().to_string(app); let title = self.title_fmt().to_string(app); let url = self.url_fmt().to_string(app); - let watch_progress = self.watch_progress_fmt().to_string(app); let video_options = self.video_options_fmt(app).to_string(app); let watched_percentage_fmt = { - if let Some(duration) = self.duration.as_secs() { - format!( - " (watched: {:0.0}%)", - (self.watch_progress.as_secs() / duration) * 100 - ) + if let Some(percent) = self.watch_progress_percent_fmt() { + format!(" (watched: {})", percent.to_string(app)) } else { - format!(" {}", watch_progress) + format!(" {}", self.watch_progress_fmt().to_string(app)) } - .into_canvas() - } - .to_string(app); + }; let options = video_options.to_string(); let options = options.trim(); diff --git a/crates/yt/src/videos/mod.rs b/crates/yt/src/videos/mod.rs index 3775e0f..f8eda88 100644 --- a/crates/yt/src/videos/mod.rs +++ b/crates/yt/src/videos/mod.rs @@ -12,7 +12,7 @@ use std::fmt::Write; use anyhow::{Context, Result}; -use colors::{Colorize, IntoCanvas}; +use colors::{Canvas, Colorize, IntoCanvas}; use url::Url; use crate::{ @@ -76,6 +76,14 @@ impl Video { pub(crate) fn watch_progress_fmt(&self) -> impl Colorize { MaybeDuration::from_std(self.watch_progress).cyan().bold() } + #[must_use] + pub(crate) fn watch_progress_percent_fmt(&self) -> Option { + self.duration.as_secs_f64().map(|duration| { + let watch_progress = self.watch_progress.as_secs_f64(); + + (format!("{:0.0}%", (watch_progress / duration) * 100.0)).into_canvas() + }) + } pub(crate) async fn extractor_hash_fmt(&self, app: &App) -> Result { let hash = self -- cgit 1.4.1