Skip to content

Commit 033498b

Browse files
committed
interp: Don't output raw binary if display is called explicitly
So raw binay is only outputted if stdout is not a terminal and "... | d" is not used.. fq -n '[1,2,3] | tobytes' | cat > binary fq -n '[1,2,3] | tobytes | d' | cat > hexdump
1 parent 1cbc253 commit 033498b

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

pkg/interp/init.jq

+11-9
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,21 @@ def _cli_slurp_error(_):
137137
_eval_error("compile"; "slurp can only be used from interactive repl");
138138
# TODO: rewrite query to reuse _display_default_opts value? also _repl_display
139139
def _cli_display:
140-
display(_display_default_opts);
140+
display_implicit(_display_default_opts);
141141
# _cli_eval halts on compile errors
142142
def _cli_eval($expr; $opts):
143143
eval(
144144
$expr;
145-
$opts + {
146-
slurps: {
147-
help: "_help_slurp",
148-
repl: "_cli_repl_error",
149-
slurp: "_cli_slurp_error"
150-
},
151-
catch_query: _query_func("_cli_eval_on_expr_error"),
152-
};
145+
( $opts
146+
+ {
147+
slurps: {
148+
help: "_help_slurp",
149+
repl: "_cli_repl_error",
150+
slurp: "_cli_slurp_error"
151+
},
152+
catch_query: _query_func("_cli_eval_on_expr_error"),
153+
}
154+
);
153155
_cli_eval_on_error;
154156
_cli_eval_on_compile_error
155157
);

pkg/interp/interp.jq

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ def _todisplay:
1212
| _format_func($f; "_todisplay")
1313
);
1414

15-
def display($opts):
15+
def display($opts; $explicit_call):
1616
( . as $c
1717
| options($opts) as $opts
1818
| try _todisplay catch $c
1919
| if $opts.value_output then tovalue end
20-
| if _can_display then _display($opts)
20+
| if _can_display then
21+
_display(
22+
( $opts
23+
# don't output raw binary if d/display was call explicitly
24+
| if $explicit_call then .raw_output = false end
25+
)
26+
)
2127
else
2228
( if _is_string and $opts.raw_string then print
2329
else _print_color_json($opts)
@@ -29,8 +35,11 @@ def display($opts):
2935
end
3036
| error("unreachable")
3137
);
38+
def display($opts): display($opts; true);
3239
def display: display({});
3340

41+
def display_implicit($opts): display($opts; false);
42+
3443
def d($opts): display($opts);
3544
def d: display({});
3645
def da($opts): display({array_truncate: 0} + $opts);

pkg/interp/testdata/binary_stdout.fqtest

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ $ _STDOUT_IS_TERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[1,1,1 | tobits] | tobit
66
0380\
77
$ _STDOUT_IS_TERMINAL=0 _STDOUT_HEX=1 NO_COLOR=1 fq -n '[5 | tobits(12)], [3 | tobytes(3)] | tobytes'
88
0005000003\
9+
# explicit call to display don't output binary
10+
$ _STDOUT_IS_TERMINAL=0 NO_COLOR=1 fq -n '[1,2,3] | tobytes | d'
11+
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
12+
0x0|01 02 03| |...| |.: raw bits 0x0-0x2.7 (3)

0 commit comments

Comments
 (0)