|
@@ -1,10 +1,11 @@
|
|
|
/* Internal stuff of color setup
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
- 2007, 2008, 2009 Free Software Foundation, Inc.
|
|
|
+ 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
|
|
|
|
|
Written by:
|
|
|
- Andrew Borodin <aborodin@vmail.ru>, 2009.
|
|
|
- Slava Zanko <slavazanko@gmail.com>, 2009.
|
|
|
+ Andrew Borodin <aborodin@vmail.ru>, 2009
|
|
|
+ Slava Zanko <slavazanko@gmail.com>, 2009
|
|
|
+ Egmont Koblinger <egmont@gmail.com>, 2010
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
it under the terms of the GNU General Public License as published by
|
|
@@ -49,21 +50,21 @@ typedef struct mc_tty_color_table_struct
|
|
|
|
|
|
mc_tty_color_table_t const color_table[] = {
|
|
|
{"black", COLOR_BLACK},
|
|
|
- {"gray", COLOR_BLACK | A_BOLD},
|
|
|
+ {"gray", COLOR_BLACK + 8},
|
|
|
{"red", COLOR_RED},
|
|
|
- {"brightred", COLOR_RED | A_BOLD},
|
|
|
+ {"brightred", COLOR_RED + 8},
|
|
|
{"green", COLOR_GREEN},
|
|
|
- {"brightgreen", COLOR_GREEN | A_BOLD},
|
|
|
+ {"brightgreen", COLOR_GREEN + 8},
|
|
|
{"brown", COLOR_YELLOW},
|
|
|
- {"yellow", COLOR_YELLOW | A_BOLD},
|
|
|
+ {"yellow", COLOR_YELLOW + 8},
|
|
|
{"blue", COLOR_BLUE},
|
|
|
- {"brightblue", COLOR_BLUE | A_BOLD},
|
|
|
+ {"brightblue", COLOR_BLUE + 8},
|
|
|
{"magenta", COLOR_MAGENTA},
|
|
|
- {"brightmagenta", COLOR_MAGENTA | A_BOLD},
|
|
|
+ {"brightmagenta", COLOR_MAGENTA + 8},
|
|
|
{"cyan", COLOR_CYAN},
|
|
|
- {"brightcyan", COLOR_CYAN | A_BOLD},
|
|
|
+ {"brightcyan", COLOR_CYAN + 8},
|
|
|
{"lightgray", COLOR_WHITE},
|
|
|
- {"white", COLOR_WHITE | A_BOLD},
|
|
|
+ {"white", COLOR_WHITE + 8},
|
|
|
{"default", -1}, /* default color of the terminal */
|
|
|
/* special colors */
|
|
|
{"A_REVERSE", SPEC_A_REVERSE},
|
|
@@ -74,24 +75,71 @@ mc_tty_color_table_t const color_table[] = {
|
|
|
{NULL, 0}
|
|
|
};
|
|
|
|
|
|
+mc_tty_color_table_t const attributes_table[] = {
|
|
|
+ {"bold", A_BOLD},
|
|
|
+ {"underline", A_UNDERLINE},
|
|
|
+ {"reverse", A_REVERSE},
|
|
|
+ {"blink", A_BLINK},
|
|
|
+ /* End of list */
|
|
|
+ {NULL, 0}
|
|
|
+};
|
|
|
+
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
+static int
|
|
|
+parse_256_color_name (const char *color_name)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ char dummy;
|
|
|
+ if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
|
|
|
+ {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
|
|
|
+ {
|
|
|
+ return 232 + i;
|
|
|
+ }
|
|
|
+ if (strncmp (color_name, "rgb", 3) == 0 &&
|
|
|
+ color_name[3] >= '0' && color_name[3] < '6' &&
|
|
|
+ color_name[4] >= '0' && color_name[4] < '6' &&
|
|
|
+ color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
|
|
|
+ {
|
|
|
+ return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+/* --------------------------------------------------------------------------------------------- */
|
|
|
/*** public functions ****************************************************************************/
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
const char *
|
|
|
-tty_color_get_valid_name (const char *color_name)
|
|
|
+tty_color_get_name_by_index (int idx)
|
|
|
{
|
|
|
-
|
|
|
- if (color_name != NULL)
|
|
|
+ static char **color_N_names = NULL;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ /* Find the real English name of the first 16 colors, */
|
|
|
+ /* as well as the A_* special values. */
|
|
|
+ for (i = 0; color_table[i].name != NULL; i++)
|
|
|
+ if (idx == color_table[i].value)
|
|
|
+ return color_table[i].name;
|
|
|
+ /* Create and return the strings "color16" to "color255". */
|
|
|
+ if (idx >= 16 && idx < 256)
|
|
|
{
|
|
|
- size_t i;
|
|
|
- for (i = 0; color_table[i].name != NULL; i++)
|
|
|
- if (strcmp (color_name, color_table[i].name) == 0)
|
|
|
- return color_table[i].name;
|
|
|
+ if (color_N_names == NULL)
|
|
|
+ {
|
|
|
+ color_N_names = g_try_malloc0 (240 * sizeof (char *));
|
|
|
+ }
|
|
|
+ if (color_N_names[idx - 16] == NULL)
|
|
|
+ {
|
|
|
+ color_N_names[idx - 16] = g_try_malloc (9);
|
|
|
+ sprintf (color_N_names[idx - 16], "color%d", idx);
|
|
|
+ }
|
|
|
+ return color_N_names[idx - 16];
|
|
|
}
|
|
|
- return NULL;
|
|
|
+ return "default";
|
|
|
}
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
@@ -106,8 +154,37 @@ tty_color_get_index_by_name (const char *color_name)
|
|
|
for (i = 0; color_table[i].name != NULL; i++)
|
|
|
if (strcmp (color_name, color_table[i].name) == 0)
|
|
|
return color_table[i].value;
|
|
|
+ return parse_256_color_name (color_name);
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
+
|
|
|
+int
|
|
|
+tty_attr_get_bits (const char *attrs)
|
|
|
+{
|
|
|
+ int attr_bits = 0;
|
|
|
+ gchar **attr_list;
|
|
|
+ int i, j;
|
|
|
+
|
|
|
+ if (attrs != NULL)
|
|
|
+ {
|
|
|
+ attr_list = g_strsplit (attrs, "+", -1);
|
|
|
+ for (i = 0; attr_list[i] != NULL; i++)
|
|
|
+ {
|
|
|
+ for (j = 0; attributes_table[j].name != NULL; j++)
|
|
|
+ {
|
|
|
+ if (strcmp (attr_list[i], attributes_table[j].name) == 0)
|
|
|
+ {
|
|
|
+ attr_bits |= attributes_table[j].value;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ g_strfreev (attr_list);
|
|
|
+ }
|
|
|
+ return attr_bits;
|
|
|
+}
|
|
|
+
|
|
|
+/* --------------------------------------------------------------------------------------------- */
|