Skip to content

Commit 3ff16d9

Browse files
committed
Add flag to disable colors in output
1 parent 9332ddc commit 3ff16d9

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ void fill_output()
208208
static const struct option longopts[] =
209209
{
210210
{ "help", no_argument, NULL, 'h' }
211+
, { "nocolor", no_argument, NULL, 'n' }
211212
, { "reverse", no_argument, NULL, 'r' }
212213
, { "sort", required_argument, NULL, 's' }
213214
, {}
@@ -246,6 +247,7 @@ static void print_help(const char* argv0)
246247
printf(
247248
"Mandatory arguments to long options are also mandatory for short options.\n"
248249
" -h --help Display this help message.\n"
250+
" -n --nocolor Do not display color codes.\n"
249251
" -r --reverse Reverse sort order.\n"
250252
" -s --sort=<ARG> Sort output by ARG. Accepted values for ARG are: \n");
251253
printf(
@@ -263,13 +265,16 @@ int main(int argc, char* argv[])
263265
int sort_dir = 1;
264266
int opt;
265267

266-
while (-1 != (opt = getopt_long(argc, argv, "hrs:", longopts, NULL)))
268+
while (-1 != (opt = getopt_long(argc, argv, "hnrs:", longopts, NULL)))
267269
{
268270
switch (opt)
269271
{
270272
case 'r':
271273
sort_dir = -1;
272274
break;
275+
case 'n':
276+
output_fmt_enable_colors(false);
277+
break;
273278
case 's':
274279
{
275280
enum FIELD f;

src/output_fmt.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,42 @@ enum COLOR
3131
, _COLOR_MAX
3232
};
3333

34-
static const char* colors[_COLOR_MAX] =
34+
enum COLORSET
3535
{
36-
[COLOR_RED] = "\x1B[31m"
37-
, [COLOR_GREEN] = "\x1B[32m"
38-
, [COLOR_YELLOW] = "\x1B[33m"
39-
, [COLOR_BLUE] = "\x1B[34m"
40-
, [COLOR_MAGENTA] = "\x1B[35m"
41-
, [COLOR_CYAN] = "\x1B[36m"
42-
, [COLOR_BOLD] = "\x1B[1m"
43-
, [COLOR_NONE] = "\x1B[0m"
36+
_COLORSET_FIRST = 0
37+
, COLORSET_DEFAULT = _COLORSET_FIRST
38+
, COLORSET_NONE
39+
, _COLORSET_MAX
4440
};
4541

42+
static const char* colorset[_COLORSET_MAX][_COLOR_MAX] =
43+
{
44+
[COLORSET_NONE] =
45+
{
46+
[COLOR_RED] = ""
47+
, [COLOR_GREEN] = ""
48+
, [COLOR_YELLOW] = ""
49+
, [COLOR_BLUE] = ""
50+
, [COLOR_MAGENTA] = ""
51+
, [COLOR_CYAN] = ""
52+
, [COLOR_BOLD] = ""
53+
, [COLOR_NONE] = ""
54+
}
55+
, [COLORSET_DEFAULT] =
56+
{
57+
[COLOR_RED] = "\x1B[31m"
58+
, [COLOR_GREEN] = "\x1B[32m"
59+
, [COLOR_YELLOW] = "\x1B[33m"
60+
, [COLOR_BLUE] = "\x1B[34m"
61+
, [COLOR_MAGENTA] = "\x1B[35m"
62+
, [COLOR_CYAN] = "\x1B[36m"
63+
, [COLOR_BOLD] = "\x1B[1m"
64+
, [COLOR_NONE] = "\x1B[0m"
65+
}
66+
};
67+
68+
static const char** colors = colorset[COLORSET_DEFAULT];
69+
4670
struct field
4771
{
4872
char* string;
@@ -628,3 +652,12 @@ output_fmt_add_dev(
628652
return rc;
629653
}
630654

655+
void
656+
output_fmt_enable_colors(
657+
bool enable
658+
)
659+
{
660+
enum COLORSET cs = enable ? COLORSET_DEFAULT : COLORSET_NONE;
661+
colors = colorset[cs];
662+
}
663+

src/output_fmt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ void
4646
output_fmt_deinit(
4747
);
4848

49+
void
50+
output_fmt_enable_colors(
51+
bool enable
52+
);
4953
#endif /* OUTPUT_FMT_H__ */
5054

0 commit comments

Comments
 (0)