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 #endif
}; };
void style_Enable(bool enable); bool style_Parse(char const *arg);
void style_Set(FILE *file, StyleColor color, bool bold); void style_Set(FILE *file, StyleColor color, bool bold);
void style_Reset(FILE *file); void style_Reset(FILE *file);

View File

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

View File

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

View File

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

View File

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

View File

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