aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-09-13 00:37:17 -0700
committerBruce Hill <bruce@bruce-hill.com>2020-09-13 00:37:17 -0700
commit8f090c68c074d2de46e11e165e76d5e108d918be (patch)
tree708c4b6bb82229f416a2b37ac8f0d4f90dbcab2b
parent877526b5df3c73310f1029e56c9dff1c0374c7a2 (diff)
Don't use color for non-tty outputs, added `-m` mode argument
-rw-r--r--bpeg.c4
-rw-r--r--vm.c16
2 files changed, 11 insertions, 9 deletions
diff --git a/bpeg.c b/bpeg.c
index 9099c26..50dc9ad 100644
--- a/bpeg.c
+++ b/bpeg.c
@@ -136,6 +136,8 @@ int main(int argc, char *argv[])
check(p, "Pattern failed to compile");
add_def(g, flag, "pattern", p);
++npatterns;
+ } else if (FLAG("--mode") || FLAG("-m")) {
+ rule = flag;
} else if (argv[i][0] != '-') {
if (npatterns > 0) break;
vm_op_t *p = bpeg_stringpattern(argv[i]);
@@ -183,7 +185,7 @@ int main(int argc, char *argv[])
printf("No match\n");
return 1;
} else {
- print_match(m, "\033[0m", verbose);
+ print_match(m, isatty(STDOUT_FILENO) ? "\033[0m" : NULL, verbose);
//printf("\033[0;2m%s\n", m->end);
}
freefile(input);
diff --git a/vm.c b/vm.c
index 1e2cb76..97c0e7c 100644
--- a/vm.c
+++ b/vm.c
@@ -426,7 +426,7 @@ static match_t *get_capture_named(match_t *m, const char *name)
void print_match(match_t *m, const char *color, int verbose)
{
if (m->is_replacement) {
- printf("\033[0;34m");
+ if (color) printf("\033[0;34m");
for (const char *r = m->name_or_replacement; *r; ) {
if (*r == '\\') {
++r;
@@ -466,27 +466,27 @@ void print_match(match_t *m, const char *color, int verbose)
}
}
if (cap != NULL) {
- print_match(cap, "\033[0;35m", verbose);
- printf("\033[0;34m");
+ print_match(cap, color ? "\033[0;35m" : NULL, verbose);
+ if (color) printf("\033[0;34m");
}
}
} else {
const char *name = m->name_or_replacement;
if (verbose && m->is_ref && name && isupper(name[0]))
- printf("\033[0;2;35m{%s:", name);
+ printf(color ? "\033[0;2;35m{%s:" : "{%s", name);
//if (m->is_capture && name)
// printf("\033[0;2;33m[%s:", name);
const char *prev = m->start;
for (match_t *child = m->child; child; child = child->nextsibling) {
if (child->start > prev)
- printf("%s%.*s", color, (int)(child->start - prev), prev);
- print_match(child, m->is_capture ? "\033[0;1m" : color, verbose);
+ printf("%s%.*s", color ? color : "", (int)(child->start - prev), prev);
+ print_match(child, color ? (m->is_capture ? "\033[0;31;1m" : color) : NULL, verbose);
prev = child->end;
}
if (m->end > prev)
- printf("%s%.*s", color, (int)(m->end - prev), prev);
+ printf("%s%.*s", color ? color : "", (int)(m->end - prev), prev);
if (verbose && m->is_ref && name && isupper(name[0]))
- printf("\033[0;2;35m}");
+ printf(color ? "\033[0;2;35m}" : "}");
//if (m->is_capture && name)
// printf("\033[0;2;33m]");
}