Code

screen_song: moved code to format_duration_short()
authorMax Kellermann <max@duempel.org>
Fri, 2 Oct 2009 06:36:36 +0000 (08:36 +0200)
committerMax Kellermann <max@duempel.org>
Fri, 2 Oct 2009 06:36:36 +0000 (08:36 +0200)
New function in utils.c.  Use that same function in screen_play.c,
status_bar.c and strfsong.c.

src/screen_play.c
src/screen_song.c
src/status_bar.c
src/strfsong.c
src/utils.c
src/utils.h

index f481d5b4f4dfbba78c8e38e7b9880f4b6c1361bd..aaaace14505572e2a152414918985477bc91c66d 100644 (file)
@@ -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)
        {
index 7b4a4810f11a320a4ae944736cbe9339f5521b12..d0285e9ab9880632c6597b25e35f1fef87d0aa83 100644 (file)
@@ -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,
index b778bc032e2af97b5198bf394611e76200e3b404..04881afc939ca2ce2b61755b5b7735ed9afe0ac4 100644 (file)
@@ -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),
index ff1d6ad6b31931bea0cb9ea22e38ee47e00df099..a146583516553754d14d4c9da03f6362de858db3 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "strfsong.h"
 #include "charset.h"
+#include "utils.h"
 
 #include <mpd/client.h>
 
@@ -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);
                        }
                }
 
index 8023bfb00879c67ae55abac46787fb9fc9ea58aa..857e15d9baa7c63f5677345e8deb87e704484e82 100644 (file)
@@ -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)
 {
index c9374fa3a3fc51c6b0f140ecf88d47688b22f444..85331bc64943fc616a138db21bee2b567e554066 100644 (file)
@@ -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);