diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2016-12-31 18:28:55 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2016-12-31 18:28:55 +0100 |
commit | 668bb3a4add26ae0193ec97097210441e8217b75 (patch) | |
tree | 68357b353c9f564187fd5bc4565e66da18ac6720 /src | |
parent | c6baabff27206ff581502106312237c4ecb24e6b (diff) | |
download | weechat-668bb3a4add26ae0193ec97097210441e8217b75.zip |
api: add info "uptime" (WeeChat uptime)
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-command.c | 39 | ||||
-rw-r--r-- | src/core/wee-util.c | 24 | ||||
-rw-r--r-- | src/core/wee-util.h | 2 | ||||
-rw-r--r-- | src/plugins/plugin-api.c | 51 |
4 files changed, 94 insertions, 22 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index dee891cf7..2ed98a250 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -6185,8 +6185,7 @@ COMMAND_CALLBACK(upgrade) COMMAND_CALLBACK(uptime) { - time_t running_time; - int day, hour, min, sec; + int days, hours, minutes, seconds; char string[512]; /* make C compiler happy */ @@ -6194,21 +6193,17 @@ COMMAND_CALLBACK(uptime) (void) data; (void) argv_eol; - running_time = time (NULL) - weechat_first_start_time; - day = running_time / (60 * 60 * 24); - hour = (running_time % (60 * 60 * 24)) / (60 * 60); - min = ((running_time % (60 * 60 * 24)) % (60 * 60)) / 60; - sec = ((running_time % (60 * 60 * 24)) % (60 * 60)) % 60; + util_get_uptime (NULL, &days, &hours, &minutes, &seconds); if ((argc >= 2) && (string_strcasecmp (argv[1], "-o") == 0)) { snprintf (string, sizeof (string), "WeeChat uptime: %d %s %02d:%02d:%02d, started on %s", - day, - (day != 1) ? "days" : "day", - hour, - min, - sec, + days, + (days != 1) ? "days" : "day", + hours, + minutes, + seconds, ctime (&weechat_first_start_time)); string[strlen (string) - 1] = '\0'; (void) input_data (buffer, string); @@ -6218,11 +6213,11 @@ COMMAND_CALLBACK(uptime) snprintf (string, sizeof (string), /* TRANSLATORS: "%s" after "started on" is a date */ _("WeeChat uptime: %d %s %02d:%02d:%02d, started on %s"), - day, - NG_("day", "days", day), - hour, - min, - sec, + days, + NG_("day", "days", days), + hours, + minutes, + seconds, util_get_time_string (&weechat_first_start_time)); (void) input_data (buffer, string); } @@ -6234,17 +6229,17 @@ COMMAND_CALLBACK(uptime) "%s%02d%s:%s%02d%s:%s%02d%s, " "started on %s%s"), GUI_COLOR(GUI_COLOR_CHAT_BUFFER), - day, + days, GUI_COLOR(GUI_COLOR_CHAT), - NG_("day", "days", day), + NG_("day", "days", days), GUI_COLOR(GUI_COLOR_CHAT_BUFFER), - hour, + hours, GUI_COLOR(GUI_COLOR_CHAT), GUI_COLOR(GUI_COLOR_CHAT_BUFFER), - min, + minutes, GUI_COLOR(GUI_COLOR_CHAT), GUI_COLOR(GUI_COLOR_CHAT_BUFFER), - sec, + seconds, GUI_COLOR(GUI_COLOR_CHAT), GUI_COLOR(GUI_COLOR_CHAT_BUFFER), util_get_time_string (&weechat_first_start_time)); diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 0dccb1861..4716956f2 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -766,3 +766,27 @@ util_version_number (const char *version) return (version_int[0] << 24) | (version_int[1] << 16) | (version_int[2] << 8) | version_int[3]; } + +/* + * Return uptime as number of days, hours, minutes, seconds. + */ + +void +util_get_uptime (time_t *total_seconds, int *days, + int *hours, int *minutes, int *seconds) +{ + time_t running_time; + + running_time = time (NULL) - weechat_first_start_time; + + if (total_seconds) + *total_seconds = running_time; + if (days) + *days = running_time / (60 * 60 * 24); + if (hours) + *hours = (running_time % (60 * 60 * 24)) / (60 * 60); + if (minutes) + *minutes = ((running_time % (60 * 60 * 24)) % (60 * 60)) / 60; + if (seconds) + *seconds = ((running_time % (60 * 60 * 24)) % (60 * 60)) % 60; +} diff --git a/src/core/wee-util.h b/src/core/wee-util.h index 47a24575f..e03c7a815 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -52,5 +52,7 @@ extern char *util_search_full_lib_name (const char *filename, const char *sys_directory); extern char *util_file_get_content (const char *filename); extern int util_version_number (const char *version); +extern void util_get_uptime (time_t *total_seconds, int *days, + int *hours, int *minutes, int *seconds); #endif /* WEECHAT_UTIL_H */ diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c index 280d830af..db0612de1 100644 --- a/src/plugins/plugin-api.c +++ b/src/plugins/plugin-api.c @@ -842,6 +842,52 @@ plugin_api_info_nick_color_name_cb (const void *pointer, void *data, } /* + * Returns WeeChat info "uptime". + */ + +const char * +plugin_api_info_uptime_cb (const void *pointer, void *data, + const char *info_name, + const char *arguments) +{ + static char value[32]; + time_t total_seconds; + int days, hours, minutes, seconds; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) info_name; + + if (!arguments || !arguments[0]) + { + /* return uptime with format: "days:hh:mm:ss" */ + util_get_uptime (NULL, &days, &hours, &minutes, &seconds); + snprintf (value, sizeof (value), "%d:%02d:%02d:%02d", + days, hours, minutes, seconds); + return value; + } + + if (strcmp (arguments, "days") == 0) + { + /* return the number of days */ + util_get_uptime (NULL, &days, NULL, NULL, NULL); + snprintf (value, sizeof (value), "%d", days); + return value; + } + + if (strcmp (arguments, "seconds") == 0) + { + /* return the number of seconds */ + util_get_uptime (&total_seconds, NULL, NULL, NULL, NULL); + snprintf (value, sizeof (value), "%ld", total_seconds); + return value; + } + + return NULL; +} + +/* * Returns WeeChat infolist "bar". * * Note: result must be freed after use with function weechat_infolist_free(). @@ -1922,6 +1968,11 @@ plugin_api_init () N_("get nick color name"), N_("nickname"), &plugin_api_info_nick_color_name_cb, NULL, NULL); + hook_info (NULL, "uptime", + N_("WeeChat uptime (format: \"days:hh:mm:ss\")"), + N_("\"days\" (number of days) or \"seconds\" (number of " + "seconds) (optional)"), + &plugin_api_info_uptime_cb, NULL, NULL); /* WeeChat core infolist hooks */ hook_infolist (NULL, "bar", |