From aa7cc0927e2ea5cde54685e01290be8c0afdc31c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 12:30:33 +0000 Subject: [PATCH] networkctl: Add color functions Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/networkctl/terminal.c | 51 ++++++++++++++++++++++++ src/networkctl/terminal.h | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/networkctl/terminal.c create mode 100644 src/networkctl/terminal.h diff --git a/Makefile.am b/Makefile.am index caab99e1..95fff11a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -402,6 +402,8 @@ dist_networkctl_SOURCES = \ src/networkctl/main.c \ src/networkctl/port.c \ src/networkctl/port.h \ + src/networkctl/terminal.c \ + src/networkctl/terminal.h \ src/networkctl/zone.c \ src/networkctl/zone.h diff --git a/src/networkctl/terminal.c b/src/networkctl/terminal.c new file mode 100644 index 00000000..de7cd8dd --- /dev/null +++ b/src/networkctl/terminal.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#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 (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) + 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/networkctl/terminal.h b/src/networkctl/terminal.h new file mode 100644 index 00000000..b7bffec7 --- /dev/null +++ b/src/networkctl/terminal.h @@ -0,0 +1,83 @@ +/*############################################################################# +# # +# 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 . # +# # +#############################################################################*/ + +#ifndef NETWORKCTL_TERMINAL_H +#define NETWORKCTL_TERMINAL_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 /* NETWORKCTL_TERMINAL_H */ -- 2.39.2