Merge common code for conditionally colored output

Instead of calling enable_color() conditionally with identical check in
three places, introduce check_enable_color() which does it in one place.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Phil Sutter 2018-08-15 18:21:26 +02:00 committed by David Ahern
parent 5332148deb
commit 4d82962ccc
5 changed files with 13 additions and 6 deletions

View File

@ -200,8 +200,7 @@ main(int argc, char **argv)
_SL_ = oneline ? "\\" : "\n";
if (color && !json)
enable_color();
check_enable_color(color, json);
if (batch_file)
return batch(batch_file);

View File

@ -13,6 +13,7 @@ enum color_attr {
};
void enable_color(void);
int check_enable_color(int color, int json);
void set_color_palette(void);
int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
enum color_attr ifa_family_color(__u8 ifa_family);

View File

@ -304,8 +304,7 @@ int main(int argc, char **argv)
_SL_ = oneline ? "\\" : "\n";
if (color && !json)
enable_color();
check_enable_color(color, json);
if (batch_file)
return batch(batch_file);

View File

@ -77,6 +77,15 @@ void enable_color(void)
set_color_palette();
}
int check_enable_color(int color, int json)
{
if (color && !json) {
enable_color();
return 0;
}
return 1;
}
void set_color_palette(void)
{
char *p = getenv("COLORFGBG");

View File

@ -518,8 +518,7 @@ int main(int argc, char **argv)
_SL_ = oneline ? "\\" : "\n";
if (color && !json)
enable_color();
check_enable_color(color, json);
if (batch_file)
return batch(batch_file);