Add --tab and -indent n options
This commit is contained in:
1 parent
74ecb88935
commit
24a5e5b1b1
4 files changed
+58
-22
No files matched your search
@@ -140,6 +140,14 @@ sections:
|
|||||||
will result in more compact output by instead putting each
|
will result in more compact output by instead putting each
|
||||||
JSON object on a single line.
|
JSON object on a single line.
|
||||||
|
|
||||||
|
* `--tab`:
|
||||||
|
|
||||||
|
Use a tab for each indentation level instead of two spaces.
|
||||||
|
|
||||||
|
* `--indent n`:
|
||||||
|
|
||||||
|
Use the given number of spaces (no more than 8) for indentation.
|
||||||
|
|
||||||
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
|
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
|
||||||
|
|
||||||
By default, jq outputs colored JSON if writing to a
|
By default, jq outputs colored JSON if writing to a
|
||||||
|
|||||||
@@ -159,14 +159,20 @@ jv jv_object_iter_value(jv, int);
|
|||||||
|
|
||||||
int jv_get_refcnt(jv);
|
int jv_get_refcnt(jv);
|
||||||
|
|
||||||
enum {
|
enum jv_print_flags {
|
||||||
JV_PRINT_PRETTY = 1,
|
JV_PRINT_PRETTY = 1,
|
||||||
JV_PRINT_ASCII = 2,
|
JV_PRINT_ASCII = 2,
|
||||||
JV_PRINT_COLOUR = 4,
|
JV_PRINT_COLOUR = 4,
|
||||||
JV_PRINT_SORTED = 8,
|
JV_PRINT_SORTED = 8,
|
||||||
JV_PRINT_INVALID = 16,
|
JV_PRINT_INVALID = 16,
|
||||||
JV_PRINT_REFCOUNT = 32,
|
JV_PRINT_REFCOUNT = 32,
|
||||||
|
JV_PRINT_TAB = 64,
|
||||||
|
JV_PRINT_SPACE0 = 256,
|
||||||
|
JV_PRINT_SPACE1 = 512,
|
||||||
|
JV_PRINT_SPACE2 = 1024,
|
||||||
};
|
};
|
||||||
|
#define JV_PRINT_INDENT_FLAGS(n) \
|
||||||
|
((n) < 0 || (n) > 7 ? JV_PRINT_TAB | JV_PRINT_PRETTY : (n) == 0 ? 0 : (n) << 8 | JV_PRINT_PRETTY)
|
||||||
void jv_dumpf(jv, FILE *f, int flags);
|
void jv_dumpf(jv, FILE *f, int flags);
|
||||||
void jv_dump(jv, int flags);
|
void jv_dump(jv, int flags);
|
||||||
void jv_show(jv, int flags);
|
void jv_show(jv, int flags);
|
||||||
|
|||||||
+17
-14
@@ -37,9 +37,14 @@ static void put_str(const char* s, FILE* fout, jv* strout) {
|
|||||||
put_buf(s, strlen(s), fout, strout);
|
put_buf(s, strlen(s), fout, strout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void put_space(int n, FILE* fout, jv* strout) {
|
static void put_indent(int n, int flags, FILE* fout, jv* strout) {
|
||||||
while (n--) {
|
if (flags & JV_PRINT_TAB) {
|
||||||
put_char(' ', fout, strout);
|
while (n--)
|
||||||
|
put_char('\t', fout, strout);
|
||||||
|
} else {
|
||||||
|
n *= ((flags & (JV_PRINT_SPACE0 | JV_PRINT_SPACE1 | JV_PRINT_SPACE2)) >> 8);
|
||||||
|
while (n--)
|
||||||
|
put_char(' ', fout, strout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,8 +115,6 @@ static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
|
|||||||
put_char('"', F, S);
|
put_char('"', F, S);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum { INDENT = 2 };
|
|
||||||
|
|
||||||
static void put_refcnt(struct dtoa_context* C, int refcnt, FILE *F, jv* S){
|
static void put_refcnt(struct dtoa_context* C, int refcnt, FILE *F, jv* S){
|
||||||
char buf[JVP_DTOA_FMT_MAX_LEN];
|
char buf[JVP_DTOA_FMT_MAX_LEN];
|
||||||
put_char(' ', F, S);
|
put_char(' ', F, S);
|
||||||
@@ -184,23 +187,23 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
|||||||
put_str("[", F, S);
|
put_str("[", F, S);
|
||||||
if (flags & JV_PRINT_PRETTY) {
|
if (flags & JV_PRINT_PRETTY) {
|
||||||
put_char('\n', F, S);
|
put_char('\n', F, S);
|
||||||
put_space(indent+INDENT, F, S);
|
put_indent(indent + 1, flags, F, S);
|
||||||
}
|
}
|
||||||
jv_array_foreach(x, i, elem) {
|
jv_array_foreach(x, i, elem) {
|
||||||
if (i!=0) {
|
if (i!=0) {
|
||||||
if (flags & JV_PRINT_PRETTY) {
|
if (flags & JV_PRINT_PRETTY) {
|
||||||
put_str(",\n", F, S);
|
put_str(",\n", F, S);
|
||||||
put_space(indent+INDENT, F, S);
|
put_indent(indent + 1, flags, F, S);
|
||||||
} else {
|
} else {
|
||||||
put_str(",", F, S);
|
put_str(",", F, S);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jv_dump_term(C, elem, flags, indent + INDENT, F, S);
|
jv_dump_term(C, elem, flags, indent + 1, F, S);
|
||||||
if (colour) put_str(colour, F, S);
|
if (colour) put_str(colour, F, S);
|
||||||
}
|
}
|
||||||
if (flags & JV_PRINT_PRETTY) {
|
if (flags & JV_PRINT_PRETTY) {
|
||||||
put_char('\n', F, S);
|
put_char('\n', F, S);
|
||||||
put_space(indent, F, S);
|
put_indent(indent, flags, F, S);
|
||||||
}
|
}
|
||||||
if (colour) put_str(colour, F, S);
|
if (colour) put_str(colour, F, S);
|
||||||
put_char(']', F, S);
|
put_char(']', F, S);
|
||||||
@@ -216,7 +219,7 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
|||||||
put_char('{', F, S);
|
put_char('{', F, S);
|
||||||
if (flags & JV_PRINT_PRETTY) {
|
if (flags & JV_PRINT_PRETTY) {
|
||||||
put_char('\n', F, S);
|
put_char('\n', F, S);
|
||||||
put_space(indent+INDENT, F, S);
|
put_indent(indent + 1, flags, F, S);
|
||||||
}
|
}
|
||||||
int first = 1;
|
int first = 1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -250,7 +253,7 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
|||||||
if (!first) {
|
if (!first) {
|
||||||
if (flags & JV_PRINT_PRETTY){
|
if (flags & JV_PRINT_PRETTY){
|
||||||
put_str(",\n", F, S);
|
put_str(",\n", F, S);
|
||||||
put_space(indent+INDENT, F, S);
|
put_indent(indent + 1, flags, F, S);
|
||||||
} else {
|
} else {
|
||||||
put_str(",", F, S);
|
put_str(",", F, S);
|
||||||
}
|
}
|
||||||
@@ -267,12 +270,12 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
|
|||||||
put_str((flags & JV_PRINT_PRETTY) ? ": " : ":", F, S);
|
put_str((flags & JV_PRINT_PRETTY) ? ": " : ":", F, S);
|
||||||
if (colour) put_str(COLRESET, F, S);
|
if (colour) put_str(COLRESET, F, S);
|
||||||
|
|
||||||
jv_dump_term(C, value, flags, indent + INDENT, F, S);
|
jv_dump_term(C, value, flags, indent + 1, F, S);
|
||||||
if (colour) put_str(colour, F, S);
|
if (colour) put_str(colour, F, S);
|
||||||
}
|
}
|
||||||
if (flags & JV_PRINT_PRETTY) {
|
if (flags & JV_PRINT_PRETTY) {
|
||||||
put_char('\n', F, S);
|
put_char('\n', F, S);
|
||||||
put_space(indent, F, S);
|
put_indent(indent, flags, F, S);
|
||||||
}
|
}
|
||||||
if (colour) put_str(colour, F, S);
|
if (colour) put_str(colour, F, S);
|
||||||
put_char('}', F, S);
|
put_char('}', F, S);
|
||||||
@@ -300,7 +303,7 @@ void jv_dump(jv x, int flags) {
|
|||||||
/* This one is nice for use in debuggers */
|
/* This one is nice for use in debuggers */
|
||||||
void jv_show(jv x, int flags) {
|
void jv_show(jv x, int flags) {
|
||||||
if (flags == -1)
|
if (flags == -1)
|
||||||
flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR;
|
flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR | JV_PRINT_INDENT_FLAGS(2);
|
||||||
jv_dumpf(jv_copy(x), stderr, flags | JV_PRINT_INVALID);
|
jv_dumpf(jv_copy(x), stderr, flags | JV_PRINT_INVALID);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ static void usage(int code) {
|
|||||||
"\t -C\t\tcolorize JSON;\n"
|
"\t -C\t\tcolorize JSON;\n"
|
||||||
"\t -M\t\tmonochrome (don't colorize JSON);\n"
|
"\t -M\t\tmonochrome (don't colorize JSON);\n"
|
||||||
"\t -S\t\tsort keys of objects on output;\n"
|
"\t -S\t\tsort keys of objects on output;\n"
|
||||||
|
"\t --tab\tuse tabs for indentation;\n"
|
||||||
"\t --arg a v\tset variable $a to value <v>;\n"
|
"\t --arg a v\tset variable $a to value <v>;\n"
|
||||||
"\t --argjson a v\tset variable $a to JSON value <v>;\n"
|
"\t --argjson a v\tset variable $a to JSON value <v>;\n"
|
||||||
"\t --slurpfile a f\tset variable $a to an array of JSON texts read from <f>;\n"
|
"\t --slurpfile a f\tset variable $a to an array of JSON texts read from <f>;\n"
|
||||||
@@ -91,7 +92,6 @@ enum {
|
|||||||
RAW_INPUT = 2,
|
RAW_INPUT = 2,
|
||||||
PROVIDE_NULL = 4,
|
PROVIDE_NULL = 4,
|
||||||
RAW_OUTPUT = 8,
|
RAW_OUTPUT = 8,
|
||||||
COMPACT_OUTPUT = 16,
|
|
||||||
ASCII_OUTPUT = 32,
|
ASCII_OUTPUT = 32,
|
||||||
COLOUR_OUTPUT = 64,
|
COLOUR_OUTPUT = 64,
|
||||||
NO_COLOUR_OUTPUT = 128,
|
NO_COLOUR_OUTPUT = 128,
|
||||||
@@ -174,6 +174,7 @@ int main(int argc, char* argv[]) {
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dumpopts = JV_PRINT_INDENT_FLAGS(2);
|
||||||
const char* program = 0;
|
const char* program = 0;
|
||||||
|
|
||||||
jq_util_input_state input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb
|
jq_util_input_state input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb
|
||||||
@@ -221,7 +222,7 @@ int main(int argc, char* argv[]) {
|
|||||||
if (!short_opts) continue;
|
if (!short_opts) continue;
|
||||||
}
|
}
|
||||||
if (isoption(argv[i], 'c', "compact-output", &short_opts)) {
|
if (isoption(argv[i], 'c', "compact-output", &short_opts)) {
|
||||||
options |= COMPACT_OUTPUT;
|
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
|
||||||
if (!short_opts) continue;
|
if (!short_opts) continue;
|
||||||
}
|
}
|
||||||
if (isoption(argv[i], 'C', "color-output", &short_opts)) {
|
if (isoption(argv[i], 'C', "color-output", &short_opts)) {
|
||||||
@@ -260,6 +261,26 @@ int main(int argc, char* argv[]) {
|
|||||||
options |= RAW_OUTPUT | RAW_NO_LF;
|
options |= RAW_OUTPUT | RAW_NO_LF;
|
||||||
if (!short_opts) continue;
|
if (!short_opts) continue;
|
||||||
}
|
}
|
||||||
|
if (isoption(argv[i], 0, "tab", &short_opts)) {
|
||||||
|
dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);
|
||||||
|
dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
|
||||||
|
if (!short_opts) continue;
|
||||||
|
}
|
||||||
|
if (isoption(argv[i], 0, "indent", &short_opts)) {
|
||||||
|
if (i >= argc - 1) {
|
||||||
|
fprintf(stderr, "%s: --indent takes one parameter\n", progname);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
|
||||||
|
int indent = atoi(argv[i+1]);
|
||||||
|
if (indent < -1 || indent > 7) {
|
||||||
|
fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
|
||||||
|
i++;
|
||||||
|
if (!short_opts) continue;
|
||||||
|
}
|
||||||
if (isoption(argv[i], 0, "seq", &short_opts)) {
|
if (isoption(argv[i], 0, "seq", &short_opts)) {
|
||||||
options |= SEQ;
|
options |= SEQ;
|
||||||
if (!short_opts) continue;
|
if (!short_opts) continue;
|
||||||
@@ -279,7 +300,7 @@ int main(int argc, char* argv[]) {
|
|||||||
// FIXME: For --arg* we should check that the varname is acceptable
|
// FIXME: For --arg* we should check that the varname is acceptable
|
||||||
if (isoption(argv[i], 0, "arg", &short_opts)) {
|
if (isoption(argv[i], 0, "arg", &short_opts)) {
|
||||||
if (i >= argc - 2) {
|
if (i >= argc - 2) {
|
||||||
fprintf(stderr, "%s: --arg takes two parameters (e.g. -a varname value)\n", progname);
|
fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
jv arg = jv_object();
|
jv arg = jv_object();
|
||||||
@@ -291,7 +312,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
if (isoption(argv[i], 0, "argjson", &short_opts)) {
|
if (isoption(argv[i], 0, "argjson", &short_opts)) {
|
||||||
if (i >= argc - 2) {
|
if (i >= argc - 2) {
|
||||||
fprintf(stderr, "%s: --argjson takes two parameters (e.g. -a varname text)\n", progname);
|
fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
jv v = jv_parse(argv[i+2]);
|
jv v = jv_parse(argv[i+2]);
|
||||||
@@ -314,7 +335,7 @@ int main(int argc, char* argv[]) {
|
|||||||
else
|
else
|
||||||
which = "slurpfile";
|
which = "slurpfile";
|
||||||
if (i >= argc - 2) {
|
if (i >= argc - 2) {
|
||||||
fprintf(stderr, "%s: --%s takes two parameters (e.g. -a varname filename)\n", progname, which);
|
fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
jv arg = jv_object();
|
jv arg = jv_object();
|
||||||
@@ -370,7 +391,6 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int dumpopts = 0;
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
/* Disable colour by default on Windows builds as Windows
|
/* Disable colour by default on Windows builds as Windows
|
||||||
terminals tend not to display it correctly */
|
terminals tend not to display it correctly */
|
||||||
@@ -378,7 +398,6 @@ int main(int argc, char* argv[]) {
|
|||||||
dumpopts |= JV_PRINT_COLOUR;
|
dumpopts |= JV_PRINT_COLOUR;
|
||||||
#endif
|
#endif
|
||||||
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
|
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
|
||||||
if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY;
|
|
||||||
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
|
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
|
||||||
if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
|
if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
|
||||||
if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;
|
if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;
|
||||||
|
|||||||
Reference in new issue
Block a user