diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2020-09-13 00:37:17 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2020-09-13 00:37:17 -0700 |
| commit | 8f090c68c074d2de46e11e165e76d5e108d918be (patch) | |
| tree | 708c4b6bb82229f416a2b37ac8f0d4f90dbcab2b | |
| parent | 877526b5df3c73310f1029e56c9dff1c0374c7a2 (diff) | |
Don't use color for non-tty outputs, added `-m` mode argument
| -rw-r--r-- | bpeg.c | 4 | ||||
| -rw-r--r-- | vm.c | 16 |
2 files changed, 11 insertions, 9 deletions
@@ -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); @@ -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]"); } |
