mirror of
https://github.com/wader/fq.git
synced 2024-09-11 20:07:11 +03:00
Merge pull request #724 from wader/jq-reformat
dev,jq: Reformat jq code to look more the same
This commit is contained in:
commit
9f1fe72f7a
@ -13,30 +13,27 @@ def formats_list:
|
||||
] | join(",\n");
|
||||
|
||||
def formats_table:
|
||||
( [ {
|
||||
name: "Name",
|
||||
desc: "Description",
|
||||
uses: "Dependencies"
|
||||
},
|
||||
{
|
||||
name: "-",
|
||||
desc: "-",
|
||||
uses: "-"
|
||||
},
|
||||
( formats
|
||||
( [ { name: "Name"
|
||||
, desc: "Description"
|
||||
, uses: "Dependencies"
|
||||
}
|
||||
, { name: "-"
|
||||
, desc: "-"
|
||||
, uses: "-"
|
||||
}
|
||||
, ( formats
|
||||
| to_entries[]
|
||||
| (_format_func(.key; "_help")? // {}) as $fhelp
|
||||
| {
|
||||
name:
|
||||
| { name:
|
||||
( ( .key as $format
|
||||
| if has_section(.value; $fhelp) then "[\($format | code)](#\($format))"
|
||||
else $format | code
|
||||
end
|
||||
)
|
||||
+ " "
|
||||
),
|
||||
desc: (.value.description | nbsp),
|
||||
uses: "<sub>\((((.value.dependencies | flatten | map(code)) | join(" "))? // ""))</sub>"
|
||||
)
|
||||
, desc: (.value.description | nbsp)
|
||||
, uses: "<sub>\((((.value.dependencies | flatten | map(code)) | join(" "))? // ""))</sub>"
|
||||
}
|
||||
),
|
||||
( [ formats
|
||||
@ -47,10 +44,9 @@ def formats_table:
|
||||
]
|
||||
| reduce .[] as $e ({}; .[$e.key] += [$e.value])
|
||||
| to_entries[]
|
||||
| {
|
||||
name: ((.key | code) + " "),
|
||||
desc: "Group",
|
||||
uses: "<sub>\(((.value | map(code)) | join(" ")))</sub>"
|
||||
| { name: ((.key | code) + " ")
|
||||
, desc: "Group"
|
||||
, uses: "<sub>\(((.value | map(code)) | join(" ")))</sub>"
|
||||
}
|
||||
)
|
||||
]
|
||||
|
@ -47,11 +47,11 @@ def _to_jq($opts):
|
||||
);
|
||||
def to_jq($opts):
|
||||
_to_jq(
|
||||
( { indent: 0,
|
||||
key_sep: ":",
|
||||
object_sep: ",",
|
||||
array_sep: ",",
|
||||
compound_newline: "",
|
||||
( { indent: 0
|
||||
, key_sep: ":"
|
||||
, object_sep: ","
|
||||
, array_sep: ","
|
||||
, compound_newline: "",
|
||||
} + $opts
|
||||
| if .indent > 0 then
|
||||
( .key_sep = ": "
|
||||
@ -77,8 +77,8 @@ def from_jq:
|
||||
elif . == "TermTypeObject" then
|
||||
( $v.term.object.key_vals // []
|
||||
| map(
|
||||
{ key: (.key // .key_string.str),
|
||||
value: (.val.queries[0] | _f)
|
||||
{ key: (.key // .key_string.str)
|
||||
, value: (.val.queries[0] | _f)
|
||||
}
|
||||
)
|
||||
| from_entries
|
||||
|
@ -1,43 +1,42 @@
|
||||
def _esc: "\u001b";
|
||||
def _ansi_codes:
|
||||
{
|
||||
black: {set: "30", reset: "39"},
|
||||
red: {set: "31", reset: "39"},
|
||||
green: {set: "32", reset: "39"},
|
||||
yellow: {set: "33", reset: "39"},
|
||||
blue: {set: "34", reset: "39"},
|
||||
magenta: {set: "35", reset: "39"},
|
||||
cyan: {set: "36", reset: "39"},
|
||||
white: {set: "37", reset: "39"},
|
||||
brightblack: {set: "90", reset: "39"},
|
||||
brightred: {set: "91", reset: "39"},
|
||||
brightgreen: {set: "92", reset: "39"},
|
||||
brightyellow: {set: "93", reset: "39"},
|
||||
brightblue: {set: "94", reset: "39"},
|
||||
brightmagenta: {set: "95", reset: "39"},
|
||||
brightcyan: {set: "96", reset: "39"},
|
||||
brightwhite: {set: "97", reset: "39"},
|
||||
reset: {set: "39", reset: "39"},
|
||||
bgblack: {set: "40", reset: "49"},
|
||||
bgred: {set: "41", reset: "49"},
|
||||
bggreen: {set: "42", reset: "49"},
|
||||
bgyellow: {set: "43", reset: "49"},
|
||||
bgblue: {set: "44", reset: "49"},
|
||||
bgmagenta: {set: "45", reset: "49"},
|
||||
bgcyan: {set: "46", reset: "49"},
|
||||
bgwhite: {set: "47", reset: "49"},
|
||||
bgbrightblack: {set: "100", reset: "49"},
|
||||
bgbrightred: {set: "101", reset: "49"},
|
||||
bgbrightgreen: {set: "102", reset: "49"},
|
||||
bgbrightyellow: {set: "103", reset: "49"},
|
||||
bgbrightblue: {set: "104", reset: "49"},
|
||||
bgbrightmagenta: {set: "105", reset: "49"},
|
||||
bgbrightcyan: {set: "106", reset: "49"},
|
||||
bgbrightwhite: {set: "107", reset: "49"},
|
||||
bold: {set: "1", reset: "22"},
|
||||
italic: {set: "3", reset: "23"},
|
||||
underline: {set: "4", reset: "24"},
|
||||
inverse: {set: "7", reset: "27"}
|
||||
{ black: {set: "30", reset: "39"}
|
||||
, red: {set: "31", reset: "39"}
|
||||
, green: {set: "32", reset: "39"}
|
||||
, yellow: {set: "33", reset: "39"}
|
||||
, blue: {set: "34", reset: "39"}
|
||||
, magenta: {set: "35", reset: "39"}
|
||||
, cyan: {set: "36", reset: "39"}
|
||||
, white: {set: "37", reset: "39"}
|
||||
, brightblack: {set: "90", reset: "39"}
|
||||
, brightred: {set: "91", reset: "39"}
|
||||
, brightgreen: {set: "92", reset: "39"}
|
||||
, brightyellow: {set: "93", reset: "39"}
|
||||
, brightblue: {set: "94", reset: "39"}
|
||||
, brightmagenta: {set: "95", reset: "39"}
|
||||
, brightcyan: {set: "96", reset: "39"}
|
||||
, brightwhite: {set: "97", reset: "39"}
|
||||
, reset: {set: "39", reset: "39"}
|
||||
, bgblack: {set: "40", reset: "49"}
|
||||
, bgred: {set: "41", reset: "49"}
|
||||
, bggreen: {set: "42", reset: "49"}
|
||||
, bgyellow: {set: "43", reset: "49"}
|
||||
, bgblue: {set: "44", reset: "49"}
|
||||
, bgmagenta: {set: "45", reset: "49"}
|
||||
, bgcyan: {set: "46", reset: "49"}
|
||||
, bgwhite: {set: "47", reset: "49"}
|
||||
, bgbrightblack: {set: "100", reset: "49"}
|
||||
, bgbrightred: {set: "101", reset: "49"}
|
||||
, bgbrightgreen: {set: "102", reset: "49"}
|
||||
, bgbrightyellow: {set: "103", reset: "49"}
|
||||
, bgbrightblue: {set: "104", reset: "49"}
|
||||
, bgbrightmagenta: {set: "105", reset: "49"}
|
||||
, bgbrightcyan: {set: "106", reset: "49"}
|
||||
, bgbrightwhite: {set: "107", reset: "49"}
|
||||
, bold: {set: "1", reset: "22"}
|
||||
, italic: {set: "3", reset: "23"}
|
||||
, underline: {set: "4", reset: "24"}
|
||||
, inverse: {set: "7", reset: "27"}
|
||||
};
|
||||
|
||||
def _ansi_if($opts; $name):
|
||||
@ -56,6 +55,5 @@ def _ansi_if($opts; $name):
|
||||
end;
|
||||
|
||||
def _ansi:
|
||||
{
|
||||
clear_line: "\(_esc)[2K",
|
||||
{ clear_line: "\(_esc)[2K"
|
||||
};
|
||||
|
@ -3,13 +3,14 @@ include "query";
|
||||
|
||||
|
||||
def _eval_error($what; $error):
|
||||
error({
|
||||
what: $what,
|
||||
error: $error,
|
||||
column: 0,
|
||||
line: 1,
|
||||
filename: ""
|
||||
});
|
||||
error(
|
||||
{ what: $what
|
||||
, error: $error
|
||||
, column: 0
|
||||
, line: 1
|
||||
, filename: ""
|
||||
}
|
||||
);
|
||||
|
||||
def _eval_error_function_not_defined($name; $args):
|
||||
_eval_error(
|
||||
|
@ -108,12 +108,11 @@ def table(colmap; render):
|
||||
# TODO: rename keys and add more, ascii/utf8/utf16/codepoint name?, le/be, signed/unsigned?
|
||||
# TODO: move?
|
||||
def iprint:
|
||||
{
|
||||
bin: "0b\(to_radix(2))",
|
||||
oct: "0o\(to_radix(8))",
|
||||
dec: "\(.)",
|
||||
hex: "0x\(to_radix(16))",
|
||||
str: (try ([.] | implode) catch null),
|
||||
{ bin: "0b\(to_radix(2))"
|
||||
, oct: "0o\(to_radix(8))"
|
||||
, dec: "\(.)"
|
||||
, hex: "0x\(to_radix(16))"
|
||||
, str: (try ([.] | implode) catch null)
|
||||
};
|
||||
|
||||
# produce a/b pairs for diffing values
|
||||
|
@ -11,47 +11,47 @@ include "args";
|
||||
# TODO: variants, values, keywords?
|
||||
# TODO: store some other way?
|
||||
def _help_functions:
|
||||
{ length: {
|
||||
summary: "Length of string, array, object, etc",
|
||||
doc:
|
||||
{ length:
|
||||
{ summary: "Length of string, array, object, etc"
|
||||
, doc:
|
||||
"- For string number of unicode codepoints
|
||||
- For array number of elements in array
|
||||
- For object number of key-value pairs
|
||||
- For null zero
|
||||
- For number the number itself
|
||||
- For boolean is an error
|
||||
",
|
||||
examples:
|
||||
[ [[1,2,3], "length"]
|
||||
, ["abc", "length"]
|
||||
, [{a: 1, b: 2}, "length"]
|
||||
, [null, "length"]
|
||||
, [123, "length"]
|
||||
, [true, "length"]
|
||||
]
|
||||
},
|
||||
"..": {
|
||||
summary: "Recursive descent of .",
|
||||
doc:
|
||||
"
|
||||
, examples:
|
||||
[ [[1,2,3], "length"]
|
||||
, ["abc", "length"]
|
||||
, [{a: 1, b: 2}, "length"]
|
||||
, [null, "length"]
|
||||
, [123, "length"]
|
||||
, [true, "length"]
|
||||
]
|
||||
}
|
||||
, "..":
|
||||
{ summary: "Recursive descent of ."
|
||||
, doc:
|
||||
"Recursively descend . and output each value.
|
||||
Same as recurse without argument.
|
||||
",
|
||||
examples:
|
||||
[ ["a", ".."]
|
||||
, [[1,2,3], ".."]
|
||||
, [{a: 1, b: {c: 3}}, ".."]
|
||||
]
|
||||
},
|
||||
empty: {
|
||||
summary: "Output nothing",
|
||||
doc:
|
||||
"
|
||||
, examples:
|
||||
[ ["a", ".."]
|
||||
, [[1,2,3], ".."]
|
||||
, [{a: 1, b: {c: 3}}, ".."]
|
||||
]
|
||||
}
|
||||
, empty:
|
||||
{ summary: "Output nothing"
|
||||
, doc:
|
||||
"Output no value, not even null, and cause backtrack.
|
||||
",
|
||||
examples:
|
||||
[ ["empty"]
|
||||
, ["[1,empty,2]"]
|
||||
]
|
||||
}
|
||||
"
|
||||
, examples:
|
||||
[ ["empty"]
|
||||
, ["[1,empty,2]"]
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
def help($_): error("help must be alone or last in pipeline. ex: help(length) or ... | help");
|
||||
|
@ -143,13 +143,12 @@ def _cli_eval($expr; $opts):
|
||||
eval(
|
||||
$expr;
|
||||
( $opts
|
||||
+ {
|
||||
slurps: {
|
||||
help: "_help_slurp",
|
||||
repl: "_cli_repl_error",
|
||||
slurp: "_cli_slurp_error"
|
||||
},
|
||||
catch_query: _query_func("_cli_eval_on_expr_error"),
|
||||
+ { slurps:
|
||||
{ help: "_help_slurp"
|
||||
, repl: "_cli_repl_error"
|
||||
, slurp: "_cli_slurp_error"
|
||||
}
|
||||
, catch_query: _query_func("_cli_eval_on_expr_error"),
|
||||
}
|
||||
);
|
||||
_cli_eval_on_error;
|
||||
|
@ -4,114 +4,112 @@ include "binary";
|
||||
|
||||
def _opt_build_default_fixed:
|
||||
( stdout_tty as $stdout
|
||||
| {
|
||||
addrbase: 16,
|
||||
arg: [],
|
||||
argdecode: [],
|
||||
argjson: [],
|
||||
array_truncate: 50,
|
||||
bits_format: "string",
|
||||
# 0-0xff=brightwhite,0=brightblack,32-126:9-13=white
|
||||
byte_colors: [
|
||||
{ ranges: [[0,255]],
|
||||
value: "brightwhite"
|
||||
},
|
||||
{ ranges: [[0]],
|
||||
value: "brightblack"
|
||||
},
|
||||
{ ranges: [[32,126],[9,13]],
|
||||
value: "white"
|
||||
| { addrbase: 16
|
||||
, arg: []
|
||||
, argdecode: []
|
||||
, argjson: []
|
||||
, array_truncate: 50
|
||||
, bits_format: "string"
|
||||
# 0-0xff=brightwhite,0=brightblack,32-126:9-13=white
|
||||
, byte_colors:
|
||||
[ { ranges: [[0,255]]
|
||||
, value: "brightwhite"
|
||||
}
|
||||
, { ranges: [[0]]
|
||||
, value: "brightblack"
|
||||
}
|
||||
, { ranges: [[32,126],[9,13]]
|
||||
, value: "white"
|
||||
}
|
||||
]
|
||||
, color: ($stdout.is_terminal and (env.NO_COLOR | . == null or . == ""))
|
||||
, colors:
|
||||
{ null: "brightblack"
|
||||
, false: "yellow"
|
||||
, true: "yellow"
|
||||
, number: "cyan"
|
||||
, string: "green"
|
||||
, objectkey: "brightblue"
|
||||
, array: "white"
|
||||
, object: "white"
|
||||
, index: "white"
|
||||
, value: "white"
|
||||
, error: "brightred"
|
||||
, dumpheader: "yellow+underline"
|
||||
, dumpaddr: "yellow"
|
||||
, prompt_repl_level: "brightblack"
|
||||
, prompt_value: "white"
|
||||
}
|
||||
],
|
||||
color: ($stdout.is_terminal and (env.NO_COLOR | . == null or . == "")),
|
||||
colors: {
|
||||
null: "brightblack",
|
||||
false: "yellow",
|
||||
true: "yellow",
|
||||
number: "cyan",
|
||||
string: "green",
|
||||
objectkey: "brightblue",
|
||||
array: "white",
|
||||
object: "white",
|
||||
index: "white",
|
||||
value: "white",
|
||||
error: "brightred",
|
||||
dumpheader: "yellow+underline",
|
||||
dumpaddr: "yellow",
|
||||
prompt_repl_level: "brightblack",
|
||||
prompt_value: "white"
|
||||
},
|
||||
compact: false,
|
||||
completion_timeout: (env.COMPLETION_TIMEOUT | if . != null then tonumber else 1 end),
|
||||
decode_group: "probe",
|
||||
decode_progress: (env.NO_DECODE_PROGRESS == null),
|
||||
depth: 0,
|
||||
expr: ".",
|
||||
expr_given: false,
|
||||
expr_eval_path: "arg",
|
||||
expr_file: null,
|
||||
filenames: null,
|
||||
force: false,
|
||||
include_path: null,
|
||||
join_string: "\n",
|
||||
null_input: false,
|
||||
raw_file: [],
|
||||
raw_output: ($stdout.is_terminal | not),
|
||||
raw_string: false,
|
||||
repl: false,
|
||||
skip_gaps: false,
|
||||
sizebase: 10,
|
||||
show_formats: false,
|
||||
show_help: false,
|
||||
slurp: false,
|
||||
string_input: false,
|
||||
unicode: ($stdout.is_terminal and env.CLIUNICODE != null),
|
||||
value_output: false,
|
||||
verbose: false,
|
||||
, compact: false
|
||||
, completion_timeout: (env.COMPLETION_TIMEOUT | if . != null then tonumber else 1 end)
|
||||
, decode_group: "probe"
|
||||
, decode_progress: (env.NO_DECODE_PROGRESS == null)
|
||||
, depth: 0
|
||||
, expr: "."
|
||||
, expr_given: false
|
||||
, expr_eval_path: "arg"
|
||||
, expr_file: null
|
||||
, filenames: null
|
||||
, force: false
|
||||
, include_path: null
|
||||
, join_string: "\n"
|
||||
, null_input: false
|
||||
, raw_file: []
|
||||
, raw_output: ($stdout.is_terminal | not)
|
||||
, raw_string: false
|
||||
, repl: false
|
||||
, skip_gaps: false
|
||||
, sizebase: 10
|
||||
, show_formats: false
|
||||
, show_help: false
|
||||
, slurp: false
|
||||
, string_input: false
|
||||
, unicode: ($stdout.is_terminal and env.CLIUNICODE != null)
|
||||
, value_output: false
|
||||
, verbose: false
|
||||
}
|
||||
);
|
||||
|
||||
def _opt_options:
|
||||
{
|
||||
addrbase: "number",
|
||||
arg: "array_string_pair",
|
||||
argdecode: "array_string_pair",
|
||||
argjson: "array_string_pair",
|
||||
array_truncate: "number",
|
||||
bits_format: "string",
|
||||
byte_colors: "csv_ranges_array",
|
||||
color: "boolean",
|
||||
colors: "csv_kv_obj",
|
||||
compact: "boolean",
|
||||
completion_timeout: "number",
|
||||
decode_group: "string",
|
||||
decode_progress: "boolean",
|
||||
depth: "number",
|
||||
display_bytes: "number",
|
||||
expr: "string",
|
||||
expr_given: "boolean",
|
||||
expr_eval_path: "string",
|
||||
expr_file: "string",
|
||||
filenames: "array_string",
|
||||
force: "boolean",
|
||||
include_path: "string",
|
||||
join_string: "string",
|
||||
line_bytes: "number",
|
||||
null_input: "boolean",
|
||||
raw_file: "array_string_pair",
|
||||
raw_output: "boolean",
|
||||
raw_string: "boolean",
|
||||
repl: "boolean",
|
||||
show_formats: "boolean",
|
||||
show_help: "boolean",
|
||||
sizebase: "number",
|
||||
skip_gaps: "boolean",
|
||||
slurp: "boolean",
|
||||
string_input: "boolean",
|
||||
unicode: "boolean",
|
||||
value_output: "boolean",
|
||||
verbose: "boolean",
|
||||
width: "number",
|
||||
{ addrbase: "number"
|
||||
, arg: "array_string_pair"
|
||||
, argdecode: "array_string_pair"
|
||||
, argjson: "array_string_pair"
|
||||
, array_truncate: "number"
|
||||
, bits_format: "string"
|
||||
, byte_colors: "csv_ranges_array"
|
||||
, color: "boolean"
|
||||
, colors: "csv_kv_obj"
|
||||
, compact: "boolean"
|
||||
, completion_timeout: "number"
|
||||
, decode_group: "string"
|
||||
, decode_progress: "boolean"
|
||||
, depth: "number"
|
||||
, display_bytes: "number"
|
||||
, expr: "string"
|
||||
, expr_given: "boolean"
|
||||
, expr_eval_path: "string"
|
||||
, expr_file: "string"
|
||||
, filenames: "array_string"
|
||||
, force: "boolean"
|
||||
, include_path: "string"
|
||||
, join_string: "string"
|
||||
, line_bytes: "number"
|
||||
, null_input: "boolean"
|
||||
, raw_file: "array_string_pair"
|
||||
, raw_output: "boolean"
|
||||
, raw_string: "boolean"
|
||||
, repl: "boolean"
|
||||
, show_formats: "boolean"
|
||||
, show_help: "boolean"
|
||||
, sizebase: "number"
|
||||
, skip_gaps: "boolean"
|
||||
, slurp: "boolean"
|
||||
, string_input: "boolean"
|
||||
, unicode: "boolean"
|
||||
, value_output: "boolean"
|
||||
, verbose: "boolean"
|
||||
, width: "number"
|
||||
};
|
||||
|
||||
def _opt_eval($rest):
|
||||
@ -148,14 +146,14 @@ def _opt_eval($rest):
|
||||
)
|
||||
end
|
||||
)
|
||||
),
|
||||
color: (
|
||||
)
|
||||
, color: (
|
||||
if .monochrome_output == true then false
|
||||
elif .color_output == true then true
|
||||
else null
|
||||
end
|
||||
),
|
||||
expr: (
|
||||
)
|
||||
, expr: (
|
||||
# if -f was used, all rest non-args are filenames
|
||||
# otherwise first is expr rest is filenames
|
||||
( .expr_file
|
||||
@ -166,13 +164,13 @@ def _opt_eval($rest):
|
||||
else $rest[0] // null
|
||||
end
|
||||
)
|
||||
),
|
||||
expr_given: (
|
||||
)
|
||||
, expr_given: (
|
||||
# was a expr arg given
|
||||
$rest[0] != null
|
||||
),
|
||||
expr_eval_path: .expr_file,
|
||||
filenames: (
|
||||
)
|
||||
, expr_eval_path: .expr_file
|
||||
, filenames: (
|
||||
( if .filenames then .filenames
|
||||
elif .expr_file then $rest
|
||||
else $rest[1:]
|
||||
@ -180,14 +178,14 @@ def _opt_eval($rest):
|
||||
# null means stdin
|
||||
| if . == [] then [null] end
|
||||
)
|
||||
),
|
||||
join_string: (
|
||||
)
|
||||
, join_string: (
|
||||
if .join_output then ""
|
||||
elif .null_output then "\u0000"
|
||||
else null
|
||||
end
|
||||
),
|
||||
null_input: (
|
||||
)
|
||||
, null_input: (
|
||||
( ( if .expr_file then $rest
|
||||
else $rest[1:]
|
||||
end
|
||||
@ -196,8 +194,8 @@ def _opt_eval($rest):
|
||||
else null
|
||||
end
|
||||
)
|
||||
),
|
||||
raw_file: (
|
||||
)
|
||||
, raw_file: (
|
||||
( .raw_file
|
||||
| if . then
|
||||
( map(.[1] |=
|
||||
@ -209,21 +207,21 @@ def _opt_eval($rest):
|
||||
)
|
||||
end
|
||||
)
|
||||
),
|
||||
raw_string: (
|
||||
)
|
||||
, raw_string: (
|
||||
if .raw_string
|
||||
or .join_output
|
||||
or .null_output
|
||||
then true
|
||||
else null
|
||||
end
|
||||
),
|
||||
unicode: (
|
||||
)
|
||||
, unicode: (
|
||||
if .unicode_output == true then true
|
||||
else null
|
||||
end
|
||||
),
|
||||
value_output: (
|
||||
)
|
||||
, value_output: (
|
||||
if .value_output == true then true
|
||||
else null
|
||||
end
|
||||
@ -290,8 +288,8 @@ def _csv_ranges_to_array:
|
||||
( .[0]
|
||||
| split(":")
|
||||
| map(split("-") | map(tonumber))
|
||||
),
|
||||
value: .[1]
|
||||
)
|
||||
, value: .[1]
|
||||
}
|
||||
))
|
||||
);
|
||||
@ -381,144 +379,143 @@ def _opt_cli_arg_from_options:
|
||||
);
|
||||
|
||||
def _opt_cli_opts:
|
||||
{
|
||||
"arg": {
|
||||
long: "--arg",
|
||||
description: "Set variable $NAME to string VALUE",
|
||||
pairs: "NAME VALUE"
|
||||
},
|
||||
"argdecode": {
|
||||
long: "--argdecode",
|
||||
{ arg:
|
||||
{ long: "--arg"
|
||||
, description: "Set variable $NAME to string VALUE"
|
||||
, pairs: "NAME VALUE"
|
||||
}
|
||||
, argdecode:
|
||||
{ long: "--argdecode"
|
||||
# TODO: remove at some point
|
||||
aliases: ["--decode-file"],
|
||||
description: "Set variable $NAME to decode of PATH",
|
||||
pairs: "NAME PATH"
|
||||
},
|
||||
"argjson": {
|
||||
long: "--argjson",
|
||||
description: "Set variable $NAME to JSON",
|
||||
pairs: "NAME JSON"
|
||||
},
|
||||
"compact": {
|
||||
short: "-c",
|
||||
long: "--compact-output",
|
||||
description: "Compact output",
|
||||
bool: true
|
||||
},
|
||||
"color_output": {
|
||||
short: "-C",
|
||||
long: "--color-output",
|
||||
description: "Force color output",
|
||||
bool: true
|
||||
},
|
||||
"decode_group": {
|
||||
short: "-d",
|
||||
long: "--decode",
|
||||
description: "Decode format or group (probe)",
|
||||
string: "NAME"
|
||||
},
|
||||
"expr_file": {
|
||||
short: "-f",
|
||||
long: "--from-file",
|
||||
description: "Read EXPR from file",
|
||||
string: "PATH"
|
||||
},
|
||||
"show_help": {
|
||||
short: "-h",
|
||||
long: "--help",
|
||||
description: "Show help for TOPIC (ex: -h formats, -h mp4)",
|
||||
string: "[TOPIC]",
|
||||
optional: true
|
||||
},
|
||||
"join_output": {
|
||||
short: "-j",
|
||||
long: "--join-output",
|
||||
description: "No newline between outputs",
|
||||
bool: true
|
||||
},
|
||||
"include_path": {
|
||||
short: "-L",
|
||||
long: "--include-path",
|
||||
description: "Include search path",
|
||||
array: "PATH"
|
||||
},
|
||||
"null_output": {
|
||||
short: "-0",
|
||||
long: "--null-output",
|
||||
, aliases: ["--decode-file"]
|
||||
, description: "Set variable $NAME to decode of PATH"
|
||||
, pairs: "NAME PATH"
|
||||
}
|
||||
, argjson:
|
||||
{ long: "--argjson"
|
||||
, description: "Set variable $NAME to JSON"
|
||||
, pairs: "NAME JSON"
|
||||
}
|
||||
, compact:
|
||||
{ short: "-c"
|
||||
, long: "--compact-output"
|
||||
, description: "Compact output"
|
||||
, bool: true
|
||||
}
|
||||
, color_output:
|
||||
{ short: "-C"
|
||||
, long: "--color-output"
|
||||
, description: "Force color output"
|
||||
, bool: true
|
||||
}
|
||||
, decode_group:
|
||||
{ short: "-d"
|
||||
, long: "--decode"
|
||||
, description: "Decode format or group (probe)"
|
||||
, string: "NAME"
|
||||
}
|
||||
, expr_file:
|
||||
{ short: "-f"
|
||||
, long: "--from-file"
|
||||
, description: "Read EXPR from file"
|
||||
, string: "PATH"
|
||||
}
|
||||
, show_help:
|
||||
{ short: "-h"
|
||||
, long: "--help"
|
||||
, description: "Show help for TOPIC (ex: -h formats, -h mp4)"
|
||||
, string: "[TOPIC]"
|
||||
, optional: true
|
||||
}
|
||||
, join_output:
|
||||
{ short: "-j"
|
||||
, long: "--join-output"
|
||||
, description: "No newline between outputs"
|
||||
, bool: true
|
||||
}
|
||||
, include_path:
|
||||
{ short: "-L"
|
||||
, long: "--include-path"
|
||||
, description: "Include search path"
|
||||
, array: "PATH"
|
||||
}
|
||||
, null_output:
|
||||
{ short: "-0"
|
||||
, long: "--null-output"
|
||||
# for jq compatibility
|
||||
aliases: ["--nul-output"],
|
||||
description: "Null byte between outputs",
|
||||
bool: true
|
||||
},
|
||||
"null_input": {
|
||||
short: "-n",
|
||||
long: "--null-input",
|
||||
description: "Null input (use input and inputs functions to read)",
|
||||
bool: true
|
||||
},
|
||||
"monochrome_output": {
|
||||
short: "-M",
|
||||
long: "--monochrome-output",
|
||||
description: "Force monochrome output",
|
||||
bool: true
|
||||
},
|
||||
"option": {
|
||||
short: "-o",
|
||||
long: "--option",
|
||||
description: "Set option (ex: -o color=true, see --help options)",
|
||||
object: "KEY=VALUE/@PATH",
|
||||
},
|
||||
"string_input": {
|
||||
short: "-R",
|
||||
long: "--raw-input",
|
||||
description: "Read raw input strings (don't decode)",
|
||||
bool: true
|
||||
},
|
||||
"raw_file": {
|
||||
long: "--raw-file",
|
||||
, aliases: ["--nul-output"]
|
||||
, description: "Null byte between outputs"
|
||||
, bool: true
|
||||
}
|
||||
, null_input:
|
||||
{ short: "-n"
|
||||
, long: "--null-input"
|
||||
, description: "Null input (use input and inputs functions to read)"
|
||||
, bool: true
|
||||
}
|
||||
, monochrome_output:
|
||||
{ short: "-M"
|
||||
, long: "--monochrome-output"
|
||||
, description: "Force monochrome output"
|
||||
, bool: true
|
||||
}
|
||||
, option:
|
||||
{ short: "-o"
|
||||
, long: "--option"
|
||||
, description: "Set option (ex: -o color=true, see --help options)"
|
||||
, object: "KEY=VALUE/@PATH",
|
||||
}
|
||||
, string_input:
|
||||
{ short: "-R"
|
||||
, long: "--raw-input"
|
||||
, description: "Read raw input strings (don't decode)"
|
||||
, bool: true
|
||||
}
|
||||
, raw_file:
|
||||
{ long: "--raw-file"
|
||||
# for jq compatibility
|
||||
aliases: ["--raw-file"],
|
||||
description: "Set variable $NAME to string content of file",
|
||||
pairs: "NAME PATH"
|
||||
},
|
||||
"raw_string": {
|
||||
short: "-r",
|
||||
, aliases: ["--raw-file"]
|
||||
, description: "Set variable $NAME to string content of file"
|
||||
, pairs: "NAME PATH"
|
||||
}
|
||||
, raw_string:
|
||||
{ short: "-r"
|
||||
# for jq compat, is called raw string internally, is different from "raw output" which
|
||||
# is if we can output raw bytes or not
|
||||
long: "--raw-output",
|
||||
description: "Raw string output (without quotes)",
|
||||
bool: true
|
||||
},
|
||||
"repl": {
|
||||
short: "-i",
|
||||
long: "--repl",
|
||||
description: "Interactive REPL",
|
||||
bool: true
|
||||
},
|
||||
"slurp": {
|
||||
short: "-s",
|
||||
long: "--slurp",
|
||||
description: "Slurp all inputs into an array or string (-Rs)",
|
||||
bool: true
|
||||
},
|
||||
"unicode_output": {
|
||||
short: "-U",
|
||||
long: "--unicode-output",
|
||||
description: "Force unicode output",
|
||||
bool: true
|
||||
},
|
||||
"value_output": {
|
||||
short: "-V",
|
||||
long: "--value-output",
|
||||
description: "Output JSON value (-Vr for raw string)",
|
||||
bool: true
|
||||
},
|
||||
"show_version": {
|
||||
short: "-v",
|
||||
long: "--version",
|
||||
description: "Show version",
|
||||
bool: true
|
||||
},
|
||||
, long: "--raw-output"
|
||||
, description: "Raw string output (without quotes)"
|
||||
, bool: true
|
||||
}
|
||||
, repl:
|
||||
{ short: "-i"
|
||||
, long: "--repl"
|
||||
, description: "Interactive REPL"
|
||||
, bool: true
|
||||
}
|
||||
, slurp:
|
||||
{ short: "-s"
|
||||
, long: "--slurp"
|
||||
, description: "Slurp all inputs into an array or string (-Rs)"
|
||||
, bool: true
|
||||
}
|
||||
, unicode_output:
|
||||
{ short: "-U"
|
||||
, long: "--unicode-output"
|
||||
, description: "Force unicode output"
|
||||
, bool: true
|
||||
}
|
||||
, value_output:
|
||||
{ short: "-V"
|
||||
, long: "--value-output"
|
||||
, description: "Output JSON value (-Vr for raw string)"
|
||||
, bool: true
|
||||
}
|
||||
, show_version:
|
||||
{ short: "-v"
|
||||
, long: "--version"
|
||||
, description: "Show version"
|
||||
, bool: true
|
||||
},
|
||||
};
|
||||
|
||||
def options($opts):
|
||||
|
@ -4,20 +4,18 @@ def _query_null:
|
||||
|
||||
# . -> (.)
|
||||
def _query_query:
|
||||
{ term: {
|
||||
type: "TermTypeQuery",
|
||||
query: .
|
||||
}
|
||||
{ term:
|
||||
{ type: "TermTypeQuery"
|
||||
, query: .
|
||||
}
|
||||
};
|
||||
|
||||
# string
|
||||
def _query_string($str):
|
||||
{ term: {
|
||||
type: "TermTypeString",
|
||||
str: {
|
||||
str: $str
|
||||
{ term:
|
||||
{ type: "TermTypeString"
|
||||
, str: {str: $str}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
# .
|
||||
@ -31,13 +29,13 @@ def _query_func_rename(name):
|
||||
.term.func.name = name;
|
||||
# $name($args)
|
||||
def _query_func($name; $args):
|
||||
{ term: {
|
||||
type: "TermTypeFunc",
|
||||
func: {
|
||||
args: $args,
|
||||
name: $name
|
||||
{ term:
|
||||
{ type: "TermTypeFunc"
|
||||
, func:
|
||||
{ args: $args
|
||||
, name: $name
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
def _query_func($name):
|
||||
_query_func($name; null);
|
||||
@ -61,47 +59,44 @@ def _query_empty:
|
||||
|
||||
# l | r
|
||||
def _query_pipe(l; r):
|
||||
{ op: "|",
|
||||
left: l,
|
||||
right: r
|
||||
{ op: "|"
|
||||
, left: l
|
||||
, right: r
|
||||
};
|
||||
|
||||
# . -> [.]
|
||||
def _query_array:
|
||||
( . as $q
|
||||
| { term: {
|
||||
type: "TermTypeArray",
|
||||
array: {}
|
||||
}
|
||||
| { term:
|
||||
{ type: "TermTypeArray"
|
||||
, array: {}
|
||||
}
|
||||
}
|
||||
| if $q then .term.array.query = $q end
|
||||
);
|
||||
|
||||
# {} -> {}
|
||||
def _query_object:
|
||||
{ term: {
|
||||
object: {
|
||||
key_vals:
|
||||
( to_entries
|
||||
| map(
|
||||
{
|
||||
key: .key,
|
||||
val: {
|
||||
queries: [.value]
|
||||
{ term:
|
||||
{ object:
|
||||
{ key_vals:
|
||||
( to_entries
|
||||
| map(
|
||||
{ key: .key
|
||||
, val: {queries: [.value]}
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
},
|
||||
type: "TermTypeObject"
|
||||
}
|
||||
}
|
||||
, type: "TermTypeObject"
|
||||
}
|
||||
};
|
||||
|
||||
# l,r
|
||||
def _query_comma(l; r):
|
||||
{ left: l,
|
||||
op: ",",
|
||||
right: r
|
||||
{ left: l
|
||||
, op: ","
|
||||
, right: r
|
||||
};
|
||||
|
||||
# [1,2,3] -> 1,2,3
|
||||
@ -121,13 +116,13 @@ def _query_iter:
|
||||
|
||||
# try b catch c
|
||||
def _query_try(b; c):
|
||||
{ term: {
|
||||
type: "TermTypeTry",
|
||||
try: {
|
||||
body: b,
|
||||
catch: c
|
||||
{ term:
|
||||
{ type: "TermTypeTry"
|
||||
, try:
|
||||
{ body: b
|
||||
, catch: c
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
def _query_try(b):
|
||||
_query_try(b; null);
|
||||
@ -199,32 +194,38 @@ def _query_completion_type:
|
||||
| _query_last
|
||||
| if .index.name then
|
||||
{ query:
|
||||
($q | _query_transform_last(
|
||||
del(.term.suffix_list[-1])
|
||||
)),
|
||||
type: "index",
|
||||
prefix: .index.name
|
||||
( $q
|
||||
| _query_transform_last(
|
||||
del(.term.suffix_list[-1])
|
||||
)
|
||||
)
|
||||
, type: "index"
|
||||
, prefix: .index.name
|
||||
}
|
||||
elif .term.index.name then
|
||||
{ query:
|
||||
($q | _query_transform_last(
|
||||
_query_ident
|
||||
)),
|
||||
type: "index",
|
||||
prefix: .term.index.name
|
||||
( $q
|
||||
| _query_transform_last(
|
||||
_query_ident
|
||||
)
|
||||
)
|
||||
, type: "index"
|
||||
, prefix: .term.index.name
|
||||
}
|
||||
elif .term.func then
|
||||
{ query:
|
||||
($q | _query_transform_last(
|
||||
_query_ident
|
||||
)),
|
||||
type:
|
||||
( $q
|
||||
| _query_transform_last(
|
||||
_query_ident
|
||||
)
|
||||
)
|
||||
, type:
|
||||
( .term.func.name
|
||||
| if startswith("$") then "var"
|
||||
else "func"
|
||||
end
|
||||
),
|
||||
prefix: .term.func.name
|
||||
)
|
||||
, prefix: .term.func.name
|
||||
}
|
||||
else
|
||||
null
|
||||
|
@ -9,28 +9,27 @@ include "ansi";
|
||||
|
||||
# TODO: currently only make sense to allow keywords starting a term or directive
|
||||
def _complete_keywords:
|
||||
[
|
||||
"and",
|
||||
#"as",
|
||||
#"break",
|
||||
#"catch",
|
||||
"def",
|
||||
#"elif",
|
||||
#"else",
|
||||
#"end",
|
||||
"false",
|
||||
"foreach",
|
||||
"if",
|
||||
"import",
|
||||
"include",
|
||||
"label",
|
||||
"module",
|
||||
"null",
|
||||
"or",
|
||||
"reduce",
|
||||
#"then",
|
||||
"true",
|
||||
"try"
|
||||
[ "and"
|
||||
#"as"
|
||||
#"break"
|
||||
#"catch"
|
||||
, "def"
|
||||
#"elif"
|
||||
#"else"
|
||||
#"end"
|
||||
, "false"
|
||||
, "foreach"
|
||||
, "if"
|
||||
, "import"
|
||||
, "include"
|
||||
, "label"
|
||||
, "module"
|
||||
, "null"
|
||||
, "or"
|
||||
, "reduce"
|
||||
#"then"
|
||||
, "true"
|
||||
, "try"
|
||||
];
|
||||
|
||||
def _complete_scope:
|
||||
@ -72,9 +71,8 @@ def _complete($line; $cursor_pos):
|
||||
else error("unreachable")
|
||||
end
|
||||
) as {$type, $query, $prefix}
|
||||
| {
|
||||
prefix: $prefix,
|
||||
names: (
|
||||
| { prefix: $prefix
|
||||
, names: (
|
||||
if $type == "none" then
|
||||
( $c
|
||||
| _query_index_or_key($line_query)
|
||||
@ -225,16 +223,16 @@ def _repl_eval($expr; on_error; on_compile_error):
|
||||
eval(
|
||||
$expr;
|
||||
{ slurps:
|
||||
{ repl: "_repl_slurp",
|
||||
help: "_help_slurp",
|
||||
slurp: "_slurp"
|
||||
},
|
||||
{ repl: "_repl_slurp"
|
||||
, help: "_help_slurp"
|
||||
, slurp: "_slurp"
|
||||
}
|
||||
# input to repl is always array of values to iterate
|
||||
input_query: (_query_ident | _query_iter), # .[]
|
||||
, input_query: (_query_ident | _query_iter) # .[]
|
||||
# each input should be evaluated separately like cli file args, so catch and just print errors
|
||||
catch_query: _query_func("_repl_on_expr_error"),
|
||||
, catch_query: _query_func("_repl_on_expr_error")
|
||||
# run display in sub eval so it can be interrupted
|
||||
output_query: _query_func("_repl_display")
|
||||
, output_query: _query_func("_repl_display")
|
||||
};
|
||||
on_error;
|
||||
on_compile_error
|
||||
@ -246,11 +244,12 @@ def _repl($opts):
|
||||
def _read_expr:
|
||||
_repeat_break(
|
||||
# both _prompt and _complete want input arrays
|
||||
( _readline({
|
||||
prompt: _prompt(options($opts)),
|
||||
complete: "_complete",
|
||||
timeout: options.completion_timeout
|
||||
})
|
||||
( _readline(
|
||||
{ prompt: _prompt(options($opts))
|
||||
, complete: "_complete"
|
||||
, timeout: options.completion_timeout
|
||||
}
|
||||
)
|
||||
| if trim == "" then empty
|
||||
else (., error("break"))
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user