diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2012-12-03 01:21:07 +0000 |
---|---|---|
committer | Stephen Dolan <mu@netsoc.tcd.ie> | 2012-12-03 01:21:07 +0000 |
commit | e9c7548b825a4fb56b53111a76cb90e0f6aef4cc (patch) | |
tree | 02caf76ee2995386edaa796bd2cd88499b4755bf /main.c | |
parent | 67f8ad943538e00826966c069d917b5bc99a4e47 (diff) |
Oh alright then, if you insist.
Colo(u)red output for jq. Enabled by default if isatty(stdout).
Closes #11.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 13 |
1 files changed, 11 insertions, 2 deletions
@@ -2,6 +2,7 @@ #include <errno.h> #include <string.h> #include <ctype.h> +#include <unistd.h> #include "compile.h" #include "builtin.h" #include "jv.h" @@ -50,8 +51,10 @@ enum { RAW_OUTPUT = 8, COMPACT_OUTPUT = 16, ASCII_OUTPUT = 32, + COLOUR_OUTPUT = 64, + NO_COLOUR_OUTPUT = 128, - FROM_FILE = 64, + FROM_FILE = 256, }; static int options = 0; static struct bytecode* bc; @@ -63,9 +66,11 @@ static void process(jv value) { if ((options & RAW_OUTPUT) && jv_get_kind(result) == JV_KIND_STRING) { fwrite(jv_string_value(result), 1, jv_string_length(jv_copy(result)), stdout); } else { - int dumpopts = 0; + int dumpopts = isatty(fileno(stdout)) ? JV_PRINT_COLOUR : 0; if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY; if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII; + if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR; + if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR; jv_dump(result, dumpopts); } printf("\n"); @@ -111,6 +116,10 @@ int main(int argc, char* argv[]) { options |= RAW_OUTPUT; } else if (isoption(argv[i], 'c', "compact-output")) { options |= COMPACT_OUTPUT; + } else if (isoption(argv[i], 'C', "color-output")) { + options |= COLOUR_OUTPUT; + } else if (isoption(argv[i], 'M', "monochrome-output")) { + options |= NO_COLOUR_OUTPUT; } else if (isoption(argv[i], 'a', "ascii-output")) { options |= ASCII_OUTPUT; } else if (isoption(argv[i], 'R', "raw-input")) { |