Forced update listing format after swap
[pantumic.git] / lib / tty / color-internal.c
blobf4478aed67234d00d62eeabbe956e81c6bd49cd4
1 /* Internal stuff of color setup
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009
7 Slava Zanko <slavazanko@gmail.com>, 2009
8 Egmont Koblinger <egmont@gmail.com>, 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 /** \file color-internal.c
25 * \brief Source: Internal stuff of color setup
28 #include <config.h>
30 #include <string.h> /* strcmp */
32 #include "color.h" /* colors and attributes */
33 #include "color-internal.h"
35 /*** global variables ****************************************************************************/
37 gboolean mc_tty_color_disable;
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 typedef struct mc_tty_color_table_struct
45 const char *name;
46 int value;
47 } mc_tty_color_table_t;
49 /*** file scope variables ************************************************************************/
51 mc_tty_color_table_t const color_table[] = {
52 {"black", COLOR_BLACK},
53 {"gray", COLOR_BLACK + 8},
54 {"red", COLOR_RED},
55 {"brightred", COLOR_RED + 8},
56 {"green", COLOR_GREEN},
57 {"brightgreen", COLOR_GREEN + 8},
58 {"brown", COLOR_YELLOW},
59 {"yellow", COLOR_YELLOW + 8},
60 {"blue", COLOR_BLUE},
61 {"brightblue", COLOR_BLUE + 8},
62 {"magenta", COLOR_MAGENTA},
63 {"brightmagenta", COLOR_MAGENTA + 8},
64 {"cyan", COLOR_CYAN},
65 {"brightcyan", COLOR_CYAN + 8},
66 {"lightgray", COLOR_WHITE},
67 {"white", COLOR_WHITE + 8},
68 {"default", -1}, /* default color of the terminal */
69 /* special colors */
70 {"A_REVERSE", SPEC_A_REVERSE},
71 {"A_BOLD", SPEC_A_BOLD},
72 {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
73 {"A_UNDERLINE", SPEC_A_UNDERLINE},
74 /* End of list */
75 {NULL, 0}
78 mc_tty_color_table_t const attributes_table[] = {
79 {"bold", A_BOLD},
80 {"underline", A_UNDERLINE},
81 {"reverse", A_REVERSE},
82 {"blink", A_BLINK},
83 /* End of list */
84 {NULL, 0}
87 /*** file scope functions ************************************************************************/
88 /* --------------------------------------------------------------------------------------------- */
90 static int
91 parse_256_color_name (const char *color_name)
93 int i;
94 char dummy;
95 if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
97 return i;
99 if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
101 return 232 + i;
103 if (strncmp (color_name, "rgb", 3) == 0 &&
104 color_name[3] >= '0' && color_name[3] < '6' &&
105 color_name[4] >= '0' && color_name[4] < '6' &&
106 color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
108 return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
110 return -1;
113 /* --------------------------------------------------------------------------------------------- */
114 /*** public functions ****************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
117 const char *
118 tty_color_get_name_by_index (int idx)
120 static char **color_N_names = NULL;
121 int i;
123 /* Find the real English name of the first 16 colors, */
124 /* as well as the A_* special values. */
125 for (i = 0; color_table[i].name != NULL; i++)
126 if (idx == color_table[i].value)
127 return color_table[i].name;
128 /* Create and return the strings "color16" to "color255". */
129 if (idx >= 16 && idx < 256)
131 if (color_N_names == NULL)
133 color_N_names = g_try_malloc0 (240 * sizeof (char *));
135 if (color_N_names[idx - 16] == NULL)
137 color_N_names[idx - 16] = g_try_malloc (9);
138 sprintf (color_N_names[idx - 16], "color%d", idx);
140 return color_N_names[idx - 16];
142 return "default";
145 /* --------------------------------------------------------------------------------------------- */
148 tty_color_get_index_by_name (const char *color_name)
151 if (color_name != NULL)
153 size_t i;
154 for (i = 0; color_table[i].name != NULL; i++)
155 if (strcmp (color_name, color_table[i].name) == 0)
156 return color_table[i].value;
157 return parse_256_color_name (color_name);
159 return -1;
162 /* --------------------------------------------------------------------------------------------- */
165 tty_attr_get_bits (const char *attrs)
167 int attr_bits = 0;
168 gchar **attr_list;
169 int i, j;
171 if (attrs != NULL)
173 attr_list = g_strsplit (attrs, "+", -1);
174 for (i = 0; attr_list[i] != NULL; i++)
176 for (j = 0; attributes_table[j].name != NULL; j++)
178 if (strcmp (attr_list[i], attributes_table[j].name) == 0)
180 attr_bits |= attributes_table[j].value;
181 break;
185 g_strfreev (attr_list);
187 return attr_bits;
190 /* --------------------------------------------------------------------------------------------- */