From 190cf0650f4e718cf89604985224ec60b25bfaef Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 2 Oct 2009 08:36:36 +0200 Subject: [PATCH] screen_song: moved code to format_duration_short() New function in utils.c. Use that same function in screen_play.c, status_bar.c and strfsong.c. --- src/screen_play.c | 19 ++++++------------- src/screen_song.c | 13 ++----------- src/status_bar.c | 27 +++++++++++++-------------- src/strfsong.c | 15 +++++---------- src/utils.c | 12 ++++++++++++ src/utils.h | 3 +++ 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/screen_play.c b/src/screen_play.c index f481d5b..aaaace1 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -116,17 +116,6 @@ playlist_restore_selection(void) playlist_save_selection(); } -#ifndef NCMPC_MINI -static char * -format_duration(unsigned duration) -{ - if (duration == 0) - return NULL; - - return g_strdup_printf("%d:%02d", duration / 60, duration % 60); -} -#endif - static const char * list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data) { @@ -146,8 +135,12 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED strfsong(songname, MAX_SONG_LENGTH, options.list_format, song); #ifndef NCMPC_MINI - if(second_column) - *second_column = format_duration(mpd_song_get_duration(song)); + if (second_column != NULL && mpd_song_get_duration(song) > 0) { + char duration[32]; + format_duration_short(duration, sizeof(duration), + mpd_song_get_duration(song)); + *second_column = g_strdup(duration); + } if (idx == lw->selected) { diff --git a/src/screen_song.c b/src/screen_song.c index 7b4a481..d0285e9 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -236,18 +236,9 @@ screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c) max_label_width); /* create time string and add it */ if (mpd_song_get_duration(song) > 0) { - unsigned t = mpd_song_get_duration(song); char length[16]; - - /*write out the time, using hours if time over 60 minutes*/ - if (t > 3600) { - g_snprintf(length, sizeof(length), - "%i:%02i:%02i", - t/3600, (t%3600)/60, t%60); - } else { - g_snprintf(length, sizeof(length), - "%i:%02i", t/60, t%60); - } + format_duration_short(length, sizeof(length), + mpd_song_get_duration(song)); screen_song_append(labels[LENGTH], length, max_label_width); } screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER, diff --git a/src/status_bar.c b/src/status_bar.c index b778bc0..04881af 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -24,6 +24,7 @@ #include "charset.h" #include "strfsong.h" #include "player_command.h" +#include "utils.h" #ifndef NCMPC_MINI #include "hscroll.h" @@ -99,6 +100,8 @@ status_bar_paint(const struct status_bar *p, const struct mpd_status *status, if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) { int total_time = mpd_status_get_total_time(status); if (total_time > 0) { + char elapsed_string[32], duration_string[32]; + /*checks the conf to see whether to display elapsed or remaining time */ if(!strcmp(options.timedisplay_type,"elapsed")) elapsedTime = mpd_status_get_elapsed_time(status); @@ -121,20 +124,16 @@ status_bar_paint(const struct status_bar *p, const struct mpd_status *status, } #endif - /*write out the time, using hours if time over 60 minutes*/ - if (total_time > 3600) { - g_snprintf(buffer, sizeof(buffer), - "%s [%i:%02i:%02i/%i:%02i:%02i]", - bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60, - total_time / 3600, - (total_time % 3600)/60, - total_time % 60); - } else { - g_snprintf(buffer, sizeof(buffer), - "%s [%i:%02i/%i:%02i]", - bitrate, elapsedTime/60, elapsedTime%60, - total_time / 60, total_time % 60); - } + /* write out the time */ + format_duration_short(elapsed_string, + sizeof(elapsed_string), + elapsedTime); + format_duration_short(duration_string, + sizeof(duration_string), + total_time); + + g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]", + bitrate, elapsed_string, duration_string); #ifndef NCMPC_MINI } else { g_snprintf(buffer, sizeof(buffer), diff --git a/src/strfsong.c b/src/strfsong.c index ff1d6ad..a146583 100644 --- a/src/strfsong.c +++ b/src/strfsong.c @@ -19,6 +19,7 @@ #include "strfsong.h" #include "charset.h" +#include "utils.h" #include @@ -184,16 +185,10 @@ _strfsong(gchar *s, unsigned duration = mpd_song_get_duration(song); if (duration > 0) { - if (duration > 3600) { - temp = g_strdup_printf("%d:%02d:%02d", - duration / 3600, - (duration % 3600) / 60, - duration % 60); - } else { - temp = g_strdup_printf("%d:%02d", - duration / 60, - duration % 60); - } + char buffer[32]; + format_duration_short(buffer, sizeof(buffer), + duration); + temp = g_strdup(buffer); } } diff --git a/src/utils.c b/src/utils.c index 8023bfb..857e15d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -122,6 +122,18 @@ gcmp_list_from_path(struct mpdclient *c, const gchar *path, return list; } +void +format_duration_short(char *buffer, size_t length, unsigned duration) +{ + if (duration < 3600) + g_snprintf(buffer, length, + "%i:%02i", duration / 60, duration % 60); + else + g_snprintf(buffer, length, + "%i:%02i:%02i", duration / 3600, + (duration % 3600) / 60, duration % 60); +} + void format_duration_long(char *p, size_t length, unsigned long duration) { diff --git a/src/utils.h b/src/utils.h index c9374fa..85331bc 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,6 +40,9 @@ GList * gcmp_list_from_path(struct mpdclient *c, const gchar *path, GList *list, gint types); +void +format_duration_short(char *buffer, size_t length, unsigned duration); + void format_duration_long(char *buffer, size_t length, unsigned long duration); -- 2.30.2