summaryrefslogtreecommitdiffstats
path: root/lib/colors.c
diff options
context:
space:
mode:
authorKarel Zak2015-02-27 13:57:34 +0100
committerKarel Zak2015-02-27 13:57:34 +0100
commit4310faf9507b863adf0d735564f28db5c68ca600 (patch)
treee60620d3a69b9566e9e214cc316ccf2525bc116d /lib/colors.c
parentMerge branch 'fixes' of https://github.com/rudimeier/util-linux (diff)
downloadkernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.tar.gz
kernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.tar.xz
kernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.zip
lib/colors: use libtinfo to check terminal capability
The current implementation assumes that all terminals supports colors and users are forcet to use terminal-colors.d/ to disable colors for some terminals. This patch checks for maximal supported colors for the current terminal and colors are automatically disabled for terminals like vt100. The patch moves lib/colors.c from libcommon.la to libtcolors.la to avoid collisions with another utils. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/colors.c')
-rw-r--r--lib/colors.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/lib/colors.c b/lib/colors.c
index 2e5af48d9..da5a3e1f4 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -10,6 +10,10 @@
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
+#ifdef HAVE_LIBTINFO
+# include <curses.h>
+# include <term.h>
+#endif
#include "c.h"
#include "colors.h"
@@ -664,6 +668,31 @@ static void termcolors_init_debug(void)
__UL_INIT_DEBUG(termcolors, TERMCOLORS_DEBUG_, 0, TERMINAL_COLORS_DEBUG);
}
+static int colors_terminal_is_ready(void)
+{
+ int ncolors = -1;
+
+ if (isatty(STDOUT_FILENO) != 1)
+ goto none;
+
+#ifdef HAVE_LIBTINFO
+ {
+ int ret;
+
+ if (setupterm(NULL, STDOUT_FILENO, &ret) != OK || ret != 1)
+ goto none;
+ ncolors = tigetnum("colors");
+ if (ncolors <= 2)
+ goto none;
+ }
+#endif
+ DBG(CONF, ul_debug("terminal is ready (supports %d colors)", ncolors));
+ return 1;
+none:
+ DBG(CONF, ul_debug("terminal is NOT ready"));
+ return 0;
+}
+
/**
* colors_init:
* @mode: UL_COLORMODE_*
@@ -676,7 +705,7 @@ static void termcolors_init_debug(void)
*/
int colors_init(int mode, const char *name)
{
- int atty = -1;
+ int ready = -1;
struct ul_color_ctl *cc = &ul_colors;
cc->utilname = name;
@@ -684,7 +713,7 @@ int colors_init(int mode, const char *name)
termcolors_init_debug();
- if (mode == UL_COLORMODE_UNDEF && (atty = isatty(STDOUT_FILENO))) {
+ if (mode == UL_COLORMODE_UNDEF && (ready = colors_terminal_is_ready())) {
int rc = colors_read_configuration(cc);
if (rc)
cc->mode = UL_COLORMODE_DEFAULT;
@@ -703,7 +732,7 @@ int colors_init(int mode, const char *name)
switch (cc->mode) {
case UL_COLORMODE_AUTO:
- cc->has_colors = atty == -1 ? isatty(STDOUT_FILENO) : atty;
+ cc->has_colors = ready == -1 ? colors_terminal_is_ready() : ready;
break;
case UL_COLORMODE_ALWAYS:
cc->has_colors = 1;