From b1801ee101fdcfc070ad09a48d3e318281b2e766 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Sep 2024 13:13:47 +0200 Subject: [PATCH] time_format: use libfmt --- src/FileListPage.cxx | 6 ++--- src/QueuePage.cxx | 6 ++--- src/SongPage.cxx | 14 +++++----- src/SongRowPaint.cxx | 6 ++--- src/StatusBar.cxx | 62 +++++++++++++++++++++++--------------------- src/StatusBar.hxx | 3 ++- src/strfsong.cxx | 3 +-- src/time_format.cxx | 15 ++++++----- src/time_format.hxx | 4 ++- 9 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/FileListPage.cxx b/src/FileListPage.cxx index c2318536..40235aea 100644 --- a/src/FileListPage.cxx +++ b/src/FileListPage.cxx @@ -568,9 +568,9 @@ FileListPage::PaintStatusBarOverride(const Window window) const noexcept duration += mpd_song_get_duration(mpd_entity_get_song(entity)); } - char duration_string[32]; - format_duration_short(duration_string, duration); - const unsigned duration_width = strlen(duration_string); + char duration_buffer[32]; + const auto duration_string = format_duration_short(duration_buffer, duration); + const unsigned duration_width = duration_string.size(); SelectStyle(window, Style::STATUS_TIME); window.String({0, (int)window.GetWidth() - (int)duration_width}, duration_string); diff --git a/src/QueuePage.cxx b/src/QueuePage.cxx index c860e7a2..2dbfeada 100644 --- a/src/QueuePage.cxx +++ b/src/QueuePage.cxx @@ -434,9 +434,9 @@ QueuePage::PaintStatusBarOverride(const Window window) const noexcept duration += mpd_song_get_duration(&song); } - char duration_string[32]; - format_duration_short(duration_string, duration); - const unsigned duration_width = strlen(duration_string); + char duration_buffer[32]; + const auto duration_string = format_duration_short(duration_buffer, duration); + const unsigned duration_width = duration_string.size(); SelectStyle(window, Style::STATUS_TIME); window.String({(int)window.GetWidth() - (int)duration_width, 0}, duration_string); diff --git a/src/SongPage.cxx b/src/SongPage.cxx index 5ae54ee9..b56dc06f 100644 --- a/src/SongPage.cxx +++ b/src/SongPage.cxx @@ -306,8 +306,8 @@ SongPage::AddSong(const struct mpd_song *song) noexcept /* create time string and add it */ if (mpd_song_get_duration(song) > 0) { - char length[16]; - format_duration_short(length, mpd_song_get_duration(song)); + char length_buffer[16]; + const auto length = format_duration_short(length_buffer, mpd_song_get_duration(song)); std::string_view value = length; @@ -315,15 +315,15 @@ SongPage::AddSong(const struct mpd_song *song) noexcept if (mpd_song_get_end(song) > 0) { char start[16], end[16]; - format_duration_short(start, mpd_song_get_start(song)); - format_duration_short(end, mpd_song_get_end(song)); - value = FmtTruncate(buffer, "{} [{}-{}]"sv, length, start, end); + value = FmtTruncate(buffer, "{} [{}-{}]"sv, length, + format_duration_short(start, mpd_song_get_start(song)), + format_duration_short(end, mpd_song_get_end(song))); } else if (mpd_song_get_start(song) > 0) { char start[16]; - format_duration_short(start, mpd_song_get_start(song)); - value = FmtTruncate(buffer, "{} [{}-]"sv, length, start); + value = FmtTruncate(buffer, "{} [{}-]"sv, length, + format_duration_short(start, mpd_song_get_start(song))); } AppendLine(get_tag_label(LABEL_LENGTH), value, diff --git a/src/SongRowPaint.cxx b/src/SongRowPaint.cxx index 5460e074..9c4d4626 100644 --- a/src/SongRowPaint.cxx +++ b/src/SongRowPaint.cxx @@ -27,9 +27,9 @@ paint_song_row(const Window window, [[maybe_unused]] int y, unsigned width, #ifndef NCMPC_MINI if (options.second_column && mpd_song_get_duration(&song) > 0) { - char duration[32]; - format_duration_short(duration, mpd_song_get_duration(&song)); - width -= strlen(duration) + 1; + char duration_buffer[32]; + const auto duration = format_duration_short(duration_buffer, mpd_song_get_duration(&song)); + width -= duration.size() + 1; window.MoveCursor({(int)width, y}); window.Char(' '); window.String(duration); diff --git a/src/StatusBar.cxx b/src/StatusBar.cxx index ba8bad48..9b4dd8e2 100644 --- a/src/StatusBar.cxx +++ b/src/StatusBar.cxx @@ -8,12 +8,15 @@ #include "strfsong.hxx" #include "DelayedSeek.hxx" #include "time_format.hxx" +#include "lib/fmt/ToSpan.hxx" #include "util/LocaleString.hxx" #include #include +using std::string_view_literals::operator""sv; + StatusBar::StatusBar(EventLoop &event_loop, Point p, unsigned width) noexcept :window(p, {width, 1u}), @@ -49,36 +52,35 @@ StatusBar::ClearMessage() noexcept doupdate(); } -static size_t -format_bitrate(char *p, size_t max_length, +[[nodiscard]] +static std::string_view +format_bitrate(std::span buffer, const struct mpd_status *status) noexcept { #ifndef NCMPC_MINI if (options.visible_bitrate && mpd_status_get_kbit_rate(status) > 0) { - snprintf(p, max_length, - " [%d kbps]", - mpd_status_get_kbit_rate(status)); - return strlen(p); + return FmtTruncate(buffer, " [{} kbps]", + mpd_status_get_kbit_rate(status)); } else { #else - (void)max_length; + (void)buffer; (void)status; #endif - p[0] = '\0'; - return 0; + return {}; #ifndef NCMPC_MINI } #endif } -static void -FormatCurrentSongTime(char *buffer, size_t size, +[[nodiscard]] +static std::string_view +FormatCurrentSongTime(std::span buffer, const struct mpd_status &status, const DelayedSeek &seek) noexcept { if (options.current_time_display == CurrentTimeDisplay::NONE) - return; + return {}; const unsigned total_time = mpd_status_get_total_time(&status); @@ -86,7 +88,7 @@ FormatCurrentSongTime(char *buffer, size_t size, ? seek.GetTime() : mpd_status_get_elapsed_time(&status); - char elapsed_string[32], duration_string[32]; + char elapsed_buffer[32], duration_buffer[32]; /* checks the conf to see whether to display elapsed or remaining time */ @@ -97,7 +99,7 @@ FormatCurrentSongTime(char *buffer, size_t size, case CurrentTimeDisplay::REMAINING: if (total_time == 0) - return; + return {}; elapsed_time = elapsed_time < total_time ? total_time - elapsed_time @@ -106,28 +108,27 @@ FormatCurrentSongTime(char *buffer, size_t size, } /* write out the time */ - format_duration_short(elapsed_string, elapsed_time); + const auto elapsed_string = format_duration_short(elapsed_buffer, elapsed_time); - if (total_time == 0) { - snprintf(buffer, size, " [%s]", elapsed_string); - return; - } + if (total_time == 0) + return FmtTruncate(buffer, " [{}]"sv, elapsed_string); - format_duration_short(duration_string, total_time); - snprintf(buffer, size, " [%s/%s]", elapsed_string, duration_string); + const auto duration_string = format_duration_short(duration_buffer, total_time); + return FmtTruncate(buffer, " [{}/{}]"sv, elapsed_string, duration_string); } -inline size_t -FormatStatusRightText(char *buffer, size_t size, +[[nodiscard]] +static std::string_view +FormatStatusRightText(std::span buffer, const struct mpd_status &status, const DelayedSeek &seek) noexcept { /* display bitrate if visible-bitrate is true */ - size_t offset = format_bitrate(buffer, size, &status); + const auto bitrate_string = format_bitrate(buffer, &status); - FormatCurrentSongTime(buffer + offset, size - offset, status, seek); + const auto time_string = FormatCurrentSongTime(buffer.subspan(bitrate_string.size()), status, seek); - return StringWidthMB(buffer); + return {buffer.data(), bitrate_string.size() + time_string.size()}; } void @@ -159,9 +160,10 @@ StatusBar::Update(const struct mpd_status *status, : 0; if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) { - right_width = FormatStatusRightText(right_text, - sizeof(right_text), - *status, seek); + const auto right_text = FormatStatusRightText(right_buffer, + *status, seek); + right_length = right_text.size(); + right_width = StringWidthMB(right_text); #ifndef NCMPC_MINI int width = COLS - left_width - right_width; @@ -224,7 +226,7 @@ StatusBar::Paint() const noexcept /* display time string */ int x = window_width - right_width; SelectStyle(window, Style::STATUS_TIME); - window.String({x, 0}, right_text); + window.String({x, 0}, {right_buffer, right_length}); } if (!center_text.empty()) { diff --git a/src/StatusBar.hxx b/src/StatusBar.hxx index 6c950f85..ed919c79 100644 --- a/src/StatusBar.hxx +++ b/src/StatusBar.hxx @@ -29,7 +29,8 @@ class StatusBar { #endif const char *left_text; - char right_text[64]; + char right_buffer[64]; + std::size_t right_length; std::string center_text; diff --git a/src/strfsong.cxx b/src/strfsong.cxx index 2988c226..b0a58bf3 100644 --- a/src/strfsong.cxx +++ b/src/strfsong.cxx @@ -212,8 +212,7 @@ _strfsong(char *const s0, char *const end, unsigned duration = mpd_song_get_duration(&song); if (duration > 0) { - format_duration_short(buffer, duration); - value = buffer; + value = format_duration_short(buffer, duration); } } diff --git a/src/time_format.cxx b/src/time_format.cxx index 16e036bf..9d8970c5 100644 --- a/src/time_format.cxx +++ b/src/time_format.cxx @@ -3,19 +3,22 @@ #include "time_format.hxx" #include "i18n.h" +#include "lib/fmt/ToSpan.hxx" #include -void +using std::string_view_literals::operator""sv; + +std::string_view format_duration_short(std::span buffer, unsigned duration) noexcept { if (duration < 3600) - snprintf(buffer.data(), buffer.size(), - "%i:%02i", duration / 60, duration % 60); + return FmtTruncate(buffer, "{}:{:02}"sv, duration / 60, duration % 60); else - snprintf(buffer.data(), buffer.size(), - "%i:%02i:%02i", duration / 3600, - (duration % 3600) / 60, duration % 60); + return FmtTruncate(buffer, "{}:{:02}:{:02}"sv, + duration / 3600, + (duration % 3600) / 60, + duration % 60); } void diff --git a/src/time_format.hxx b/src/time_format.hxx index 93489757..d1061994 100644 --- a/src/time_format.hxx +++ b/src/time_format.hxx @@ -4,8 +4,10 @@ #pragma once #include +#include -void +[[nodiscard]] [[gnu::pure]] +std::string_view format_duration_short(std::span buffer, unsigned duration) noexcept; void