From 105c9c2943987f89c0429e300e4b6587f975dbb5 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 7 Mar 2018 21:29:41 +0000 Subject: [PATCH] Add time to status bar --- src/ui/core.c | 3 ++- src/ui/statusbar.c | 53 ++++++++++++++++++++++++++++++++++++++++++---- src/ui/statusbar.h | 3 ++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 49c8406a..9c26750a 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -102,7 +102,7 @@ ui_init(void) ui_load_colours(); refresh(); create_title_bar(); - create_status_bar(); + status_bar_init(); status_bar_active(1); create_input_window(); wins_init(); @@ -183,6 +183,7 @@ ui_close(void) notifier_uninit(); wins_destroy(); inp_close(); + status_bar_close(); endwin(); } diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index 2096aa42..50cb1a0e 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -51,14 +51,13 @@ #include "ui/inputwin.h" #include "ui/screen.h" -#define TIME_CHECK 60000000 - typedef struct _status_bar_t { - char *time; + gchar *time; char *message; GList *tabs; } StatusBar; +static GTimeZone *tz; static StatusBar *statusbar; static WINDOW *statusbar_win; //static char *message = NULL; @@ -80,13 +79,18 @@ static WINDOW *statusbar_win; static void _status_bar_draw(void); void -create_status_bar(void) +status_bar_init(void) { + tz = g_time_zone_new_local(); + statusbar = malloc(sizeof(StatusBar)); + statusbar->time = NULL; + int row = screen_statusbar_row(); int cols = getmaxx(stdscr); statusbar_win = newwin(1, cols, row, 0); wbkgd(statusbar_win, theme_attrs(THEME_STATUS_TEXT)); + _status_bar_draw(); // int i; @@ -122,6 +126,19 @@ create_status_bar(void) // _status_bar_draw(); } +void +status_bar_close(void) +{ + if (tz) { + g_time_zone_unref(tz); + } + if (statusbar) { + if (statusbar->time) { + g_free(statusbar->time); + } + } +} + void status_bar_update_virtual(void) { @@ -475,6 +492,34 @@ status_bar_clear_message(void) static void _status_bar_draw(void) { + char *time_pref = prefs_get_string(PREF_TIME_STATUSBAR); + if (g_strcmp0(time_pref, "off") != 0) { + if (statusbar->time) { + g_free(statusbar->time); + statusbar->time = NULL; + } + + GDateTime *datetime = g_date_time_new_now(tz); + statusbar->time = g_date_time_format(datetime, time_pref); + assert(statusbar->time != NULL); + g_date_time_unref(datetime); + + int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET); + int time_attrs = theme_attrs(THEME_STATUS_TIME); + + size_t len = strlen(statusbar->time); + wattron(statusbar_win, bracket_attrs); + mvwaddch(statusbar_win, 0, 1, '['); + wattroff(statusbar_win, bracket_attrs); + wattron(statusbar_win, time_attrs); + mvwprintw(statusbar_win, 0, 2, statusbar->time); + wattroff(statusbar_win, time_attrs); + wattron(statusbar_win, bracket_attrs); + mvwaddch(statusbar_win, 0, 2 + len, ']'); + wattroff(statusbar_win, bracket_attrs); + } + prefs_free_string(time_pref); + wnoutrefresh(statusbar_win); inp_put_back(); diff --git a/src/ui/statusbar.h b/src/ui/statusbar.h index def9c04b..370b4bbb 100644 --- a/src/ui/statusbar.h +++ b/src/ui/statusbar.h @@ -35,7 +35,8 @@ #ifndef UI_STATUSBAR_H #define UI_STATUSBAR_H -void create_status_bar(void); +void status_bar_init(void); +void status_bar_close(void); void status_bar_update_virtual(void); void status_bar_resize(void); void status_bar_clear(void);