Code

status_bar: remove the status bar clock (option "display-time")
[ncmpc.git] / src / status_bar.c
index 2803b28745a8f1214ad69759402f781a89bb2277..74f91de3a67a75baa121960e8f928484c9934714 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -61,22 +61,21 @@ status_bar_deinit(struct status_bar *p)
 #endif
 }
 
-static gboolean
-status_bar_clear_message(gpointer data)
+void
+status_bar_clear_message(struct status_bar *p)
 {
-       struct status_bar *p = data;
-       WINDOW *w = p->window.w;
-
        assert(p != NULL);
-       assert(p->message_source_id != 0);
 
-       p->message_source_id = 0;
+       if (p->message_source_id != 0) {
+               g_source_remove(p->message_source_id);
+               p->message_source_id = 0;
+       }
+
+       WINDOW *w = p->window.w;
 
        wmove(w, 0, 0);
        wclrtoeol(w);
        wrefresh(w);
-
-       return false;
 }
 
 #ifndef NCMPC_MINI
@@ -99,10 +98,6 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                 const struct mpd_song *song)
 {
        WINDOW *w = p->window.w;
-       enum mpd_state state;
-       int elapsedTime = 0;
-       const char *str = NULL;
-       int x = 0;
        char buffer[p->window.cols * 4 + 1];
 
 #ifndef NCMPC_MINI
@@ -117,9 +112,10 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
        wclrtoeol(w);
        colors_use(w, COLOR_STATUS_BOLD);
 
-       state = status == NULL ? MPD_STATE_UNKNOWN
+       enum mpd_state state = status == NULL ? MPD_STATE_UNKNOWN
                : mpd_status_get_state(status);
 
+       const char *str = NULL;
        switch (state) {
        case MPD_STATE_PLAY:
                str = _("Playing:");
@@ -132,6 +128,7 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                break;
        }
 
+       int x = 0;
        if (str) {
                waddstr(w, str);
                x += utf8_width(str) + 1;
@@ -139,8 +136,12 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
 
        /* create time string */
        if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
+               int elapsedTime = seek_id >= 0 &&
+                       seek_id == mpd_status_get_song_id(status)
+                       ? (unsigned)seek_target_time
+                       : mpd_status_get_elapsed_time(status);
                int total_time = mpd_status_get_total_time(status);
-               if (total_time > 0) {
+               if (elapsedTime > 0 || total_time > 0) {
 #ifdef NCMPC_MINI
                        static const char bitrate[1];
 #else
@@ -149,14 +150,10 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
                        char elapsed_string[32], duration_string[32];
 
                        /*checks the conf to see whether to display elapsed or remaining time */
-                       if (seek_id >= 0 &&
-                           seek_id == mpd_status_get_song_id(status))
-                               elapsedTime = seek_target_time;
-                       else if (options.display_remaining_time)
-                               elapsedTime = total_time -
-                                       mpd_status_get_elapsed_time(status);
-                       else
-                               elapsedTime = mpd_status_get_elapsed_time(status);
+                       if (options.display_remaining_time)
+                               elapsedTime = elapsedTime < total_time
+                                       ? total_time - elapsedTime
+                                       : 0;
 
                        /* display bitrate if visible-bitrate is true */
 #ifndef NCMPC_MINI
@@ -181,15 +178,7 @@ status_bar_paint(struct status_bar *p, const struct mpd_status *status,
 #endif
                }
        } else {
-#ifndef NCMPC_MINI
-               if (options.display_time) {
-                       time_t timep;
-
-                       time(&timep);
-                       strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
-               } else
-#endif
-                       buffer[0] = 0;
+               buffer[0] = 0;
        }
 
        /* display song */
@@ -243,6 +232,17 @@ status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
        mvwin(p->window.w, y, x);
 }
 
+static gboolean
+status_bar_clear_message_cb(gpointer data)
+{
+       struct status_bar *p = data;
+       assert(p->message_source_id != 0);
+       p->message_source_id = 0;
+
+       status_bar_clear_message(p);
+       return false;
+}
+
 void
 status_bar_message(struct status_bar *p, const char *msg)
 {
@@ -262,5 +262,5 @@ status_bar_message(struct status_bar *p, const char *msg)
        if (p->message_source_id != 0)
                g_source_remove(p->message_source_id);
        p->message_source_id = g_timeout_add(options.status_message_time * 1000,
-                                            status_bar_clear_message, p);
+                                            status_bar_clear_message_cb, p);
 }