degesch: simplify formatter_flush()

This commit is contained in:
Přemysl Eric Janouch 2015-04-28 17:25:50 +02:00
parent 4013921de7
commit 1ff56bfe5a
1 changed files with 13 additions and 21 deletions

View File

@ -1109,32 +1109,30 @@ static void
formatter_flush (struct formatter *self, FILE *stream) formatter_flush (struct formatter *self, FILE *stream)
{ {
terminal_printer_fn printer = get_attribute_printer (stream); terminal_printer_fn printer = get_attribute_printer (stream);
if (!printer)
{
LIST_FOR_EACH (struct formatter_item, iter, self->items)
if (iter->type == FORMATTER_ITEM_TEXT)
fputs (iter->data, stream);
return;
}
const char *attr_reset = str_map_find (&self->ctx->config, ATTR_RESET); const char *attr_reset = str_map_find (&self->ctx->config, ATTR_RESET);
if (printer) tputs (attr_reset, 1, printer);
tputs (attr_reset, 1, printer);
bool is_attributed = false; bool is_attributed = false;
bool is_tty = isatty (fileno (stream));
LIST_FOR_EACH (struct formatter_item, iter, self->items) LIST_FOR_EACH (struct formatter_item, iter, self->items)
{ {
switch (iter->type) switch (iter->type)
{ {
char *term;
case FORMATTER_ITEM_TEXT: case FORMATTER_ITEM_TEXT:
if (is_tty) term = iconv_xstrdup
{ (self->ctx->term_from_utf8, iter->data, -1, NULL);
char *term = iconv_xstrdup fputs (term, stream);
(self->ctx->term_from_utf8, iter->data, -1, NULL); free (term);
fputs (term, stream);
free (term);
}
else
fputs (iter->data, stream);
break; break;
case FORMATTER_ITEM_ATTR: case FORMATTER_ITEM_ATTR:
if (!printer)
continue;
if (is_attributed) if (is_attributed)
{ {
tputs (attr_reset, 1, printer); tputs (attr_reset, 1, printer);
@ -1147,16 +1145,10 @@ formatter_flush (struct formatter *self, FILE *stream)
} }
break; break;
case FORMATTER_ITEM_FG_COLOR: case FORMATTER_ITEM_FG_COLOR:
if (!printer)
continue;
tputs (g_terminal.color_set_fg[iter->color], 1, printer); tputs (g_terminal.color_set_fg[iter->color], 1, printer);
is_attributed = true; is_attributed = true;
break; break;
case FORMATTER_ITEM_BG_COLOR: case FORMATTER_ITEM_BG_COLOR:
if (!printer)
continue;
tputs (g_terminal.color_set_bg[iter->color], 1, printer); tputs (g_terminal.color_set_bg[iter->color], 1, printer);
is_attributed = true; is_attributed = true;
break; break;