]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Add color functions
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Sep 2023 11:58:18 +0000 (11:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Sep 2023 11:58:18 +0000 (11:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/color.c [new file with mode: 0644]
src/cli/lib/color.h [new file with mode: 0644]
src/cli/lib/terminal.c
src/cli/lib/terminal.h

index 91810b11099d3f2dfe7461b2e2ce762d27912b10..24a9db311189d0be47d9b777da91c0b22a8dac67 100644 (file)
@@ -432,6 +432,8 @@ libcli_la_SOURCES = \
        src/cli/lib/check.h \
        src/cli/lib/clean.c \
        src/cli/lib/clean.h \
+       src/cli/lib/color.c \
+       src/cli/lib/color.h \
        src/cli/lib/command.c \
        src/cli/lib/command.h \
        src/cli/lib/dist.c \
diff --git a/src/cli/lib/color.c b/src/cli/lib/color.c
new file mode 100644 (file)
index 0000000..859d5fc
--- /dev/null
@@ -0,0 +1,51 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# 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 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdlib.h>
+
+#include "color.h"
+#include "terminal.h"
+
+// Cache the color mode
+static color_mode_t __color_mode = COLORS_UNKNOWN;
+
+static color_mode_t detect_color_mode(void) {
+       const char* s = NULL;
+
+       // Check for NO_COLOR and if found turn off colours
+       s = secure_getenv("NO_COLOR");
+       if (s)
+               return COLORS_OFF;
+
+       // Disable colours if this isn't an interactive terminal
+       if (!cli_term_is_interactive())
+               return COLORS_OFF;
+
+       // Otherwise we enable colours
+       return COLORS_ON;
+}
+
+color_mode_t color_mode() {
+       if (__color_mode == COLORS_UNKNOWN) {
+               __color_mode = detect_color_mode();
+       }
+
+       return __color_mode;
+}
diff --git a/src/cli/lib/color.h b/src/cli/lib/color.h
new file mode 100644 (file)
index 0000000..2817fc6
--- /dev/null
@@ -0,0 +1,83 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# 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 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# 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, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_COLOR_H
+#define PAKFIRE_CLI_COLOR_H
+
+typedef enum color_mode {
+       COLORS_UNKNOWN = 0,
+       COLORS_OFF,
+       COLORS_ON,
+} color_mode_t;
+
+// Reset
+#define COLOR_RESET              "\x1B[0m"
+
+// Highlight
+#define COLOR_HIGHLIGHT          "\x1B[0;1;39m"
+
+// Regular Colors
+#define COLOR_BLACK              "\x1B[0;30m"
+#define COLOR_RED                "\x1B[0;31m"
+#define COLOR_GREEN              "\x1B[0;32m"
+#define COLOR_YELLOW             "\x1B[0;33m"
+#define COLOR_BLUE               "\x1B[0;34m"
+#define COLOR_MAGENTA            "\x1B[0;35m"
+#define COLOR_CYAN               "\x1B[0;36m"
+#define COLOR_WHITE              "\x1B[0;37m"
+
+#define COLOR_BRIGHT_BLACK       "\x1B[0;90m"
+#define COLOR_BRIGHT_RED         "\x1B[0;91m"
+#define COLOR_BRIGHT_GREEN       "\x1B[0;92m"
+#define COLOR_BRIGHT_YELLOW      "\x1B[0;93m"
+#define COLOR_BRIGHT_BLUE        "\x1B[0;94m"
+#define COLOR_BRIGHT_MAGENTA     "\x1B[0;95m"
+#define COLOR_BRIGHT_CYAN        "\x1B[0;96m"
+#define COLOR_BRIGHT_WHITE       "\x1B[0;97m"
+
+#define COLOR_HIGHLIGHT_BLACK    "\x1B[0;1;30m"
+#define COLOR_HIGHLIGHT_RED      "\x1B[0;1;31m"
+#define COLOR_HIGHLIGHT_GREEN    "\x1B[0;1;32m"
+#define COLOR_HIGHLIGHT_YELLOW   "\x1B[0;1;33m"
+#define COLOR_HIGHLIGHT_BLUE     "\x1B[0;1;34m"
+#define COLOR_HIGHLIGHT_MAGENTA  "\x1B[0;1;35m"
+#define COLOR_HIGHLIGHT_CYAN     "\x1B[0;1;36m"
+#define COLOR_HIGHLIGHT_WHITE    "\x1B[0;1;37m"
+
+// Returns the color mode
+color_mode_t color_mode(void);
+
+#define COLOR_FUNC(name, color) \
+       static inline const char* color_##name(void) { \
+               return (color_mode() == COLORS_ON) ? COLOR_ ## color : ""; \
+       }
+
+COLOR_FUNC(reset,     RESET)
+COLOR_FUNC(highlight, HIGHLIGHT)
+COLOR_FUNC(black,     BLACK)
+COLOR_FUNC(red,       RED)
+COLOR_FUNC(green,     GREEN)
+COLOR_FUNC(yellow,    YELLOW)
+COLOR_FUNC(blue,      BLUE)
+COLOR_FUNC(magenta,   MAGENTA)
+COLOR_FUNC(cyan,      CYAN)
+COLOR_FUNC(white,     WHITE)
+
+#endif /* PAKFIRE_CLI_COLOR_H */
index ad005029d442e9242db6532458c2c26707ce9b39..53fbca07466225f29e46e3be5573a240a0806a33 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "terminal.h"
 
-static int cli_term_is_interactive(void) {
+int cli_term_is_interactive() {
        return isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO);
 }
 
index 5243ea41967f2cf44c95c9f347b213ccdb79bf66..9798c102d32c1784675deb8c4a4d61391a65816e 100644 (file)
@@ -24,6 +24,8 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/transaction.h>
 
+int cli_term_is_interactive(void);
+
 int cli_term_confirm(struct pakfire* pakfire,
        void* data, const char* message, const char* question);
 int cli_term_confirm_yes(struct pakfire* pakfire,