From 74e6dd22ff845c5388ecf7d2713f7bbc5d900702 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Thu, 6 Oct 2016 07:21:36 +0200 Subject: Factor out stupid column alignment loops --- nncmpp.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/nncmpp.c b/nncmpp.c index 38d8602..2d5a845 100644 --- a/nncmpp.c +++ b/nncmpp.c @@ -645,6 +645,21 @@ row_buffer_pop_cells (struct row_buffer *self, int space) return made; } +static void +row_buffer_space (struct row_buffer *self, int width, chtype attrs) +{ + if (width < 0) + return; + + while (self->chars_len + width >= self->chars_alloc) + self->chars = xreallocarray (self->chars, + sizeof *self->chars, (self->chars_alloc <<= 1)); + + struct row_char space = { .attrs = attrs, .c = ' ', .width = 1 }; + while (width-- > 0) + self->chars[self->chars_len++] = space; +} + static void row_buffer_ellipsis (struct row_buffer *self, int target, chtype attrs) { @@ -726,10 +741,9 @@ app_write_line (const char *str, chtype attrs) if (buf.total_width > COLS) row_buffer_ellipsis (&buf, COLS, attrs); + row_buffer_space (&buf, COLS - buf.total_width, attrs); row_buffer_flush (&buf); - for (int i = buf.total_width; i < COLS; i++) - addch (' ' | attrs); row_buffer_free (&buf); } @@ -838,12 +852,10 @@ app_write_gauge (struct row_buffer *buf, float ratio, int width) len_left += remainder >= (int) 4; int len_right = width - len_left; - while (len_left-- > 0) - row_buffer_append (buf, " ", APP_ATTR (ELAPSED)); + row_buffer_space (buf, len_left, APP_ATTR (ELAPSED)); if (partial && len_right-- > 0) row_buffer_append (buf, partial, APP_ATTR (REMAINS)); - while (len_right-- > 0) - row_buffer_append (buf, " ", APP_ATTR (REMAINS)); + row_buffer_space (buf, len_right, APP_ATTR (REMAINS)); } static void @@ -906,8 +918,8 @@ app_draw_status (void) app_write_gauge (&buf, (float) g_ctx.song_elapsed / g_ctx.song_duration, remaining); } - else while (remaining-- > 0) - row_buffer_append (&buf, " ", a_normal); + else + row_buffer_space (&buf, remaining, a_normal); if (volume) { @@ -1577,8 +1589,7 @@ info_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width) row_buffer_addv (buffer, g_info_tab.keys.vector[item_index], A_BOLD, ":", A_BOLD, NULL); - while (buffer->total_width < 8) - row_buffer_append (buffer, " ", 0); + row_buffer_space (buffer, 8 - buffer->total_width, 0); row_buffer_append (buffer, g_info_tab.values.vector[item_index], 0); } @@ -1687,8 +1698,7 @@ debug_tab_on_item_draw (size_t item_index, struct row_buffer *buffer, int width) // We override the formatting including colors -- do it for the whole line if (buffer->total_width > width) row_buffer_ellipsis (buffer, width, item->attrs); - while (buffer->total_width < width) - row_buffer_append (buffer, " ", item->attrs); + row_buffer_space (buffer, width - buffer->total_width, item->attrs); } static void -- cgit v1.2.3-70-g09d2