Rationalise our colour usage a little.

This commit is contained in:
Roy Marples 2007-04-17 09:32:18 +00:00
parent 0f92b5e7df
commit f657d87b99
9 changed files with 113 additions and 112 deletions

View File

@ -3,7 +3,7 @@
17 Apr 2007; Roy Marples <uberlord@gentoo.org>: 17 Apr 2007; Roy Marples <uberlord@gentoo.org>:
env vars EINFO_GOOD and friends now override our hardcoded builtins. env vars ECOLOR_GOOD and friends now override our hardcoded builtins.
16 Apr 2007; Mike Frysinger <vapier@gentoo.org>: 16 Apr 2007; Mike Frysinger <vapier@gentoo.org>:

View File

@ -79,6 +79,12 @@ start() {
|| rm -f "${RC_LIBDIR}"/console/map || rm -f "${RC_LIBDIR}"/console/map
/bin/setfont -ou "${RC_LIBDIR}"/console/unimap 2>/dev/null \ /bin/setfont -ou "${RC_LIBDIR}"/console/unimap 2>/dev/null \
|| rm -f "${RC_LIBDIR}"/console/unimap || rm -f "${RC_LIBDIR}"/console/unimap
if [ "${UNICODE}" = "yes" ] ; then
touch "${RC_LIBDIR}"/console/unicode
else
rm -f "${RC_LIBDIR}"/console/unicode
fi
fi fi
return ${retval} return ${retval}

View File

@ -88,17 +88,20 @@ get_KV() {
# Try and set a font as early as we can # Try and set a font as early as we can
ttydev=${CONSOLE:-/dev/tty1} ttydev=${CONSOLE:-/dev/tty1}
if [ -c "${ttydev}" ] ; then if [ ! -c "${ttydev}" ] ; then
ttydev="-C ${ttydev}" [ -c /dev/vc/1 ] && ttydev="/dev/vc/1" || ttydev=
else
[ -c /dev/vc/1 ] && ttydev="-C /dev/vc/1" || ttydev=
fi fi
[ -r "${RC_LIBDIR}"/console/font ] \ [ -r "${RC_LIBDIR}"/console/font ] \
&& /bin/setfont ${ttydev} "${RC_LIBDIR}"/console/font && /bin/setfont ${ttydev:+-C} ${ttydev} "${RC_LIBDIR}"/console/font
[ -r "${RC_LIBDIR}"/console/map ] \ [ -r "${RC_LIBDIR}"/console/map ] \
&& /bin/setfont ${ttydev} -m "${RC_LIBDIR}"/console/map && /bin/setfont ${ttydev:+-C} ${ttydev} -m "${RC_LIBDIR}"/console/map
[ -r "${RC_LIBDIR}"/console/unimap ] \ [ -r "${RC_LIBDIR}"/console/unimap ] \
&& /bin/setfont ${ttydev} -u "${RC_LIBDIR}"/console/unimap && /bin/setfont ${ttydev:+-C} ${ttydev} -u "${RC_LIBDIR}"/console/unimap
if [ -e "${RC_LIBDIR}"/console/unicode ] ; then
eval printf "\033%%G" ${ttydev:+>} ${ttydev}
else
eval printf "\033(K" ${ttydev:+>} ${ttydev}
fi
unset ttydev unset ttydev
. /etc/init.d/functions.sh . /etc/init.d/functions.sh

View File

@ -144,14 +144,7 @@ for arg in "$@" ; do
done done
if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then
if color_terminal ; then eval $(eval_ecolors)
GOOD=${EINFO_GOOD:-$'\e[32;01m'}
WARN=${EINFO_WARN:-$'\e[33;01m'}
BAD=${EINFO_BAD:-$'\e[31;01m'}
HILITE=${EINFO_HILITE:-$'\e[36;01m'}
BRACKET=${EINFO_BRACKET:-$'\e[34;01m'}
NORMAL=$'\e[0m'
fi
fi fi
# vim: set ts=4 : # vim: set ts=4 :

View File

@ -38,7 +38,7 @@ SYS_WHITELIST = env_whitelist
TARGET = $(LIB_TARGETS) $(BIN_TARGETS) $(SBIN_TARGETS) $(PRIV_BIN_TARGETS) TARGET = $(LIB_TARGETS) $(BIN_TARGETS) $(SBIN_TARGETS) $(PRIV_BIN_TARGETS)
RCLINKS = einfon einfo ewarnn ewarn eerrorn eerror ebegin eend ewend \ RCLINKS = einfon einfo ewarnn ewarn eerrorn eerror ebegin eend ewend \
eindent eoutdent eflush color_terminal \ eindent eoutdent eflush eval_ecolors \
veinfo vewarn vebegin veend vewend veindent veoutdent \ veinfo vewarn vebegin veend vewend veindent veoutdent \
service_starting service_inactive service_started \ service_starting service_inactive service_started \
service_stopping service_stopped \ service_stopping service_stopped \

View File

@ -18,30 +18,14 @@
typedef enum typedef enum
{ {
einfo_good, ecolor_good,
einfo_warn, ecolor_warn,
einfo_bad, ecolor_bad,
einfo_hilite, ecolor_hilite,
einfo_bracket, ecolor_bracket,
einfo_normal ecolor_normal
} einfo_color_t; } einfo_color_t;
/* Colour codes used by the below functions. */
#define EINFO_GOOD "\033[32;01m"
#define EINFO_WARN "\033[33;01m"
#define EINFO_BAD "\033[31;01m"
#define EINFO_HILITE "\033[36;01m"
#define EINFO_BRACKET "\033[34;01m"
#define EINFO_NORMAL "\033[0m"
/* Handy macros to easily use the above in a custom manner */
#define PEINFO_GOOD if (colour_terminal ()) printf (EINFO_GOOD)
#define PEINFO_WARN if (colour_terminal ()) printf (EINFO_WARN)
#define PEINFO_BAD if (colour_terminal ()) printf (EINFO_BAD)
#define PEINFO_HILITE if (colour_terminal ()) printf (EINFO_HILITE)
#define PEINFO_BRACKET if (colour_terminal ()) printf (EINFO_BRACKET)
#define PEINFO_NORMAL if (colour_terminal ()) printf (EINFO_NORMAL)
/* We work out if the terminal supports colour or not through the use /* We work out if the terminal supports colour or not through the use
of the TERM env var. We cache the reslt in a static bool, so of the TERM env var. We cache the reslt in a static bool, so
subsequent calls are very fast. subsequent calls are very fast.
@ -51,7 +35,7 @@ typedef enum
in the future, but veinfo is used by shell scripts as they don't in the future, but veinfo is used by shell scripts as they don't
have the va_list concept have the va_list concept
*/ */
bool colour_terminal (void); const char *ecolor (einfo_color_t);
int einfon (const char *fmt, ...) EINFO_PRINTF (1, 2); int einfon (const char *fmt, ...) EINFO_PRINTF (1, 2);
int ewarnn (const char *fmt, ...) EINFO_PRINTF (1, 2); int ewarnn (const char *fmt, ...) EINFO_PRINTF (1, 2);
int eerrorn (const char *fmt, ...) EINFO_PRINTF (1, 2); int eerrorn (const char *fmt, ...) EINFO_PRINTF (1, 2);

View File

@ -22,7 +22,7 @@
#include "rc-misc.h" #include "rc-misc.h"
#include "hidden-visibility.h" #include "hidden-visibility.h"
hidden_proto(colour_terminal) hidden_proto(ecolor)
hidden_proto(ebegin) hidden_proto(ebegin)
hidden_proto(ebeginv) hidden_proto(ebeginv)
hidden_proto(ebracket) hidden_proto(ebracket)
@ -48,7 +48,7 @@ hidden_proto(ewarnx)
hidden_proto(ewend) hidden_proto(ewend)
hidden_proto(ewendv) hidden_proto(ewendv)
/* Incase we cannot work out how many columns from ioctl, supply a default */ /* Incase we cannot work out how many columns from ioctl, supply a default */
#define DEFAULT_COLS 80 #define DEFAULT_COLS 80
#define OK "ok" #define OK "ok"
@ -56,18 +56,27 @@ hidden_proto(ewendv)
#define CHECK_VERBOSE if (! is_env ("RC_VERBOSE", "yes")) return 0 #define CHECK_VERBOSE if (! is_env ("RC_VERBOSE", "yes")) return 0
/* Number of spaces for an indent */ /* Number of spaces for an indent */
#define INDENT_WIDTH 2 #define INDENT_WIDTH 2
/* How wide can the indent go? */ /* How wide can the indent go? */
#define INDENT_MAX 40 #define INDENT_MAX 40
#define EBUFFER_LOCK RC_SVCDIR "ebuffer/.lock" #define EBUFFER_LOCK RC_SVCDIR "ebuffer/.lock"
/* Default colours */
#define ECOLOR_GOOD "\033[32;01m"
#define ECOLOR_WARN "\033[33;01m"
#define ECOLOR_BAD "\033[31;01m"
#define ECOLOR_HILITE "\033[36;01m"
#define ECOLOR_BRACKET "\033[34;01m"
#define ECOLOR_NORMAL "\033[0m"
#define ECOLOR_FLUSH "\033[K"
/* A cheat sheet of colour capable terminals /* A cheat sheet of colour capable terminals
This is taken from DIR_COLORS from GNU coreutils This is taken from DIR_COLORS from GNU coreutils
We embed it here as we shouldn't depend on coreutils */ We embed it here as we shouldn't depend on coreutils */
static const char *colour_terms[] = { static const char *color_terms[] = {
"Eterm", "Eterm",
"ansi", "ansi",
"color-xterm", "color-xterm",
@ -123,7 +132,7 @@ static bool is_env (const char *var, const char *val)
return (strcasecmp (v, val) ? false : true); return (strcasecmp (v, val) ? false : true);
} }
bool colour_terminal (void) static bool colour_terminal (void)
{ {
static int in_colour = -1; static int in_colour = -1;
int i = 0; int i = 0;
@ -139,8 +148,8 @@ bool colour_terminal (void)
if (! term) if (! term)
return (true); return (true);
while (colour_terms[i]) { while (color_terms[i]) {
if (strcmp (colour_terms[i], term) == 0) { if (strcmp (color_terms[i], term) == 0) {
in_colour = 1; in_colour = 1;
return (true); return (true);
} }
@ -150,7 +159,6 @@ bool colour_terminal (void)
in_colour = 0; in_colour = 0;
return (false); return (false);
} }
hidden_def(colour_terminal)
static int get_term_columns (void) static int get_term_columns (void)
{ {
@ -397,43 +405,44 @@ static int _eindent (FILE *stream)
return (fprintf (stream, "%s", indent)); return (fprintf (stream, "%s", indent));
} }
static const char *ecolor (einfo_color_t color) { const char *ecolor (einfo_color_t color) {
const char *col = NULL; const char *col = NULL;
if (! colour_terminal ())
return ("");
switch (color) { switch (color) {
case einfo_good: case ecolor_good:
if (! (col = getenv ("EINFO_GOOD"))) if (! (col = getenv ("ECOLOR_GOOD")))
col = EINFO_GOOD; col = ECOLOR_GOOD;
break; break;
case einfo_warn: case ecolor_warn:
if (! (col = getenv ("EINFO_WARN"))) if (! (col = getenv ("ECOLOR_WARN")))
col = EINFO_WARN; col = ECOLOR_WARN;
break; break;
case einfo_bad: case ecolor_bad:
if (! (col = getenv ("EINFO_BAD"))) if (! (col = getenv ("ECOLOR_BAD")))
col = EINFO_BAD; col = ECOLOR_BAD;
break; break;
case einfo_hilite: case ecolor_hilite:
if (! (col = getenv ("EINFO_HILITE"))) if (! (col = getenv ("ECOLOR_HILITE")))
col = EINFO_HILITE; col = ECOLOR_HILITE;
break; break;
case einfo_bracket: case ecolor_bracket:
if (! (col = getenv ("EINFO_BRACKET"))) if (! (col = getenv ("ECOLOR_BRACKET")))
col = EINFO_BRACKET; col = ECOLOR_BRACKET;
break; break;
case einfo_normal: case ecolor_normal:
col = EINFO_NORMAL; col = ECOLOR_NORMAL;
break; break;
} }
return (col); return (col);
} }
hidden_def(ecolor)
#define EINFOVN(_file, _color) \ #define EINFOVN(_file, _color) \
if (colour_terminal ()) \ fprintf (_file, " %s*%s ", ecolor (_color), ecolor (ecolor_normal)); \
fprintf (_file, " %s*%s ", ecolor (_color), ecolor (einfo_normal)); \
else \
fprintf (_file, " * "); \
retval += _eindent (_file); \ retval += _eindent (_file); \
{ \ { \
va_list _ap; \ va_list _ap; \
@ -442,13 +451,13 @@ retval += _eindent (_file); \
va_end (_ap); \ va_end (_ap); \
} \ } \
if (colour_terminal ()) \ if (colour_terminal ()) \
fprintf (_file, "\033[K"); fprintf (_file, ECOLOR_FLUSH);
static int _einfovn (const char *fmt, va_list ap) static int _einfovn (const char *fmt, va_list ap)
{ {
int retval = 0; int retval = 0;
EINFOVN (stdout, einfo_good); EINFOVN (stdout, ecolor_good);
return (retval); return (retval);
} }
@ -456,7 +465,7 @@ static int _ewarnvn (const char *fmt, va_list ap)
{ {
int retval = 0; int retval = 0;
EINFOVN (stdout, einfo_warn); EINFOVN (stdout, ecolor_warn);
return (retval); return (retval);
} }
@ -464,7 +473,7 @@ static int _eerrorvn (const char *fmt, va_list ap)
{ {
int retval = 0; int retval = 0;
EINFOVN (stderr, einfo_bad); EINFOVN (stderr, ecolor_bad);
return (retval); return (retval);
} }
@ -637,15 +646,15 @@ static void _eend (int col, einfo_color_t color, const char *msg)
if (! msg) if (! msg)
return; return;
if (color == einfo_bad) if (color == ecolor_bad)
fp = stderr; fp = stderr;
cols = get_term_columns () - (strlen (msg) + 6); cols = get_term_columns () - (strlen (msg) + 6);
if (cols > 0 && colour_terminal ()) { if (cols > 0 && colour_terminal ()) {
fprintf (fp, "\033[A\033[%dC %s[ %s%s %s]%s\n", cols, fprintf (fp, "\033[A\033[%dC %s[ %s%s %s]%s\n", cols,
ecolor (einfo_bracket), ecolor (color), msg, ecolor (ecolor_bracket), ecolor (color), msg,
ecolor (einfo_bracket), ecolor (einfo_normal)); ecolor (ecolor_bracket), ecolor (ecolor_normal));
} else { } else {
for (i = -1; i < cols - col; i++) for (i = -1; i < cols - col; i++)
fprintf (fp, " "); fprintf (fp, " ");
@ -682,7 +691,9 @@ static int _do_eend (const char *cmd, int retval, const char *fmt, va_list ap)
fprintf (fp, "\n"); fprintf (fp, "\n");
} }
_eend (col, retval == 0 ? einfo_good : einfo_bad, retval == 0 ? OK : NOT_OK); _eend (col,
retval == 0 ? ecolor_good : ecolor_bad,
retval == 0 ? OK : NOT_OK);
return (retval); return (retval);
} }

View File

@ -18,34 +18,34 @@
static void print_level (char *level) static void print_level (char *level)
{ {
printf ("Runlevel: "); printf ("Runlevel: %s%s%s\n",
PEINFO_HILITE; ecolor (ecolor_hilite),
printf ("%s\n", level); level,
PEINFO_NORMAL; ecolor (ecolor_normal));
} }
static void print_service (char *service) static void print_service (char *service)
{ {
char status[10]; char status[10];
int cols = printf (" %s\n", service); int cols = printf (" %s\n", service);
einfo_color_t color = einfo_bad; einfo_color_t color = ecolor_bad;
if (rc_service_state (service, rc_service_stopping)) if (rc_service_state (service, rc_service_stopping))
snprintf (status, sizeof (status), "stopping "); snprintf (status, sizeof (status), "stopping ");
else if (rc_service_state (service, rc_service_starting)) { else if (rc_service_state (service, rc_service_starting)) {
snprintf (status, sizeof (status), "starting "); snprintf (status, sizeof (status), "starting ");
color = einfo_warn; color = ecolor_warn;
} else if (rc_service_state (service, rc_service_inactive)) { } else if (rc_service_state (service, rc_service_inactive)) {
snprintf (status, sizeof (status), "inactive "); snprintf (status, sizeof (status), "inactive ");
color = einfo_warn; color = ecolor_warn;
} else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed)) } else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
snprintf (status, sizeof (status), " crashed "); snprintf (status, sizeof (status), " crashed ");
else if (rc_service_state (service, rc_service_started)) { else if (rc_service_state (service, rc_service_started)) {
snprintf (status, sizeof (status), " started "); snprintf (status, sizeof (status), " started ");
color = einfo_good; color = ecolor_good;
} else if (rc_service_state (service, rc_service_scheduled)) { } else if (rc_service_state (service, rc_service_scheduled)) {
snprintf (status, sizeof (status), "scheduled"); snprintf (status, sizeof (status), "scheduled");
color = einfo_warn; color = ecolor_warn;
} else } else
snprintf (status, sizeof (status), " stopped "); snprintf (status, sizeof (status), " stopped ");
ebracket (cols, color, status); ebracket (cols, color, status);

View File

@ -114,6 +114,17 @@ static int do_e (int argc, char **argv)
char *p; char *p;
char *fmt = NULL; char *fmt = NULL;
if (strcmp (applet, "eval_ecolors") == 0) {
printf ("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n",
ecolor (ecolor_good),
ecolor (ecolor_warn),
ecolor (ecolor_bad),
ecolor (ecolor_hilite),
ecolor (ecolor_bracket),
ecolor (ecolor_normal));
exit (EXIT_SUCCESS);
}
if (strcmp (applet, "eend") == 0 || if (strcmp (applet, "eend") == 0 ||
strcmp (applet, "ewend") == 0 || strcmp (applet, "ewend") == 0 ||
strcmp (applet, "veend") == 0 || strcmp (applet, "veend") == 0 ||
@ -497,8 +508,6 @@ int main (int argc, char **argv)
exit (rc_runlevel_starting () ? 0 : 1); exit (rc_runlevel_starting () ? 0 : 1);
else if (strcmp (applet, "is_runlevel_stop") == 0) else if (strcmp (applet, "is_runlevel_stop") == 0)
exit (rc_runlevel_stopping () ? 0 : 1); exit (rc_runlevel_stopping () ? 0 : 1);
else if (strcmp (applet, "color_terminal") == 0)
exit (colour_terminal () ? 0 : 1);
if (strcmp (applet, "rc" ) != 0) if (strcmp (applet, "rc" ) != 0)
eerrorx ("%s: unknown applet", applet); eerrorx ("%s: unknown applet", applet);
@ -578,19 +587,14 @@ int main (int argc, char **argv)
uname (&uts); uname (&uts);
printf ("\n"); printf ("\n");
PEINFO_GOOD; printf (" %sGentoo/%s; %shttp://www.gentoo.org/%s"
printf (" Gentoo/%s; ", uts.sysname); "\n Copyright 1999-2007 Gentoo Foundation; "
PEINFO_BRACKET; "Distributed under the GPLv2\n\n",
printf ("http://www.gentoo.org/"); ecolor (ecolor_good), uts.sysname, ecolor (ecolor_bracket),
PEINFO_NORMAL; ecolor (ecolor_normal));
printf ("\n Copyright 1999-2007 Gentoo Foundation; "
"Distributed under the GPLv2\n\n");
printf ("Press "); printf ("Press %sI%s to enter interactive boot mode\n\n",
PEINFO_GOOD; ecolor (ecolor_good), ecolor (ecolor_normal));
printf ("I");
PEINFO_NORMAL;
printf (" to enter interactive boot mode\n\n");
setenv ("RC_SOFTLEVEL", newlevel, 1); setenv ("RC_SOFTLEVEL", newlevel, 1);
rc_plugin_run (rc_hook_runlevel_start_in, newlevel); rc_plugin_run (rc_hook_runlevel_start_in, newlevel);