From 90c52cc59761fa1d4fa362cd5efd99c5c64f78a4 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 16 Jun 2013 23:24:05 +0000 Subject: [PATCH] Display current time at the top right --- src/date.c | 14 ++++++++++++++ src/date.h | 4 ++++ src/main.c | 7 +++++-- src/view.c | 18 ++++++++++++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/date.c b/src/date.c index 9bcf816..5714f52 100644 --- a/src/date.c +++ b/src/date.c @@ -23,6 +23,7 @@ #include "date.h" /* Global data */ +date_t NOW; date_t SEL; /* Initialize */ @@ -36,6 +37,19 @@ void date_init(void) SEL.day = tm->tm_mday-1; } +void date_sync(void) +{ + time_t sec = time(NULL); + struct tm *tm = localtime(&sec); + + NOW.year = tm->tm_year+1900; + NOW.month = tm->tm_mon; + NOW.day = tm->tm_mday-1; + NOW.hour = tm->tm_hour; + NOW.min = tm->tm_min; + NOW.sec = tm->tm_sec; +} + /* Time functions */ int is_leap_year(year_t year) { diff --git a/src/date.h b/src/date.h index a04d0de..600f262 100644 --- a/src/date.h +++ b/src/date.h @@ -22,6 +22,7 @@ typedef int year_t; typedef int day_t; typedef int hour_t; typedef int min_t; +typedef int sec_t; typedef enum { JAN = 0, @@ -54,13 +55,16 @@ typedef struct { day_t day; hour_t hour; min_t min; + sec_t sec; } date_t; /* Global data */ +extern date_t NOW; // current wall clock time, refreshed at 10 Hz extern date_t SEL; // date and time the user is looking at /* Initialize */ void date_init(void); +void date_sync(void); /* Time functions */ int is_leap_year(year_t year); diff --git a/src/main.c b/src/main.c index fdcfbd8..7d8d517 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,7 @@ int main(int argc, char **argv) keypad(stdscr, TRUE); start_color(); curs_set(false); + timeout(100); use_default_colors(); mousemask(ALL_MOUSE_EVENTS, NULL); @@ -86,6 +87,7 @@ int main(int argc, char **argv) view_init(); /* Draw initial view */ + date_sync(); view_draw(); /* Run */ @@ -93,14 +95,13 @@ int main(int argc, char **argv) MEVENT btn; conf_sync(); int chr = getch(); + date_sync(); if (chr == 'q') break; if (chr == KEY_MOUSE) if (getmouse(&btn) != OK) continue; switch (chr) { - case ERR: - continue; case KEY_RESIZE: endwin(); refresh(); @@ -116,6 +117,8 @@ int main(int argc, char **argv) } if (view_run(chr, btn.bstate, btn.y, btn.x)) continue; + if (chr == ERR) // timeout + continue; debug("main: Unhandled key - Dec %3d, Hex %02x, Oct %03o, Chr <%c>", chr, chr, chr, chr); } diff --git a/src/view.c b/src/view.c index fb97c51..3527849 100644 --- a/src/view.c +++ b/src/view.c @@ -94,6 +94,9 @@ static void draw_header(void) { move(0, 0); attron(COLOR_PAIR(COLOR_TITLE)); + clrtoeol(); + + /* Draw menu */ for (int i = 0; i < N_ELEMENTS(menu); i++) { if (menu[i] == active) attron(A_BOLD); @@ -101,13 +104,21 @@ static void draw_header(void) if (menu[i] == active) attroff(A_BOLD); } - clrtoeol(); + + /* Draw popup window */ if (popup) { + printw("| "); attron(A_BOLD); - move(0, COLS-strlen(popup->title)-2); printw("[%s]", popup->title); attroff(A_BOLD); } + + /* Draw date */ + move(0, COLS-19); + printw("%04d-%02d-%02d %02d:%02d:%02d", + NOW.year, NOW.month, NOW.day, + NOW.hour, NOW.min, NOW.sec); + attroff(COLOR_PAIR(COLOR_TITLE)); if (!COMPACT) mvhline(1, 0, ACS_HLINE, COLS); @@ -320,6 +331,9 @@ void view_draw(void) /* View run */ int view_run(int key, mmask_t btn, int row, int col) { + /* Refresh timestamp */ + draw_header(); + /* Check for mouse events on the menu */ if (key == KEY_MOUSE && row == 0) { int start = 1; -- 2.43.2