From 01117585025befdce6edd8c67b3fad295d171623 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 5 Jul 2009 10:53:06 +0200 Subject: [PATCH 1/1] gui: Avoid unnecessary strdup's and replace newlines by spaces in curses output. align_str() made a copy of the given buffer because one of its callers, print_in_bar(), passes a string literal and align_str() needs to truncate large strings.However, all other callers pass a writable string anyway, so do the strdup() in print_in_bar(). This avoids the unnecessary copies for each status item. The areas for the status items was designed to hold only a part of a single line, so status item text should not contain newlines. The patch replaces newlines by spaces. Also get rid of a stale comment. --- gui.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/gui.c b/gui.c index 21770ca8..e734b10c 100644 --- a/gui.c +++ b/gui.c @@ -294,20 +294,23 @@ static void add_spaces(WINDOW* win, unsigned int num) * print aligned string to curses window. This function always prints * exactly len chars. */ -static int align_str(WINDOW* win, const char *string, unsigned int len, +static int align_str(WINDOW* win, char *str, unsigned int len, unsigned int align) { - int num; /* of spaces */ - char *str; + int i, num; /* of spaces */ - if (!win || !string) + if (!win || !str) return -1; - num = len - strlen(string); - str = para_strdup(string); + num = len - strlen(str); if (num < 0) { str[len] = '\0'; num = 0; } + /* replace newlines by spaces */ + for (i = 0; i < len; i++) { + if (str[i] == '\n') + str[i] = ' '; + } if (align == LEFT) { waddstr(win, str); add_spaces(win, num); @@ -319,7 +322,6 @@ static int align_str(WINDOW* win, const char *string, unsigned int len, waddstr(win, str[0]? str: ""); add_spaces(win, num - num / 2); } - free(str); return 1; } @@ -342,10 +344,14 @@ __printf_2_3 static void print_in_bar(int color, const char *fmt,...) */ static void print_status_bar(void) { + char *tmp; + if (!curses_active) return; + tmp = para_strdup(STANDARD_STATUS_BAR); wmove(sb.win, 0, 0); - align_str(sb.win, STANDARD_STATUS_BAR, sb.cols, CENTER); + align_str(sb.win, tmp, sb.cols, CENTER); + free(tmp); wrefresh(sb.win); } @@ -669,9 +675,6 @@ static void print_stat_item(int i) wrefresh(top.win); } -/* - * print status line if line starts with known command. - */ static int update_item(int item_num, char *buf) { free(stat_content[item_num]); -- 2.30.2