cleanup times_in_seconds enhancement
authorPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 22:39:08 +0000 (23:39 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 22:39:08 +0000 (23:39 +0100)
This also adds code to unset the variable before reloading the config.

src/common.c
src/conky.c
src/conky.h
src/timeinfo.c
src/timeinfo.h
src/users.c

index f691a92..c4099d0 100644 (file)
@@ -34,6 +34,7 @@
 #include "logging.h"
 #include "net_stat.h"
 #include "specials.h"
+#include "timeinfo.h"
 #include <ctype.h>
 #include <errno.h>
 #include <sys/time.h>
@@ -218,6 +219,11 @@ void format_seconds(char *buf, unsigned int n, long seconds)
        long days;
        int hours, minutes;
 
+       if (times_in_seconds()) {
+               snprintf(buf, n, "%ld", seconds);
+               return;
+       }
+
        days = seconds / 86400;
        seconds %= 86400;
        hours = seconds / 3600;
@@ -237,6 +243,11 @@ void format_seconds_short(char *buf, unsigned int n, long seconds)
        long days;
        int hours, minutes;
 
+       if (times_in_seconds()) {
+               snprintf(buf, n, "%ld", seconds);
+               return;
+       }
+
        days = seconds / 86400;
        seconds %= 86400;
        hours = seconds / 3600;
index 7561601..6b798ea 100644 (file)
@@ -167,7 +167,6 @@ int top_io;
 int top_running;
 #endif
 int output_methods;
-char times_in_seconds = 0;
 static int extra_newline;
 enum x_initialiser_state x_initialised = NO;
 static volatile int g_signal_pending;
@@ -727,6 +726,11 @@ static inline void format_media_player_time(char *buf, const int size,
 {
        int days, hours, minutes;
 
+       if (times_in_seconds()) {
+               snprintf(buf, size, "%d", seconds);
+               return;
+       }
+
        days = seconds / (24 * 60 * 60);
        seconds %= (24 * 60 * 60);
        hours = seconds / (60 * 60);
@@ -1508,11 +1512,7 @@ void generate_text_internal(char *p, int p_max_size,
 
                                generate_text_internal(buf, max_user_text, *obj->sub, cur);
                                obj->data.s = buf;
-                               if(times_in_seconds) {
-                                       print_format_time(obj, p, p_max_size);
-                               } else {
-                                       NORM_ERR("Enable \"times_in_seconds\" to use $format_time");
-                               }
+                               print_format_time(obj, p, p_max_size);
                        }
                        /* mail stuff */
                        OBJ(mails) {
@@ -1939,18 +1939,10 @@ void generate_text_internal(char *p, int p_max_size,
                        }
 #endif /* X11 */
                        OBJ(uptime_short) {
-                               if(times_in_seconds) {
-                                       snprintf(p, p_max_size, "%d", (int) cur->uptime);
-                               } else {
-                                       format_seconds_short(p, p_max_size, (int) cur->uptime);
-                               }
+                               format_seconds_short(p, p_max_size, (int) cur->uptime);
                        }
                        OBJ(uptime) {
-                               if(times_in_seconds) {
-                                       snprintf(p, p_max_size, "%d", (int) cur->uptime);
-                               } else {
-                                       format_seconds(p, p_max_size, (int) cur->uptime);
-                               }
+                               format_seconds(p, p_max_size, (int) cur->uptime);
                        }
 #ifdef __linux__
                        OBJ(user_names) {
@@ -1963,7 +1955,7 @@ void generate_text_internal(char *p, int p_max_size,
                                snprintf(p, p_max_size, "%s", cur->users.times);
                        }
                        OBJ(user_time) {
-                               update_user_time(obj->data.s, times_in_seconds);
+                               update_user_time(obj->data.s);
                                snprintf(p, p_max_size, "%s", cur->users.ctime);
                        }
                        OBJ(user_number) {
@@ -2027,18 +2019,10 @@ void generate_text_internal(char *p, int p_max_size,
                        OBJ(mpd_status)
                                mpd_printf("%s", status);
                        OBJ(mpd_elapsed) {
-                               if(times_in_seconds) {
-                                       snprintf(p, p_max_size, "%d", mpd_get_info()->elapsed);
-                               } else {
-                                       format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
-                               }
+                               format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
                        }
                        OBJ(mpd_length) {
-                               if(times_in_seconds) {
-                                       snprintf(p, p_max_size, "%d", mpd_get_info()->length);
-                               } else {
-                                       format_media_player_time(p, p_max_size, mpd_get_info()->length);
-                               }
+                               format_media_player_time(p, p_max_size, mpd_get_info()->length);
                        }
                        OBJ(mpd_percent) {
                                percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
@@ -4459,6 +4443,8 @@ static void set_default_configurations(void)
        stuff_in_uppercase = 0;
        info.users.number = 1;
 
+       set_times_in_seconds(0);
+
 #ifdef TCP_PORT_MONITOR
        /* set default connection limit */
        tcp_portmon_set_max_connections(0);
@@ -4923,7 +4909,7 @@ char load_config_file(const char *f)
                }
 #endif /* X11 */
                CONF("times_in_seconds") {
-                       times_in_seconds = string_to_bool(value);
+                       set_times_in_seconds(string_to_bool(value));
                }
                CONF("out_to_console") {
                        if(string_to_bool(value)) {
index 0f47b1e..7878288 100644 (file)
@@ -309,7 +309,7 @@ extern struct information info;
 
 /* defined in users.c */
 void update_users(void);
-void update_user_time(char *tty, char times_in_seconds);
+void update_user_time(char *tty);
 
 /* defined in conky.c */
 extern double current_update_time, last_update_time, update_interval;
index 1cb7628..9e5288b 100644 (file)
 #include <errno.h>
 #include <logging.h>
 
+char print_times_in_seconds = 0;
+
 struct tztime_s {
        char *tz;       /* timezone variable */
        char *fmt;      /* time display formatting */
 };
 
+void set_times_in_seconds(char val)
+{
+       print_times_in_seconds = val;
+}
+
+char times_in_seconds(void)
+{
+       return print_times_in_seconds;
+}
+
 void scan_time(struct text_object *obj, const char *arg)
 {
        obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size);
@@ -150,6 +162,11 @@ void print_format_time(struct text_object *obj, char *p, unsigned int p_max_size
        int minutes, hours, days, weeks;
        char show_minutes = 0, show_hours = 0, show_days = 0, show_weeks = 0, hidestring;
 
+       if (!times_in_seconds()) {
+               NORM_ERR("Enable \"times_in_seconds\" to use $format_time");
+               return;
+       }
+
        errno = 0;
        seconds = strtod(obj->data.s, &currentchar);
        if(errno == 0 && obj->data.s != currentchar) {
index 8aa505f..7988cda 100644 (file)
 #ifndef _TIMEINFO_H
 #define _TIMEINFO_H
 
+/* for the times_in_seconds configuration variable and it's users */
+void set_times_in_seconds(char);
+char times_in_seconds(void);
+
 /* since time and utime are quite equal, certain functions
  * are shared in between both text object types. */
 
index 1b97725..73e8d08 100644 (file)
@@ -93,7 +93,7 @@ static void user_time(char *ptr)
                }
        }
 }
-static void tty_user_time(char *ptr, char *tty, char times_in_seconds)
+static void tty_user_time(char *ptr, char *tty)
 {
        time_t real, diff, log_in;
        char buf[BUFLEN] = "";
@@ -111,11 +111,7 @@ static void tty_user_time(char *ptr, char *tty, char times_in_seconds)
 
        time(&real);
        diff = difftime(real, log_in);
-       if(times_in_seconds) {
-               snprintf(buf, BUFLEN, "%d", (int) diff);
-       } else {
-               format_seconds(buf, BUFLEN, diff);
-       }
+       format_seconds(buf, BUFLEN, diff);
        strncpy(ptr, buf, BUFLEN-1);
 }
 
@@ -133,7 +129,7 @@ static void users_alloc(struct information *ptr)
        }
 }
 
-void update_user_time(char *tty, char times_in_seconds)
+void update_user_time(char *tty)
 {
        struct information *current_info = &info;
        char temp[BUFLEN] = "";
@@ -142,7 +138,7 @@ void update_user_time(char *tty, char times_in_seconds)
                current_info->users.ctime = malloc(text_buffer_size);
        }
 
-       tty_user_time(temp, tty, times_in_seconds);
+       tty_user_time(temp, tty);
 
        if (temp != NULL) {
                if (current_info->users.ctime) {