degesch: shorten/dedup init_colors()

This commit is contained in:
Přemysl Eric Janouch 2015-04-21 20:52:41 +02:00
parent c69c8048b4
commit 69582c9e14

View File

@ -722,31 +722,18 @@ log_message_attributed (void *user_data, const char *quote, const char *fmt,
static void static void
init_colors (struct app_context *ctx) init_colors (struct app_context *ctx)
{ {
bool have_ti = init_terminal ();
// Use escape sequences from terminfo if possible, and SGR as a fallback // Use escape sequences from terminfo if possible, and SGR as a fallback
if (init_terminal ()) #define INIT_ATTR(id, ti, vt100) \
{ str_map_set (&ctx->config, (id), xstrdup (have_ti ? (ti) : (vt100)));
const char *attrs[][2] =
{ INIT_ATTR (ATTR_PROMPT, enter_bold_mode, "\x1b[1m");
{ ATTR_PROMPT, enter_bold_mode }, INIT_ATTR (ATTR_RESET, exit_attribute_mode, "\x1b[0m");
{ ATTR_RESET, exit_attribute_mode }, INIT_ATTR (ATTR_WARNING, g_terminal.color_set[3], "\x1b[33m");
{ ATTR_WARNING, g_terminal.color_set[3] }, INIT_ATTR (ATTR_ERROR, g_terminal.color_set[1], "\x1b[31m");
{ ATTR_ERROR, g_terminal.color_set[1] },
}; #undef INIT_ATTR
for (size_t i = 0; i < N_ELEMENTS (attrs); i++)
str_map_set (&ctx->config, attrs[i][0], xstrdup (attrs[i][1]));
}
else
{
const char *attrs[][2] =
{
{ ATTR_PROMPT, "\x1b[1m" },
{ ATTR_RESET, "\x1b[0m" },
{ ATTR_WARNING, "\x1b[33m" },
{ ATTR_ERROR, "\x1b[31m" },
};
for (size_t i = 0; i < N_ELEMENTS (attrs); i++)
str_map_set (&ctx->config, attrs[i][0], xstrdup (attrs[i][1]));
}
switch (ctx->color_mode) switch (ctx->color_mode)
{ {