From 7f436d614b11d72893d856f1c3d817ab34b0b9eb Mon Sep 17 00:00:00 2001 From: Will Song Date: Tue, 27 Jan 2015 19:55:16 -0600 Subject: use custom format string for time preference --- src/command/command.c | 22 +++++----------------- src/command/commands.c | 23 ++++------------------- src/config/preferences.c | 2 +- src/ui/console.c | 6 ++---- src/ui/window.c | 9 +++------ 5 files changed, 15 insertions(+), 47 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 8e16276b..1441ef46 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -620,10 +620,10 @@ static struct cmd_t command_defs[] = { "/time", cmd_time, parse_args, 1, 1, &cons_time_setting, - { "/time minutes|seconds", "Time display.", - { "/time minutes|seconds", + { "/time ", "Time display.", + { "/time ", "---------------------", - "Configure time precision for the main window.", + "Configure the time format for the main window.", NULL } } }, { "/inpblock", @@ -1121,7 +1121,6 @@ static Autocomplete form_ac; static Autocomplete form_field_multi_ac; static Autocomplete occupants_ac; static Autocomplete occupants_default_ac; -static Autocomplete time_ac; static Autocomplete resource_ac; static Autocomplete inpblock_ac; @@ -1461,15 +1460,6 @@ cmd_init(void) autocomplete_add(occupants_default_ac, "show"); autocomplete_add(occupants_default_ac, "hide"); - time_ac = autocomplete_new(); - autocomplete_add(time_ac, "minutes"); - autocomplete_add(time_ac, "seconds"); - autocomplete_add(time_ac, "off"); - - time_ac = autocomplete_new(); - autocomplete_add(time_ac, "minutes"); - autocomplete_add(time_ac, "seconds"); - resource_ac = autocomplete_new(); autocomplete_add(resource_ac, "set"); autocomplete_add(resource_ac, "off"); @@ -1532,7 +1522,6 @@ cmd_uninit(void) autocomplete_free(form_field_multi_ac); autocomplete_free(occupants_ac); autocomplete_free(occupants_default_ac); - autocomplete_free(time_ac); autocomplete_free(resource_ac); autocomplete_free(inpblock_ac); } @@ -1697,7 +1686,6 @@ cmd_reset_autocomplete() autocomplete_reset(form_field_multi_ac); autocomplete_reset(occupants_ac); autocomplete_reset(occupants_default_ac); - autocomplete_reset(time_ac); autocomplete_reset(resource_ac); autocomplete_reset(inpblock_ac); @@ -2010,8 +1998,8 @@ _cmd_complete_parameters(const char * const input) } } - gchar *cmds[] = { "/help", "/prefs", "/disco", "/close", "/wins", "/subject", "/room", "/time" }; - Autocomplete completers[] = { help_ac, prefs_ac, disco_ac, close_ac, wins_ac, subject_ac, room_ac, time_ac }; + gchar *cmds[] = { "/help", "/prefs", "/disco", "/close", "/wins", "/subject", "/room", }; + Autocomplete completers[] = { help_ac, prefs_ac, disco_ac, close_ac, wins_ac, subject_ac, room_ac, }; for (i = 0; i < ARRAY_SIZE(cmds); i++) { result = autocomplete_param_with_ac(input, cmds[i], completers[i], TRUE); diff --git a/src/command/commands.c b/src/command/commands.c index 60ef3780..02910f99 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -3257,25 +3257,10 @@ cmd_wrap(gchar **args, struct cmd_help_t help) gboolean cmd_time(gchar **args, struct cmd_help_t help) { - if (g_strcmp0(args[0], "minutes") == 0) { - prefs_set_string(PREF_TIME, "minutes"); - cons_show("Time precision set to minutes."); - wins_resize_all(); - return TRUE; - } else if (g_strcmp0(args[0], "seconds") == 0) { - prefs_set_string(PREF_TIME, "seconds"); - cons_show("Time precision set to seconds."); - wins_resize_all(); - return TRUE; - } else if (g_strcmp0(args[0], "off") == 0) { - prefs_set_string(PREF_TIME, "off"); - cons_show("Time display disabled."); - wins_resize_all(); - return TRUE; - } else { - cons_show("Usage: %s", help.usage); - return TRUE; - } + prefs_set_string(PREF_TIME, args[0]); + cons_show("Time format set to '%s'", args[0]); + wins_resize_all(); + return TRUE; } gboolean diff --git a/src/config/preferences.c b/src/config/preferences.c index 67f12b18..652d4ed9 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -727,7 +727,7 @@ _get_default_string(preference_t pref) case PREF_ROSTER_BY: return "none"; case PREF_TIME: - return "seconds"; + return "%H:%M:%S"; default: return NULL; } diff --git a/src/ui/console.c b/src/ui/console.c index dd50d6d3..689f218c 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -919,12 +919,10 @@ void cons_time_setting(void) { char *pref_time = prefs_get_string(PREF_TIME); - if (g_strcmp0(pref_time, "minutes") == 0) - cons_show("Time (/time) : minutes"); - else if (g_strcmp0(pref_time, "off") == 0) + if (g_strcmp0(pref_time, "off") == 0) cons_show("Time (/time) : OFF"); else - cons_show("Time (/time) : seconds"); + cons_show("Time (/time) : %s", pref_time); prefs_free_string(pref_time); } diff --git a/src/ui/window.c b/src/ui/window.c index dfec5aab..7757fe39 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -882,14 +883,10 @@ _win_print(ProfWin *window, const char show_char, GDateTime *time, if ((flags & NO_DATE) == 0) { gchar *date_fmt = NULL; char *time_pref = prefs_get_string(PREF_TIME); - if (g_strcmp0(time_pref, "minutes") == 0) { - date_fmt = g_date_time_format(time, "%H:%M"); - } else if (g_strcmp0(time_pref, "seconds") == 0) { - date_fmt = g_date_time_format(time, "%H:%M:%S"); - } + date_fmt = g_date_time_format(time, time_pref); free(time_pref); - if (date_fmt) { + if (date_fmt && strlen(date_fmt)) { if ((flags & NO_COLOUR_DATE) == 0) { wattron(window->layout->win, theme_attrs(THEME_TIME)); } -- cgit 1.4.1-2-gfad0 From f2629aca7bdfd280e8e7268952e9016c56b2294e Mon Sep 17 00:00:00 2001 From: Will Song Date: Fri, 29 May 2015 20:23:09 -0500 Subject: updated statusbar to use strftime formats --- src/ui/statusbar.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index 581e63df..c388a874 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -129,10 +129,13 @@ status_bar_resize(void) if (message) { char *time_pref = prefs_get_string(PREF_TIME_STATUSBAR); - if (g_strcmp0(time_pref, "minutes") == 0) { - mvwprintw(status_bar, 0, 10, message); - } else if (g_strcmp0(time_pref, "seconds") == 0) { - mvwprintw(status_bar, 0, 13, message); + gchar *date_fmt = g_date_time_format(last_time, time_pref); + assert(date_fmt != NULL); + size_t len = strlen(date_fmt); + if (g_strcmp0(time_pref, "") != 0) { + /* 01234567890123456 + * [HH:MM] message */ + mvwprintw(status_bar, 0, 5 + len, message); } else { mvwprintw(status_bar, 0, 1, message); } @@ -303,10 +306,11 @@ status_bar_print_message(const char * const msg) message = strdup(msg); char *time_pref = prefs_get_string(PREF_TIME_STATUSBAR); - if (g_strcmp0(time_pref, "minutes") == 0) { - mvwprintw(status_bar, 0, 10, message); - } else if (g_strcmp0(time_pref, "seconds") == 0) { - mvwprintw(status_bar, 0, 13, message); + gchar *date_fmt = g_date_time_format(last_time, time_pref); + assert(date_fmt != NULL); + size_t len = strlen(date_fmt); + if (g_strcmp0(time_pref, "") != 0) { + mvwprintw(status_bar, 0, 5 + len, message); } else { mvwprintw(status_bar, 0, 1, message); } @@ -436,26 +440,16 @@ _status_bar_draw(void) int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET); char *time_pref = prefs_get_string(PREF_TIME_STATUSBAR); - if (g_strcmp0(time_pref, "minutes") == 0) { - gchar *date_fmt = g_date_time_format(last_time, "%H:%M"); - assert(date_fmt != NULL); - wattron(status_bar, bracket_attrs); - mvwaddch(status_bar, 0, 1, '['); - wattroff(status_bar, bracket_attrs); - mvwprintw(status_bar, 0, 2, date_fmt); - wattron(status_bar, bracket_attrs); - mvwaddch(status_bar, 0, 7, ']'); - wattroff(status_bar, bracket_attrs); - g_free(date_fmt); - } else if (g_strcmp0(time_pref, "seconds") == 0) { - gchar *date_fmt = g_date_time_format(last_time, "%H:%M:%S"); + if (g_strcmp0(time_pref, "") != 0) { + gchar *date_fmt = g_date_time_format(last_time, time_pref); assert(date_fmt != NULL); + size_t len = strlen(date_fmt); wattron(status_bar, bracket_attrs); mvwaddch(status_bar, 0, 1, '['); wattroff(status_bar, bracket_attrs); mvwprintw(status_bar, 0, 2, date_fmt); wattron(status_bar, bracket_attrs); - mvwaddch(status_bar, 0, 10, ']'); + mvwaddch(status_bar, 0, 2 + len, ']'); wattroff(status_bar, bracket_attrs); g_free(date_fmt); } -- cgit 1.4.1-2-gfad0 From 3842daa334095fcd4e8a851ca143b8a72a9094fc Mon Sep 17 00:00:00 2001 From: Will Song Date: Fri, 29 May 2015 20:45:10 -0500 Subject: fix indent code --- src/ui/window.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/ui/window.c b/src/ui/window.c index d2462a61..60b47659 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -61,7 +61,7 @@ static void _win_print(ProfWin *window, const char show_char, GDateTime *time, int flags, theme_item_t theme_item, const char * const from, const char * const message, DeliveryReceipt *receipt); -static void _win_print_wrapped(WINDOW *win, const char * const message); +static void _win_print_wrapped(WINDOW *win, const char * const message, size_t indent); int win_roster_cols(void) @@ -952,12 +952,18 @@ _win_print(ProfWin *window, const char show_char, GDateTime *time, gboolean me_message = FALSE; int offset = 0; int colour = theme_attrs(THEME_ME); + size_t indent = 0; if ((flags & NO_DATE) == 0) { gchar *date_fmt = NULL; char *time_pref = prefs_get_string(PREF_TIME); date_fmt = g_date_time_format(time, time_pref); free(time_pref); + assert(date_fmt != NULL); + + if(strlen(date_fmt) != 0){ + indent = 3 + strlen(date_fmt); + } if (date_fmt && strlen(date_fmt)) { if ((flags & NO_COLOUR_DATE) == 0) { @@ -1004,7 +1010,7 @@ _win_print(ProfWin *window, const char show_char, GDateTime *time, } if (prefs_get_boolean(PREF_WRAP)) { - _win_print_wrapped(window->layout->win, message+offset); + _win_print_wrapped(window->layout->win, message+offset, indent); } else { wprintw(window->layout->win, "%s", message+offset); } @@ -1034,20 +1040,11 @@ _win_indent(WINDOW *win, int size) } static void -_win_print_wrapped(WINDOW *win, const char * const message) +_win_print_wrapped(WINDOW *win, const char * const message, size_t indent) { int wordi = 0; char *word = malloc(strlen(message) + 1); - char *time_pref = prefs_get_string(PREF_TIME); - int indent = 0; - if (g_strcmp0(time_pref, "minutes") == 0) { - indent = 8; - } else if (g_strcmp0(time_pref, "seconds") == 0) { - indent = 11; - } - free(time_pref); - gchar *curr_ch = g_utf8_offset_to_pointer(message, 0); while (*curr_ch != '\0') { -- cgit 1.4.1-2-gfad0 From 0e7507a1aa3390fe1899e3b4d705c823ff4011f8 Mon Sep 17 00:00:00 2001 From: Will Song Date: Fri, 29 May 2015 20:49:28 -0500 Subject: update autocomplete --- src/command/command.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 1871ea6c..3fa467d4 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1201,7 +1201,7 @@ static Autocomplete occupants_ac; static Autocomplete occupants_default_ac; static Autocomplete occupants_show_ac; static Autocomplete time_ac; -static Autocomplete time_statusbar_ac; +static Autocomplete time_format_ac; static Autocomplete resource_ac; static Autocomplete inpblock_ac; static Autocomplete receipts_ac; @@ -1546,15 +1546,12 @@ cmd_init(void) autocomplete_add(occupants_show_ac, "jid"); time_ac = autocomplete_new(); - autocomplete_add(time_ac, "minutes"); - autocomplete_add(time_ac, "seconds"); - autocomplete_add(time_ac, "off"); + autocomplete_add(time_ac, "main"); autocomplete_add(time_ac, "statusbar"); - time_statusbar_ac = autocomplete_new(); - autocomplete_add(time_statusbar_ac, "minutes"); - autocomplete_add(time_statusbar_ac, "seconds"); - autocomplete_add(time_statusbar_ac, "off"); + time_format_ac = autocomplete_new(); + autocomplete_add(time_format_ac, "set"); + autocomplete_add(time_format_ac, "off"); resource_ac = autocomplete_new(); autocomplete_add(resource_ac, "set"); @@ -1624,7 +1621,7 @@ cmd_uninit(void) autocomplete_free(occupants_default_ac); autocomplete_free(occupants_show_ac); autocomplete_free(time_ac); - autocomplete_free(time_statusbar_ac); + autocomplete_free(time_format_ac); autocomplete_free(resource_ac); autocomplete_free(inpblock_ac); autocomplete_free(receipts_ac); @@ -1796,7 +1793,7 @@ cmd_reset_autocomplete() autocomplete_reset(occupants_default_ac); autocomplete_reset(occupants_show_ac); autocomplete_reset(time_ac); - autocomplete_reset(time_statusbar_ac); + autocomplete_reset(time_format_ac); autocomplete_reset(resource_ac); autocomplete_reset(inpblock_ac); autocomplete_reset(receipts_ac); @@ -2682,7 +2679,12 @@ _time_autocomplete(const char * const input) { char *found = NULL; - found = autocomplete_param_with_ac(input, "/time statusbar", time_statusbar_ac, TRUE); + found = autocomplete_param_with_ac(input, "/time statusbar", time_format_ac, TRUE); + if (found) { + return found; + } + + found = autocomplete_param_with_ac(input, "/time main", time_format_ac, TRUE); if (found) { return found; } -- cgit 1.4.1-2-gfad0 From 83bed119e4c5a39bc9f76640187db94dffac23cb Mon Sep 17 00:00:00 2001 From: Will Song Date: Fri, 29 May 2015 20:55:33 -0500 Subject: remove memory leak from creating a gdatetime string --- src/ui/statusbar.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index c388a874..f3d204f1 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -132,6 +132,7 @@ status_bar_resize(void) gchar *date_fmt = g_date_time_format(last_time, time_pref); assert(date_fmt != NULL); size_t len = strlen(date_fmt); + g_free(date_fmt); if (g_strcmp0(time_pref, "") != 0) { /* 01234567890123456 * [HH:MM] message */ @@ -309,6 +310,7 @@ status_bar_print_message(const char * const msg) gchar *date_fmt = g_date_time_format(last_time, time_pref); assert(date_fmt != NULL); size_t len = strlen(date_fmt); + g_free(date_fmt); if (g_strcmp0(time_pref, "") != 0) { mvwprintw(status_bar, 0, 5 + len, message); } else { -- cgit 1.4.1-2-gfad0 From 837f8b1f10fc94cd784f9c03ff469298822efd4c Mon Sep 17 00:00:00 2001 From: Will Song Date: Sat, 30 May 2015 00:42:19 -0500 Subject: add examples and be more specific about time formatting --- src/command/command.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 3fa467d4..4086278c 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -688,13 +688,18 @@ static struct cmd_t command_defs[] = cmd_time, parse_args, 1, 3, &cons_time_setting, { "/time main|statusbar set|off [format]", "Time display.", { "/time main|statusbar set|off [format]", - "---------------------------------", + "-------------------------------------", "Configure time display preferences.", "", - "main set : Change strftime format to in main window.", + "main set : Change time format to in main window.", "main off : Do not show time in main window.", - "statusbar set : Change strftime format to in statusbar.", + "statusbar set : Change time format to in statusbar.", "statusbar off : Do not show time in status bar.", + "", + "Time formats are strings supported by g_date_time_format.", + "See https://developer.gnome.org/glib/stable/glib-GDateTime.html#g-date-time-format for more details.", + "Example: /time main set %H:%M (main time will be set to HH:MM)", + "Example: /time statusbar set yolo (statusbar time will all be changed to a static yolo)", NULL } } }, { "/inpblock", -- cgit 1.4.1-2-gfad0