libsmartcols: don't link with tinfo

Let's move color names to sequence translation to separate file to
make it usable without all the stuff in lib/colors.c.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2015-07-24 12:57:46 +02:00
parent 5404421866
commit 0bef6f759b
8 changed files with 98 additions and 71 deletions

View File

@ -9,6 +9,7 @@ dist_noinst_HEADERS += \
include/c.h \
include/closestream.h \
include/colors.h \
include/color-names.h \
include/cpuset.h \
include/crc32.h \
include/crc64.h \

40
include/color-names.h Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2012-2015 Karel Zak <kzak@redhat.com>
*
* This file may be distributed under the terms of the
* GNU Lesser General Public License.
*/
#ifndef UTIL_LINUX_COLOR_NAMES_H
#define UTIL_LINUX_COLOR_NAMES_H
#define UL_COLOR_RESET "\033[0m"
#define UL_COLOR_BOLD "\033[1m"
#define UL_COLOR_HALFBRIGHT "\033[2m"
#define UL_COLOR_UNDERSCORE "\033[4m"
#define UL_COLOR_BLINK "\033[5m"
#define UL_COLOR_REVERSE "\033[7m"
/* Standard colors */
#define UL_COLOR_BLACK "\033[30m"
#define UL_COLOR_RED "\033[31m"
#define UL_COLOR_GREEN "\033[32m"
#define UL_COLOR_BROWN "\033[33m" /* well, brown */
#define UL_COLOR_BLUE "\033[34m"
#define UL_COLOR_MAGENTA "\033[35m"
#define UL_COLOR_CYAN "\033[36m"
#define UL_COLOR_GRAY "\033[37m"
/* Bold variants */
#define UL_COLOR_DARK_GRAY "\033[1;30m"
#define UL_COLOR_BOLD_RED "\033[1;31m"
#define UL_COLOR_BOLD_GREEN "\033[1;32m"
#define UL_COLOR_BOLD_YELLOW "\033[1;33m"
#define UL_COLOR_BOLD_BLUE "\033[1;34m"
#define UL_COLOR_BOLD_MAGENTA "\033[1;35m"
#define UL_COLOR_BOLD_CYAN "\033[1;36m"
#define UL_COLOR_WHITE "\033[1;37m"
extern const char *color_sequence_from_colorname(const char *str);
#endif /* UTIL_LINUX_COLOR_NAMES_H */

View File

@ -11,34 +11,7 @@
#include <stdio.h>
#include <unistd.h>
#define UL_COLOR_RESET "\033[0m"
#define UL_COLOR_BOLD "\033[1m"
#define UL_COLOR_HALFBRIGHT "\033[2m"
#define UL_COLOR_UNDERSCORE "\033[4m"
#define UL_COLOR_BLINK "\033[5m"
#define UL_COLOR_REVERSE "\033[7m"
/* Standard colors */
#define UL_COLOR_BLACK "\033[30m"
#define UL_COLOR_RED "\033[31m"
#define UL_COLOR_GREEN "\033[32m"
#define UL_COLOR_BROWN "\033[33m" /* well, brown */
#define UL_COLOR_BLUE "\033[34m"
#define UL_COLOR_MAGENTA "\033[35m"
#define UL_COLOR_CYAN "\033[36m"
#define UL_COLOR_GRAY "\033[37m"
/* Bold variants */
#define UL_COLOR_DARK_GRAY "\033[1;30m"
#define UL_COLOR_BOLD_RED "\033[1;31m"
#define UL_COLOR_BOLD_GREEN "\033[1;32m"
#define UL_COLOR_BOLD_YELLOW "\033[1;33m"
#define UL_COLOR_BOLD_BLUE "\033[1;34m"
#define UL_COLOR_BOLD_MAGENTA "\033[1;35m"
#define UL_COLOR_BOLD_CYAN "\033[1;36m"
#define UL_COLOR_WHITE "\033[1;37m"
#include "color-names.h"
/* --color[=WHEN] */
enum colortmode {
@ -94,8 +67,4 @@ static inline void color_disable(void)
color_fdisable(stdout);
}
/* converts "red" to UL_COLOR_RED, etc. */
extern const char *color_sequence_from_colorname(const char *str);
#endif /* UTIL_LINUX_COLORS_H */

View File

@ -10,6 +10,7 @@ libcommon_la_SOURCES = \
lib/env.c \
lib/fileutils.c \
lib/ismounted.c \
lib/color-names.c \
lib/mangle.c \
lib/match.c \
lib/mbsalign.c \
@ -44,7 +45,7 @@ endif
noinst_LTLIBRARIES += libtcolors.la
libtcolors_la_CFLAGS = $(AM_CFLAGS) $(TINFO_CFLAGS)
libtcolors_la_LIBADD = $(TINFO_LIBS)
libtcolors_la_SOURCES = lib/colors.c include/colors.h
libtcolors_la_SOURCES = lib/colors.c lib/color-names.c include/colors.h include/color-names.h
dist_man_MANS += lib/terminal-colors.d.5

52
lib/color-names.c Normal file
View File

@ -0,0 +1,52 @@
#include "c.h"
#include "color-names.h"
struct ul_color_name {
const char *name;
const char *seq;
};
/*
* qsort/bsearch buddy
*/
static int cmp_color_name(const void *a0, const void *b0)
{
struct ul_color_name *a = (struct ul_color_name *) a0,
*b = (struct ul_color_name *) b0;
return strcmp(a->name, b->name);
}
/*
* Maintains human readable color names
*/
const char *color_sequence_from_colorname(const char *str)
{
static const struct ul_color_name basic_schemes[] = {
{ "black", UL_COLOR_BLACK },
{ "blue", UL_COLOR_BLUE },
{ "brown", UL_COLOR_BROWN },
{ "cyan", UL_COLOR_CYAN },
{ "darkgray", UL_COLOR_DARK_GRAY },
{ "gray", UL_COLOR_GRAY },
{ "green", UL_COLOR_GREEN },
{ "lightblue", UL_COLOR_BOLD_BLUE },
{ "lightcyan", UL_COLOR_BOLD_CYAN },
{ "lightgray,", UL_COLOR_GRAY },
{ "lightgreen", UL_COLOR_BOLD_GREEN },
{ "lightmagenta", UL_COLOR_BOLD_MAGENTA },
{ "lightred", UL_COLOR_BOLD_RED },
{ "magenta", UL_COLOR_MAGENTA },
{ "red", UL_COLOR_RED },
{ "yellow", UL_COLOR_BOLD_YELLOW },
};
struct ul_color_name key = { .name = (char *) str }, *res;
if (!str)
return NULL;
res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
sizeof(struct ul_color_name),
cmp_color_name);
return res ? res->seq : NULL;
}

View File

@ -112,41 +112,6 @@ static int cmp_scheme_name(const void *a0, const void *b0)
return strcmp(a->name, b->name);
}
/*
* Maintains human readable color names
*/
const char *color_sequence_from_colorname(const char *str)
{
static const struct ul_color_scheme basic_schemes[] = {
{ "black", UL_COLOR_BLACK },
{ "blue", UL_COLOR_BLUE },
{ "brown", UL_COLOR_BROWN },
{ "cyan", UL_COLOR_CYAN },
{ "darkgray", UL_COLOR_DARK_GRAY },
{ "gray", UL_COLOR_GRAY },
{ "green", UL_COLOR_GREEN },
{ "lightblue", UL_COLOR_BOLD_BLUE },
{ "lightcyan", UL_COLOR_BOLD_CYAN },
{ "lightgray,", UL_COLOR_GRAY },
{ "lightgreen", UL_COLOR_BOLD_GREEN },
{ "lightmagenta", UL_COLOR_BOLD_MAGENTA },
{ "lightred", UL_COLOR_BOLD_RED },
{ "magenta", UL_COLOR_MAGENTA },
{ "red", UL_COLOR_RED },
{ "yellow", UL_COLOR_BOLD_YELLOW },
};
struct ul_color_scheme key = { .name = (char *) str }, *res;
if (!str)
return NULL;
res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
sizeof(struct ul_color_scheme),
cmp_scheme_name);
return res ? res->seq : NULL;
}
/*
* Resets control struct (note that we don't allocate the struct)
*/

View File

@ -22,7 +22,7 @@ libsmartcols_la_SOURCES= \
nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h
libsmartcols_la_LIBADD = libcommon.la libtcolors.la
libsmartcols_la_LIBADD = libcommon.la
libsmartcols_la_CFLAGS = \
$(SOLIB_CFLAGS) \
@ -31,7 +31,6 @@ libsmartcols_la_CFLAGS = \
libsmartcols_la_DEPENDENCIES = \
libcommon.la \
libtcolors.la \
libsmartcols/src/libsmartcols.sym \
libsmartcols/src/libsmartcols.h.in

View File

@ -13,7 +13,7 @@
#include "c.h"
#include "list.h"
#include "colors.h"
#include "color-names.h"
#include "debug.h"
#include "libsmartcols.h"