Further optimize terminal output

By calling addstr() once instead of several addch() calls.

As a side effect, it's becoming easier to ditch ncurses altogether.
This commit is contained in:
Přemysl Eric Janouch 2016-10-06 07:01:50 +02:00
parent ca38c1d91a
commit 54936c4bcb
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 19 additions and 17 deletions

View File

@ -671,12 +671,13 @@ row_buffer_ellipsis (struct row_buffer *self, int target, chtype attrs)
static void static void
row_buffer_print (uint32_t *ucs4, chtype attrs) row_buffer_print (uint32_t *ucs4, chtype attrs)
{ {
// Cannot afford to convert negative numbers to the unsigned chtype. // This assumes that we can reset the attribute set without consequences
uint8_t *str = (uint8_t *) u32_strconv_to_locale (ucs4); char *str = u32_strconv_to_locale (ucs4);
if (str) if (str)
{ {
for (uint8_t *p = str; *p; p++) attrset (attrs);
addch (*p | attrs); addstr (str);
attrset (0);
free (str); free (str);
} }
} }
@ -1054,26 +1055,29 @@ app_draw_view (void)
if (item_index == tab->item_selected) if (item_index == tab->item_selected)
row_attrs = APP_ATTR (SELECTION); row_attrs = APP_ATTR (SELECTION);
attrset (row_attrs);
mvwhline (stdscr, g_ctx.header_height + row, 0, ' ', COLS);
struct row_buffer buf; struct row_buffer buf;
row_buffer_init (&buf); row_buffer_init (&buf);
tab->on_item_draw (item_index, &buf, view_width); tab->on_item_draw (item_index, &buf, view_width);
if (item_index == tab->item_selected)
// Combine attributes used by the handler with the defaults.
// Avoiding attrset() because of row_buffer_flush().
for (size_t i = 0; i < buf.chars_len; i++)
{ {
// Make it so that the selection color always wins chtype *attrs = &buf.chars[i].attrs;
for (size_t i = 0; i < buf.chars_len; i++) if (item_index == tab->item_selected)
buf.chars[i].attrs &= ~(A_COLOR | A_REVERSE); *attrs = (*attrs & ~(A_COLOR | A_REVERSE)) | row_attrs;
else if ((*attrs & A_COLOR) && (row_attrs & A_COLOR))
*attrs |= (row_attrs & ~A_COLOR);
else
*attrs |= row_attrs;
} }
if (buf.total_width > view_width) if (buf.total_width > view_width)
row_buffer_ellipsis (&buf, view_width, row_attrs); row_buffer_ellipsis (&buf, view_width, row_attrs);
mvwhline (stdscr, g_ctx.header_height + row, 0, ' ' | row_attrs, COLS);
row_buffer_flush (&buf); row_buffer_flush (&buf);
row_buffer_free (&buf); row_buffer_free (&buf);
} }
attrset (0);
if (want_scrollbar) if (want_scrollbar)
app_draw_scrollbar (); app_draw_scrollbar ();
@ -2152,11 +2156,9 @@ app_log_handler (void *user_data, const char *quote, const char *fmt,
} }
else else
{ {
// TODO: remember the position and attributes and restore them // TODO: remember the position and restore it
attrset (A_REVERSE);
mvwhline (stdscr, LINES - 1, 0, A_REVERSE, COLS); mvwhline (stdscr, LINES - 1, 0, A_REVERSE, COLS);
app_write_line (message.str, 0); app_write_line (message.str, A_REVERSE);
attrset (0);
} }
str_free (&message); str_free (&message);