]> git.ipfire.org Git - people/ms/network.git/blame - src/networkctl/terminal.c
networkctl: Add color functions
[people/ms/network.git] / src / networkctl / terminal.c
CommitLineData
aa7cc092
MT
1/*#############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2023 IPFire Network Development Team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
21#include <stdlib.h>
22#include <unistd.h>
23
24#include "terminal.h"
25
26// Cache the color mode
27static color_mode_t __color_mode = COLORS_UNKNOWN;
28
29static color_mode_t detect_color_mode(void) {
30 const char* s = NULL;
31
32 // Check for NO_COLOR and if found turn off colours
33 s = secure_getenv("NO_COLOR");
34 if (s)
35 return COLORS_OFF;
36
37 // Disable colours if this isn't an interactive terminal
38 if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO))
39 return COLORS_OFF;
40
41 // Otherwise we enable colours
42 return COLORS_ON;
43}
44
45color_mode_t color_mode() {
46 if (__color_mode == COLORS_UNKNOWN) {
47 __color_mode = detect_color_mode();
48 }
49
50 return __color_mode;
51}