1998-02-27 07:54:42 +03:00
|
|
|
/* Color setup
|
2007-09-26 14:22:25 +04:00
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2009-05-10 19:01:15 +04:00
|
|
|
2007, 2008, 2009 Free Software Foundation, Inc.
|
2009-06-12 14:55:06 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009.
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2009-05-10 19:01:15 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-05 21:28:18 +03:00
|
|
|
/** \file color.c
|
|
|
|
* \brief Source: color setup
|
|
|
|
*/
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <stdio.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
#include <stdlib.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <string.h>
|
2009-05-10 19:01:15 +04:00
|
|
|
#include <sys/types.h> /* size_t */
|
2005-02-08 12:04:03 +03:00
|
|
|
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "../../src/global.h"
|
|
|
|
|
|
|
|
#include "../../src/tty/tty.h"
|
|
|
|
#include "../../src/tty/color.h"
|
2009-05-10 19:01:15 +04:00
|
|
|
#include "../../src/tty/color-internal.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
#include "../../src/setup.h" /* setup_color_string, term_color_string */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-06-04 23:55:47 +04:00
|
|
|
extern char *command_line_colors;
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Set if we are actually using colors */
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean use_colors = FALSE;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-06-04 23:55:47 +04:00
|
|
|
struct colors_avail c;
|
|
|
|
int max_index = 0;
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
|
|
|
get_color (const char *cpp, CTYPE *colp)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2009-05-10 19:01:15 +04:00
|
|
|
const size_t table_len = color_table_len ();
|
2004-08-16 07:12:05 +04:00
|
|
|
size_t i;
|
2009-05-10 19:01:15 +04:00
|
|
|
|
|
|
|
for (i = 0; i < table_len; i++)
|
|
|
|
if (strcmp (cpp, color_name (i)) == 0) {
|
1998-02-27 07:54:42 +03:00
|
|
|
*colp = color_value (i);
|
2009-05-10 19:01:15 +04:00
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
static void
|
|
|
|
configure_colors_string (const char *the_color_string)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2009-05-10 19:01:15 +04:00
|
|
|
const size_t map_len = color_map_len ();
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2004-08-16 07:12:05 +04:00
|
|
|
size_t i;
|
2009-06-04 23:55:47 +04:00
|
|
|
char **color_strings, **p;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (!the_color_string)
|
|
|
|
return;
|
|
|
|
|
2009-06-04 23:55:47 +04:00
|
|
|
color_strings = g_strsplit (the_color_string, ":", -1);
|
|
|
|
|
|
|
|
p = color_strings;
|
|
|
|
|
|
|
|
while ((p != NULL) && (*p != NULL)) {
|
|
|
|
char **cfb; /* color, fore, back*/
|
|
|
|
/* cfb[0] - entry name
|
|
|
|
* cfb[1] - fore color
|
|
|
|
* cfb[20 - back color
|
|
|
|
*/
|
|
|
|
char *e;
|
|
|
|
|
|
|
|
cfb = g_strsplit_set (*p, "=,", 3);
|
|
|
|
/* append '=' to the entry name */
|
|
|
|
e = g_strdup_printf ("%s=", cfb[0]);
|
|
|
|
g_free (cfb[0]);
|
|
|
|
cfb[0] = e;
|
|
|
|
|
|
|
|
for (i = 0; i < map_len; i++)
|
|
|
|
if (color_map [i].name != NULL) {
|
|
|
|
size_t klen = strlen (color_map [i].name);
|
|
|
|
|
|
|
|
if (strncmp (cfb[0], color_map [i].name, klen) == 0) {
|
|
|
|
if ((cfb[1] != NULL) && (*cfb[1] != '\0'))
|
|
|
|
get_color (cfb[1], &color_map [i].fg);
|
|
|
|
if ((cfb[2] != NULL) && (*cfb[2] != '\0'))
|
|
|
|
get_color (cfb[2], &color_map [i].bg);
|
|
|
|
break;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2009-06-04 23:55:47 +04:00
|
|
|
|
|
|
|
g_strfreev (cfb);
|
|
|
|
p++;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2009-06-04 23:55:47 +04:00
|
|
|
|
|
|
|
g_strfreev (color_strings);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
|
|
|
configure_colors (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-06-22 13:32:52 +04:00
|
|
|
configure_colors_string (default_colors);
|
1998-02-27 07:54:42 +03:00
|
|
|
configure_colors_string (setup_color_string);
|
|
|
|
configure_colors_string (term_color_string);
|
|
|
|
configure_colors_string (getenv ("MC_COLOR_TABLE"));
|
|
|
|
configure_colors_string (command_line_colors);
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
int
|
2002-07-15 01:41:12 +04:00
|
|
|
alloc_color_pair (CTYPE foreground, CTYPE background)
|
|
|
|
{
|
|
|
|
mc_init_pair (++max_index, foreground, background);
|
|
|
|
return max_index;
|
|
|
|
}
|
|
|
|
|
2002-07-25 17:04:19 +04:00
|
|
|
void
|
2009-05-10 19:01:15 +04:00
|
|
|
tty_colors_done (void)
|
2002-07-25 17:04:19 +04:00
|
|
|
{
|
|
|
|
struct colors_avail *p, *next;
|
|
|
|
|
2009-06-04 23:55:47 +04:00
|
|
|
for (p = c.next; p != NULL; p = next) {
|
2002-07-25 17:04:19 +04:00
|
|
|
next = p->next;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (p->fg);
|
|
|
|
g_free (p->bg);
|
|
|
|
g_free (p);
|
2002-07-25 17:04:19 +04:00
|
|
|
}
|
|
|
|
c.next = NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean
|
|
|
|
tty_use_colors (void)
|
|
|
|
{
|
|
|
|
return use_colors;
|
|
|
|
}
|