summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOndrej Oprala2012-10-16 16:39:00 +0200
committerKarel Zak2012-10-16 16:39:00 +0200
commitdfa68ad183617ef2a18e86ea7782c4e95c0f8e01 (patch)
tree32910ec412b43c5a26ed3527c012fd8b780e411d
parentmount: add long options for -L and -U to man page (diff)
downloadkernel-qcow2-util-linux-dfa68ad183617ef2a18e86ea7782c4e95c0f8e01.tar.gz
kernel-qcow2-util-linux-dfa68ad183617ef2a18e86ea7782c4e95c0f8e01.tar.xz
kernel-qcow2-util-linux-dfa68ad183617ef2a18e86ea7782c4e95c0f8e01.zip
lib/color: add module for work with terminal colors
[kzak@redhat.com: - split from dmesg patch - add more colors] Signed-off-by: Ondrej Oprala <ooprala@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--include/colors.h50
-rw-r--r--lib/Makemodule.am1
-rw-r--r--lib/colors.c28
3 files changed, 79 insertions, 0 deletions
diff --git a/include/colors.h b/include/colors.h
new file mode 100644
index 000000000..0eb19464f
--- /dev/null
+++ b/include/colors.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2012 Ondrej Oprala <ooprala@redhat.com>
+ *
+ * This file may be distributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#ifndef UTIL_LINUX_COLORS_H
+#define UTIL_LINUX_COLORS_H
+
+#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_YELLOW "\033[33m"
+#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"
+
+/* Initialize the global variable OUT_IS_TERM */
+extern int colors_init(void);
+
+/* Set the color to CLR_SCHEME */
+extern void color_enable(const char *clr_scheme);
+
+/* Reset colors to default */
+extern void color_disable(void);
+
+#endif /* UTIL_LINUX_COLORS_H */
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index 33a3eb818..3c89fb8f9 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -5,6 +5,7 @@ libcommon_la_SOURCES = \
lib/at.c \
lib/blkdev.c \
lib/canonicalize.c \
+ lib/colors.c \
lib/cpuset.c \
lib/crc32.c \
lib/env.c \
diff --git a/lib/colors.c b/lib/colors.c
new file mode 100644
index 000000000..bb9c057e1
--- /dev/null
+++ b/lib/colors.c
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2012 Ondrej Oprala <ooprala@redhat.com>
+ *
+ * This file may be distributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+
+#include "colors.h"
+
+static int ul_color_term_ok;
+
+int colors_init(void)
+{
+ ul_color_term_ok = isatty(STDOUT_FILENO);
+ return ul_color_term_ok;
+}
+
+void color_enable(const char *color_scheme)
+{
+ if (ul_color_term_ok)
+ fputs(color_scheme, stdout);
+}
+
+void color_disable(void)
+{
+ if (ul_color_term_ok)
+ fputs(UL_COLOR_RESET, stdout);
+}