From 6a401c82620681aa8c6ec4323474d22e4e5ec226 Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Fri, 8 Nov 2013 12:19:41 +0000 Subject: Add a --unbuffered option. Closes #206 --- docs/content/3.manual/manual.yml | 6 ++++++ jv.h | 2 +- jv_print.c | 3 +++ main.c | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) 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. diff --git a/jv.h b/jv.h index a9010211..0f50359a 100644 --- a/jv.h +++ b/jv.h @@ -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); diff --git a/jv_print.c b/jv_print.c index da90abfb..d6eff040 100644 --- a/jv_print.c +++ b/jv_print.c @@ -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) { diff --git a/main.c b/main.c index c365a3c2..c9e09ce8 100644 --- a/main.c +++ b/main.c @@ -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")) { -- cgit v1.2.3