Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ratatui::{
};
use std::borrow::Cow;
use std::sync::Arc;
use std::time::Duration;
use tokio::task::JoinHandle;

use crate::artwork::converter::ArtworkConverter;
Expand Down Expand Up @@ -449,17 +448,18 @@ impl App {
} else {
0
};
cache.duration_str = format!(
"{} / {}",
format_duration(track.position),
format_duration(track.duration)
);
cache.gauge_label = format!(
" {}/{} | {:02}% ",
format_duration_seconds(track.position),
format_duration_seconds(track.duration),
progress_percent
);
// Bolt ⚡ Optimization:
// Inline integer calculations for duration and directly use them
// in a single format macro to avoid multiple intermediate String allocations.
let pos_secs = track.position.as_secs();
let pos_m = pos_secs / 60;
let pos_s = pos_secs % 60;
let dur_secs = track.duration.as_secs();
let dur_m = dur_secs / 60;
let dur_s = dur_secs % 60;

cache.duration_str = format!("{:02}:{:02} / {:02}:{:02}", pos_m, pos_s, dur_m, dur_s);
cache.gauge_label = format!(" {}s/{}s | {:02}% ", pos_secs, dur_secs, progress_percent);
cache.progress_percent = progress_percent;
self.metadata_cache = Some(cache);
} else {
Expand Down Expand Up @@ -1029,18 +1029,6 @@ pub fn draw(f: &mut Frame, app: &mut App) {
}
}

fn format_duration_seconds(duration: Duration) -> String {
let total_seconds = duration.as_secs();
format!("{}s", total_seconds)
}

fn format_duration(duration: Duration) -> String {
let total_seconds = duration.as_secs();
let minutes = total_seconds / 60;
let seconds = total_seconds % 60;
format!("{:02}:{:02}", minutes, seconds)
}

// Optimized: Uses iterator chaining/cycling to avoid intermediate Vec<char> and format! allocations.
// Benchmark: ~32% speedup (329ms vs 484ms for 100k iters).
fn scroll_text<'a>(text: &'a str, width: usize, frame: u32) -> Cow<'a, str> {
Expand Down Expand Up @@ -1070,6 +1058,7 @@ mod tests {
use async_trait::async_trait;
use ratatui::backend::TestBackend;
use ratatui::Terminal;
use std::time::Duration;

struct MockPlayer {
volume: u8,
Expand Down
Loading