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
1 changed files with 11 additions and 24 deletions

View File

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