From 17aead45edab3c210cff7c3b9679ff23a94dabcc Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 11 Oct 2013 01:11:43 -0400 Subject: [PATCH] put repeated code for printing time into a function --- src/chat.c | 24 ++++-------------------- src/groupchat.c | 10 ++-------- src/misc_tools.c | 10 ++++++++++ src/misc_tools.h | 3 +++ 4 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/chat.c b/src/chat.c index 068e4ec..827047b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -24,14 +24,11 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 return; ChatContext *ctx = (ChatContext *) self->chatwin; - struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, num, nick); - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + print_time(ctx->history); wattron(ctx->history, COLOR_PAIR(4)); wprintw(ctx->history, "%s: ", nick); wattroff(ctx->history, COLOR_PAIR(4)); @@ -62,15 +59,11 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin return; ChatContext *ctx = (ChatContext *) self->chatwin; - struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getname(m, num, nick); - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - + print_time(ctx->history); wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); @@ -202,20 +195,15 @@ static void print_chat_help(ChatContext *ctx) } static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) { - struct tm *timeinfo = get_time(); - if (action == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); - uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); + print_time(ctx->history); wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", selfname, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); @@ -232,8 +220,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) ChatContext *ctx = (ChatContext *) self->chatwin; StatusBar *statusbar = (StatusBar *) self->stb; - struct tm *timeinfo = get_time(); - int x, y, y2, x2; getyx(self->window, y, x); getmaxyx(self->window, y2, x2); @@ -287,9 +273,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + print_time(ctx->history); wattron(ctx->history, COLOR_PAIR(GREEN)); wprintw(ctx->history, "%s: ", selfname); wattroff(ctx->history, COLOR_PAIR(GREEN)); diff --git a/src/groupchat.c b/src/groupchat.c index 93f8bf3..a53ce25 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -100,15 +100,12 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int return; ChatContext *ctx = (ChatContext *) self->chatwin; - struct tm *timeinfo = get_time(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_group_peername(m, groupnum, peernum, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */ - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + print_time(ctx->history); wattron(ctx->history, COLOR_PAIR(4)); wprintw(ctx->history, "%s: ", nick); wattroff(ctx->history, COLOR_PAIR(4)); @@ -126,7 +123,6 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->chatwin; - struct tm *timeinfo = get_time(); int x, y, y2, x2; getyx(self->window, y, x); @@ -181,9 +177,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) // uint8_t selfname[TOX_MAX_NAME_LENGTH]; // tox_getselfname(m, selfname, TOX_MAX_NAME_LENGTH); - // wattron(ctx->history, COLOR_PAIR(CYAN)); - // wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); - // wattroff(ctx->history, COLOR_PAIR(CYAN)); + // print_time(ctx->history); // wattron(ctx->history, COLOR_PAIR(GREEN)); // wprintw(ctx->history, "%s: ", selfname); // wattroff(ctx->history, COLOR_PAIR(GREEN)); diff --git a/src/misc_tools.c b/src/misc_tools.c index ff807d1..925ff50 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -95,3 +95,13 @@ char *wc_to_char(wchar_t ch) return ret; } + +/* Prints the time to given window */ +void print_time(WINDOW *window) +{ + struct tm *timeinfo = get_time(); + + wattron(window, COLOR_PAIR(CYAN)); + wprintw(window, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(window, COLOR_PAIR(CYAN)); +} \ No newline at end of file diff --git a/src/misc_tools.h b/src/misc_tools.h index 52ca967..6256beb 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -16,3 +16,6 @@ uint8_t *wcs_to_char(wchar_t *string); /* convert a wide char to null terminated string */ char *wc_to_char(wchar_t ch); + +/* Prints the time to window w */ +void print_time(WINDOW *window);