Skip to content

Commit

Permalink
Merge pull request #54 from wader/interp-stdio-cleanup
Browse files Browse the repository at this point in the history
interp: Cleanup stdio usage and functions
  • Loading branch information
wader authored Jan 5, 2022
2 parents 293d8cf + e365f22 commit f6abcff
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ notable is support for arbitrary-precision integers.

- All standard library functions from jq
- Adds a few new general functions:
- `print/0`, `println/0`, `printerr/0`, `printerrln` prints to stdout and stderr.
- `streaks/0`, `streaks_by/1` like `group` but groups streaks based on condition.
- `count/0`, `count_by/1` like `group` but counts groups lengths.
- `debug/1` like `debug/0` but uses arg to produce debug message. `{a: 123} | debug({a}) | ...`.
Expand Down
6 changes: 3 additions & 3 deletions pkg/interp/assert.jq
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def log: if env.VERBOSE then stderr else empty end;
def log: if env.VERBOSE then printerrln else empty end;

def assert($name; $expected; $actual):
( if $expected == $actual then
"PASS \($name)\n" | log
"PASS \($name)" | log
else
( "FAIL \($name): expected \($expected) got \($actual)\n" | stderr
( "FAIL \($name): expected \($expected) got \($actual)" | printerrln
, (null | halt_error(1))
)
end
Expand Down
5 changes: 2 additions & 3 deletions pkg/interp/decode.jq
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ def _decode_progress:
)
else empty
end
| stderr
| printerr
);

def decode($name; $decode_opts):
( options as $opts
| (null | stdout) as $stdout
| _decode(
$name;
$opts +
{
_progress: (
if $opts.decode_progress and $opts.repl and $stdout.is_terminal then
if $opts.decode_progress and $opts.repl and stdout_tty.is_terminal then
"_decode_progress"
else null
end
Expand Down
4 changes: 2 additions & 2 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ def display($opts):
( options($opts) as $opts
| if _can_display then _display($opts)
else
( if type == "string" and $opts.raw_string then (tostring | stdout)
( if type == "string" and $opts.raw_string then print
else _print_color_json($opts)
end
, ( $opts.join_string
| if . then stdout else empty end
| if . then print else empty end
)
)
end
Expand Down
16 changes: 11 additions & 5 deletions pkg/interp/internal.jq
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# is here to be defined as early as possible to allow debugging
# TODO: move to builtin.jq etc?
def print: stdout;
def println: ., "\n" | stdout;
# TODO: move some _* to builtin.jq etc?

def stdin_tty: null | _stdin;
def stdout_tty: null | _stdout;

def print: tostring | _stdout;
def println: ., "\n" | print;
def printerr: tostring | _stderr;
def printerrln: ., "\n" | printerr;

def debug:
( ((["DEBUG", .] | tojson), "\n" | stderr)
( ((["DEBUG", .] | tojson) | printerrln)
, .
);
def debug(f): . as $c | f | debug | $c;
Expand Down Expand Up @@ -118,4 +125,3 @@ def _is_scalar:
def _is_context_canceled_error: . == "context canceled";

def _error_str: "error: \(.)";
def _errorln: ., "\n" | stderr;
6 changes: 3 additions & 3 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func init() {
return []Function{
{"_readline", 0, 2, i.readline, nil},
{"eval", 1, 2, nil, i.eval},
{"stdin", 0, 0, nil, i.makeStdioFn(i.os.Stdin())},
{"stdout", 0, 0, nil, i.makeStdioFn(i.os.Stdout())},
{"stderr", 0, 0, nil, i.makeStdioFn(i.os.Stderr())},
{"_stdin", 0, 0, nil, i.makeStdioFn(i.os.Stdin())},
{"_stdout", 0, 0, nil, i.makeStdioFn(i.os.Stdout())},
{"_stderr", 0, 0, nil, i.makeStdioFn(i.os.Stderr())},
{"_extkeys", 0, 0, i._extKeys, nil},
{"_exttype", 0, 0, i._extType, nil},
{"_global_state", 0, 1, i.makeStateFn(i.state), nil},
Expand Down
13 changes: 6 additions & 7 deletions pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def input:
( . as $err
| _input_io_errors(. += {($h): $err}) as $_
| $err
| (_error_str | _errorln)
| (_error_str | printerrln)
, _input($opts; f)
)
| try f
Expand All @@ -71,7 +71,7 @@ def input:
else ": failed to decode (try -d FORMAT)"
end
] | join("")
| (_error_str | _errorln)
| (_error_str | printerrln)
, _input($opts; f)
)
);
Expand Down Expand Up @@ -143,7 +143,7 @@ def var($k): . as $c | var($k; $c);
def _cli_expr_on_error:
( . as $err
| _cli_last_expr_error($err) as $_
| (_error_str | _errorln)
| (_error_str | printerrln)
);
def _cli_expr_on_compile_error:
( _eval_compile_error_tostring
Expand Down Expand Up @@ -180,7 +180,6 @@ def _main:
def _usage($arg0):
"Usage: \($arg0) [OPTIONS] [--] [EXPR] [FILE...]";
( . as {$version, $args, args: [$arg0]}
| (null | [stdin, stdout]) as [$stdin, $stdout]
# make sure we don't unintentionally use . to make things clearer
| null
| ( try _args_parse($args[1:]; _opt_cli_opts)
Expand Down Expand Up @@ -311,10 +310,10 @@ def _main:
$opts.null_input == false and
($opts.repl | not) and
($opts.expr_file | not) and
$stdin.is_terminal and
$stdout.is_terminal
stdin_tty.is_terminal and
stdout_tty.is_terminal
) then
( (( _usage($arg0), "\n") | stderr)
( (_usage($arg0) | printerrln)
, null | halt_error(_exit_code_args_error)
)
else
Expand Down
14 changes: 9 additions & 5 deletions pkg/interp/options.jq
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def _obj_to_csv_kv:
[to_entries[] | [.key, .value] | join("=")] | join(",");

def _opt_build_default_fixed:
( (null | stdout) as $stdout
( stdout_tty as $stdout
| {
addrbase: 16,
arg: [],
Expand Down Expand Up @@ -55,11 +55,15 @@ def _opt_build_default_fixed:
);

def _opt_default_dynamic:
( (null | stdout) as $stdout
( stdout_tty as $stdout
# TODO: intdiv 2 * 2 to get even number, nice or maybe not needed?
| ( if $stdout.is_terminal then [_intdiv(_intdiv($stdout.width; 8); 2) * 2, 4] | max
else 16
end
) as $display_bytes
| {
# TODO: intdiv 2 * 2 to get even number, nice or maybe not needed?
display_bytes: (if $stdout.is_terminal then [_intdiv(_intdiv($stdout.width; 8); 2) * 2, 4] | max else 16 end),
line_bytes: (if $stdout.is_terminal then [_intdiv(_intdiv($stdout.width; 8); 2) * 2, 4] | max else 16 end),
display_bytes: $display_bytes,
line_bytes: $display_bytes,
}
);

Expand Down

0 comments on commit f6abcff

Please sign in to comment.