mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Factor out shared --color-parsing code
This commit is contained in:
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -831,15 +831,9 @@ 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")) {
|
|
||||||
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;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -544,15 +544,9 @@ 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")) {
|
|
||||||
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;
|
||||||
case 1: // Positional argument, requested by leading `-` in opt string
|
case 1: // Positional argument, requested by leading `-` in opt string
|
||||||
if (musl_optarg[0] == '@') {
|
if (musl_optarg[0] == '@') {
|
||||||
|
|||||||
@@ -392,15 +392,9 @@ 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")) {
|
|
||||||
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;
|
||||||
default:
|
default:
|
||||||
usage.printAndExit(1); // LCOV_EXCL_LINE
|
usage.printAndExit(1); // LCOV_EXCL_LINE
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user