mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 02:02:06 +00:00
Factor out shared --color-parsing code
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -831,14 +831,8 @@ 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")) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@@ -544,14 +544,8 @@ 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")) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
break;
|
||||
case 1: // Positional argument, requested by leading `-` in opt string
|
||||
|
||||
@@ -392,14 +392,8 @@ 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")) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
if (longOpt == 'c' && !style_Parse(musl_optarg)) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user