From f7b000dffd3ed72b9bcab56fffb4525a5902b351 Mon Sep 17 00:00:00 2001 From: juntaochi <53086415+juntaochi@users.noreply.github.com> Date: Tue, 21 Jul 2026 16:10:17 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Replace=20intermediate=20du?= =?UTF-8?q?ration=20strings=20with=20Display=20structs=20to=20reduce=20all?= =?UTF-8?q?ocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced string-allocating helper functions format_duration and format_duration_seconds with tuple structs implementing std::fmt::Display to output durations directly to the formatter buffer. --- src/ui/mod.rs | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e4a57ad..f150263 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -550,13 +550,13 @@ impl App { }; cache.duration_str = format!( "{} / {}", - format_duration(track.position), - format_duration(track.duration) + FmtDuration(track.position), + FmtDuration(track.duration) ); cache.gauge_label = format!( " {}/{} | {:02}% ", - format_duration_seconds(track.position), - format_duration_seconds(track.duration), + FmtDurationSeconds(track.position), + FmtDurationSeconds(track.duration), progress_percent ); cache.progress_percent = progress_percent; @@ -829,8 +829,8 @@ fn draw_progress(f: &mut Frame, area: Rect, track: &Track, theme: Theme) { let label = format!( " {}/{} | {:02}% ", - format_duration_seconds(track.position), - format_duration_seconds(track.duration), + FmtDurationSeconds(track.position), + FmtDurationSeconds(track.duration), progress_percent ); @@ -967,8 +967,8 @@ fn draw_metadata( track.album.to_uppercase(), format!( "{} / {}", - format_duration(track.position), - format_duration(track.duration) + FmtDuration(track.position), + FmtDuration(track.duration) ), ]; @@ -1241,18 +1241,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) -} - // Marquee scroll measured by display width (columns), so full-width CJK glyphs // trigger scrolling and fill the window correctly instead of overflowing. fn scroll_text<'a>(text: &'a str, width: usize, frame: u32) -> Cow<'a, str> { @@ -1572,3 +1560,20 @@ mod tests { } } } + +struct FmtDurationSeconds(Duration); +impl std::fmt::Display for FmtDurationSeconds { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}s", self.0.as_secs()) + } +} + +struct FmtDuration(Duration); +impl std::fmt::Display for FmtDuration { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let total_seconds = self.0.as_secs(); + let minutes = total_seconds / 60; + let seconds = total_seconds % 60; + write!(f, "{:02}:{:02}", minutes, seconds) + } +}