Factor out shared --color-parsing code

This commit is contained in:
Rangi42
2025-08-12 15:53:57 -04:00
parent 7b405513d9
commit 1bf1219e07
6 changed files with 38 additions and 35 deletions

View File

@@ -35,7 +35,7 @@ enum StyleColor {
#endif
};
void style_Enable(bool enable);
bool style_Parse(char const *arg);
void style_Set(FILE *file, StyleColor color, bool bold);
void style_Reset(FILE *file);

View File

@@ -476,11 +476,7 @@ int main(int argc, char *argv[]) {
case 0:
switch (longOpt) {
case 'c':
if (!strcasecmp(musl_optarg, "always")) {
style_Enable(true);
} else if (!strcasecmp(musl_optarg, "never")) {
style_Enable(false);
} else if (strcasecmp(musl_optarg, "auto")) {
if (!style_Parse(musl_optarg)) {
fatal("Invalid argument for option '--color'");
}
break;

View File

@@ -831,15 +831,9 @@ int main(int argc, char *argv[]) {
// Long-only options
case 0:
if (longOpt == 'c') {
if (!strcasecmp(musl_optarg, "always")) {
style_Enable(true);
} else if (!strcasecmp(musl_optarg, "never")) {
style_Enable(false);
} else if (strcasecmp(musl_optarg, "auto")) {
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
fatal("Invalid argument for option '--color'");
}
}
break;
default:

View File

@@ -544,15 +544,9 @@ static char *parseArgv(int argc, char *argv[]) {
options.columnMajor = true;
break;
case 0: // Long-only options
if (longOpt == 'c') {
if (!strcasecmp(musl_optarg, "always")) {
style_Enable(true);
} else if (!strcasecmp(musl_optarg, "never")) {
style_Enable(false);
} else if (strcasecmp(musl_optarg, "auto")) {
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
fatal("Invalid argument for option '--color'");
}
}
break;
case 1: // Positional argument, requested by leading `-` in opt string
if (musl_optarg[0] == '@') {

View File

@@ -392,15 +392,9 @@ int main(int argc, char *argv[]) {
options.is32kMode = true;
break;
case 0: // Long-only options
if (longOpt == 'c') {
if (!strcasecmp(musl_optarg, "always")) {
style_Enable(true);
} else if (!strcasecmp(musl_optarg, "never")) {
style_Enable(false);
} else if (strcasecmp(musl_optarg, "auto")) {
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
fatal("Invalid argument for option '--color'");
}
}
break;
default:
usage.printAndExit(1); // LCOV_EXCL_LINE

View File

@@ -8,7 +8,7 @@
#include <stdlib.h> // getenv
#include <string.h>
#include "platform.hpp" // isatty
#include "platform.hpp" // isatty, strcasecmp
#if !STYLE_ANSI
// clang-format off: maintain `include` order
@@ -36,7 +36,7 @@ static HANDLE getHandle(FILE *file) {
}
#endif // !STYLE_ANSI
static Tribool forceStyle = []() {
static Tribool const envStyle = []() {
if (char const *forceColor = getenv("FORCE_COLOR");
forceColor && strcmp(forceColor, "") && strcmp(forceColor, "0")) {
return TRI_YES;
@@ -48,6 +48,8 @@ static Tribool forceStyle = []() {
return TRI_MAYBE;
}();
static Tribool argStyle = TRI_MAYBE;
static bool isTerminal(FILE *file) {
static bool isOutTerminal = isatty(STDOUT_FILENO);
static bool isErrTerminal = isatty(STDERR_FILENO);
@@ -56,11 +58,34 @@ static bool isTerminal(FILE *file) {
}
static bool allowStyle(FILE *file) {
return forceStyle == TRI_YES || (forceStyle == TRI_MAYBE && isTerminal(file));
if (argStyle == TRI_YES) {
return true;
} else if (argStyle == TRI_NO) {
return false;
}
if (envStyle == TRI_YES) {
return true;
} else if (envStyle == TRI_NO) {
return false;
}
return isTerminal(file);
}
void style_Enable(bool enable) {
forceStyle = enable ? TRI_YES : TRI_NO;
bool style_Parse(char const *arg) {
if (!strcasecmp(arg, "always")) {
argStyle = TRI_YES;
return true;
} else if (!strcasecmp(arg, "never")) {
argStyle = TRI_NO;
return true;
} else if (!strcasecmp(arg, "auto")) {
argStyle = TRI_MAYBE;
return true;
} else {
return false;
}
}
void style_Set(FILE *file, StyleColor color, bool bold) {