Simplify some terminal output

This commit is contained in:
Přemysl Eric Janouch 2016-10-04 22:21:30 +02:00
parent fec9f83cbc
commit 07e3aafd84
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 28 additions and 40 deletions

View File

@ -605,6 +605,7 @@ row_buffer_pop_cells (struct row_buffer *self, int space)
static void static void
row_buffer_ellipsis (struct row_buffer *self, int target, chtype attrs) row_buffer_ellipsis (struct row_buffer *self, int target, chtype attrs)
{ {
// TODO: get "attrs" from the last eaten item
row_buffer_pop_cells (self, self->total_width - target); row_buffer_pop_cells (self, self->total_width - target);
ucs4_t ellipsis = L''; ucs4_t ellipsis = L'';
@ -662,31 +663,21 @@ row_buffer_flush (struct row_buffer *self)
// --- Rendering --------------------------------------------------------------- // --- Rendering ---------------------------------------------------------------
/// Write the given UTF-8 string padded with spaces. /// Write the given UTF-8 string padded with spaces.
/// @param[in] n The number of characters to write, or -1 for the whole string. /// @param[in] attrs Text attributes for the text, including padding.
/// @param[in] attrs Text attributes for the text, without padding. static void
/// To change the attributes of all output, use attrset(). app_write_line (const char *str, chtype attrs)
/// @return The number of characters output.
static size_t
app_write_utf8 (const char *str, chtype attrs, int n)
{ {
if (!n)
return 0;
struct row_buffer buf; struct row_buffer buf;
row_buffer_init (&buf); row_buffer_init (&buf);
row_buffer_append (&buf, str, attrs); row_buffer_append (&buf, str, attrs);
if (n < 0) if (buf.total_width > COLS)
n = buf.total_width; row_buffer_ellipsis (&buf, COLS, attrs);
if (buf.total_width > n)
row_buffer_ellipsis (&buf, n, attrs);
row_buffer_flush (&buf); row_buffer_flush (&buf);
for (int i = buf.total_width; i < n; i++) for (int i = buf.total_width; i < COLS; i++)
addch (' '); addch (' ' | attrs);
row_buffer_free (&buf); row_buffer_free (&buf);
return n;
} }
/// Clear a row in the header to be used and increment the listview offset /// Clear a row in the header to be used and increment the listview offset
@ -884,40 +875,38 @@ app_redraw_top (void)
g_ctx.gauge_offset = -1; g_ctx.gauge_offset = -1;
g_ctx.gauge_width = 0; g_ctx.gauge_width = 0;
attrset (0);
switch (g_ctx.client.state) switch (g_ctx.client.state)
{ {
case MPD_CONNECTED: case MPD_CONNECTED:
app_redraw_status (); app_redraw_status ();
break; break;
case MPD_CONNECTING: case MPD_CONNECTING:
attrset (APP_ATTR (HEADER)); app_next_row (APP_ATTR (HEADER));
app_next_row (0); app_write_line ("Connecting to MPD...", APP_ATTR (HEADER));
app_write_utf8 ("Connecting to MPD...", APP_ATTR (HEADER), COLS);
break; break;
case MPD_DISCONNECTED: case MPD_DISCONNECTED:
attrset (APP_ATTR (HEADER)); app_next_row (APP_ATTR (HEADER));
app_next_row (0); app_write_line ("Disconnected", APP_ATTR (HEADER));
app_write_utf8 ("Disconnected", APP_ATTR (HEADER), COLS);
} }
attrset (APP_ATTR (TAB_BAR)); // XXX: can we get rid of this and still make it look acceptable?
app_next_row (0); chtype a_normal = APP_ATTR (TAB_BAR);
chtype a_active = APP_ATTR (TAB_ACTIVE);
struct row_buffer buf;
row_buffer_init (&buf);
// The help tab is disguised so that it's not too intruding // The help tab is disguised so that it's not too intruding
size_t indent = app_write_utf8 (APP_TITLE, row_buffer_append (&buf, APP_TITLE,
g_ctx.active_tab == g_ctx.help_tab ? APP_ATTR (TAB_ACTIVE) : 0, -1); g_ctx.active_tab == g_ctx.help_tab ? a_active : a_normal);
row_buffer_append (&buf, " ", a_normal);
addch (' '); LIST_FOR_EACH (struct tab, iter, g_ctx.tabs)
indent++;
attrset (0);
LIST_FOR_EACH (struct tab, it, g_ctx.tabs)
{ {
indent += app_write_utf8 (it->name, row_buffer_append (&buf, iter->name,
it == g_ctx.active_tab ? APP_ATTR (TAB_ACTIVE) : APP_ATTR (TAB_BAR), iter == g_ctx.active_tab ? a_active : a_normal);
MIN (COLS - indent, it->name_width));
} }
app_flush_buffer (&buf, a_normal);
refresh (); refresh ();
} }
@ -1015,7 +1004,7 @@ app_redraw_view (void)
row_attrs = APP_ATTR (SELECTION); row_attrs = APP_ATTR (SELECTION);
attrset (row_attrs); attrset (row_attrs);
move (g_ctx.top_height + row, 0); mvwhline (stdscr, g_ctx.top_height + row, 0, ' ', COLS);
struct row_buffer buf; struct row_buffer buf;
row_buffer_init (&buf); row_buffer_init (&buf);
@ -1031,8 +1020,6 @@ app_redraw_view (void)
row_buffer_ellipsis (&buf, view_width, row_attrs); row_buffer_ellipsis (&buf, view_width, row_attrs);
row_buffer_flush (&buf); row_buffer_flush (&buf);
for (int i = buf.total_width; i < view_width; i++)
addch (' ');
row_buffer_free (&buf); row_buffer_free (&buf);
} }
attrset (0); attrset (0);
@ -2047,7 +2034,8 @@ app_log_handler (void *user_data, const char *quote, const char *fmt,
// TODO: remember the position and attributes and restore them // TODO: remember the position and attributes and restore them
attrset (A_REVERSE); attrset (A_REVERSE);
mvwhline (stdscr, LINES - 1, 0, A_REVERSE, COLS); mvwhline (stdscr, LINES - 1, 0, A_REVERSE, COLS);
app_write_utf8 (message.str, 0, COLS); app_write_line (message.str, 0);
attrset (0);
} }
str_free (&message); str_free (&message);