Skip to content

Commit

Permalink
time_format: use libfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 2, 2024
1 parent 6c6a4e2 commit b1801ee
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,24 @@ 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;

char buffer[64];

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,
Expand Down
6 changes: 3 additions & 3 deletions src/SongRowPaint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 32 additions & 30 deletions src/StatusBar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include "strfsong.hxx"
#include "DelayedSeek.hxx"
#include "time_format.hxx"
#include "lib/fmt/ToSpan.hxx"
#include "util/LocaleString.hxx"

#include <mpd/client.h>

#include <string.h>

using std::string_view_literals::operator""sv;

StatusBar::StatusBar(EventLoop &event_loop,
Point p, unsigned width) noexcept
:window(p, {width, 1u}),
Expand Down Expand Up @@ -49,44 +52,43 @@ StatusBar::ClearMessage() noexcept
doupdate();
}

static size_t
format_bitrate(char *p, size_t max_length,
[[nodiscard]]
static std::string_view
format_bitrate(std::span<char> 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<char> 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);

unsigned elapsed_time = seek.IsSeeking(mpd_status_get_song_id(&status))
? 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 */
Expand All @@ -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
Expand All @@ -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<char> 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion src/StatusBar.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/strfsong.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/time_format.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@

#include "time_format.hxx"
#include "i18n.h"
#include "lib/fmt/ToSpan.hxx"

#include <stdio.h>

void
using std::string_view_literals::operator""sv;

std::string_view
format_duration_short(std::span<char> 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
Expand Down
4 changes: 3 additions & 1 deletion src/time_format.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#pragma once

#include <span>
#include <string_view>

void
[[nodiscard]] [[gnu::pure]]
std::string_view
format_duration_short(std::span<char> buffer, unsigned duration) noexcept;

void
Expand Down

0 comments on commit b1801ee

Please sign in to comment.