diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-11-08 12:19:41 +0000 |
---|---|---|
committer | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-11-08 12:21:45 +0000 |
commit | 6a401c82620681aa8c6ec4323474d22e4e5ec226 (patch) | |
tree | 4c59e401424e5cbe6229e85db9f0590ebe3bb9fb | |
parent | d235f32bc920e2a6f34f3ac97af56b7ed7a580e7 (diff) |
Add a --unbuffered option. Closes #206
-rw-r--r-- | docs/content/3.manual/manual.yml | 6 | ||||
-rw-r--r-- | jv.h | 2 | ||||
-rw-r--r-- | jv_print.c | 3 | ||||
-rw-r--r-- | main.c | 4 |
4 files changed, 14 insertions, 1 deletions
diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 7f0a353a..467617ac 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -114,6 +114,12 @@ sections: ASCII output with every non-ASCII character replaced with the equivalent escape sequence. + * `--unbuffered` + + Flush the output after each JSON object is printed (useful if + you're piping a slow data source into jq and piping jq's + output elsewhere). + * `--sort-keys` / `-S`: Output the fields of each object with the keys in sorted order. @@ -110,7 +110,7 @@ jv jv_object_iter_value(jv, int); int jv_get_refcnt(jv); -enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4, JV_PRINT_SORTED = 8 }; +enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4, JV_PRINT_SORTED = 8, JV_PRINT_UNBUFFERED = 16 }; void jv_dump(jv, int flags); jv jv_dump_string(jv, int flags); @@ -265,6 +265,9 @@ void jv_dump(jv x, int flags) { jvp_dtoa_context_init(&C); jv_dump_term(&C, x, flags, 0, stdout, 0); jvp_dtoa_context_free(&C); + if (flags & JV_PRINT_UNBUFFERED) { + fflush(stdout); + } } jv jv_dump_string(jv x, int flags) { @@ -55,6 +55,7 @@ enum { COLOUR_OUTPUT = 64, NO_COLOUR_OUTPUT = 128, SORTED_OUTPUT = 256, + UNBUFFERED_OUTPUT = 4096, FROM_FILE = 512, @@ -84,6 +85,7 @@ static void process(jq_state *jq, jv value, int flags) { 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; + if (options & UNBUFFERED_OUTPUT) dumpopts |= JV_PRINT_UNBUFFERED; jv_dump(result, dumpopts); } printf("\n"); @@ -166,6 +168,8 @@ int main(int argc, char* argv[]) { options |= NO_COLOUR_OUTPUT; } else if (isoption(argv[i], 'a', "ascii-output")) { options |= ASCII_OUTPUT; + } else if (isoption(argv[i], 0, "unbuffered")) { + options |= UNBUFFERED_OUTPUT; } else if (isoption(argv[i], 'S', "sort-keys")) { options |= SORTED_OUTPUT; } else if (isoption(argv[i], 'R', "raw-input")) { |