Skip to content

Commit

Permalink
fq,cli: Rename chunk to streaks, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 285356d commit 25f5ad7
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 101 deletions.
2 changes: 1 addition & 1 deletion dev/format_summary.jq
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def recurse_depth(f; cond):
}
)
]
| chunk_by(.norm_path)
| streaks_by(.norm_path)
| map(.[0] + {count: length})
| .[]
| [ if .depth > 0 then " "*.depth else empty end
Expand Down
11 changes: 6 additions & 5 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,22 @@ notable is support for arbitrary-precision integers.
### Functions

- All standard library functions from jq
- `chunk/1`, `chunk_by/1` like `group` but groups consecutively on condition.
- `count_by` like `group_by` but counts groups lengths.
- `streaks/0`, `streaks_by/1` like `group` but groups streaks based on condition.
- `count`, `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}) | ...`.
- `path_to_expr` from `["key", 1]` to `".key[1]"`.
- `expr_to_path` from `".key[1]"` to `["key", 1]`.
- `diff/2` produce diff object between two values.
- `delta`, `delta_by/1`, array with difference between all consecutive pairs.
- `open` open file for reading
- `probe` or `decode` try to automatically detect format and decode
- `mp3`, `matroska`, ..., `<name>`, `decode([name])` try decode as format
- `probe` or `decode` probe format and decode
- `mp3`, `matroska`, ..., `<name>`, `decode([name])` force decode as format
- `d`/`display` display value and truncate long arrays
- `f`/`full` display value and don't truncate arrays
- `v`/`verbose` display value verbosely and don't truncate array
- `p`/`preview` show preview of field tree
- `hd`/`hexdump` hexdump value
- `repl` nested REPL
- `repl` nested REPL, must be last. `1 | repl` or iterable `1, 2, 3 | repl`.

### Decoded values (TODO: better name?)

Expand Down
8 changes: 4 additions & 4 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def trim: capture("^\\s*(?<str>.*?)\\s*$"; "").str;
# does +1 and [:1] as " "*0 is null
def rpad($s; $w): . + ($s * ($w+1-length))[1:];

# like `group` but groups consecutively on condition
def chunk_by(f):
# like group but groups streaks based on condition
def streaks_by(f):
( . as $a
| length as $l
| if $l == 0 then []
Expand Down Expand Up @@ -73,9 +73,9 @@ def chunk_by(f):
end
);
# [1, 2, 2, 3] => [[1], [2, 2], [3]]
def chunk: chunk_by(.);
def streaks: streaks_by(.);

# same as group_by but counts
# same as group_by but counts, array or pairs with [value, count]
def count_by(exp):
group_by(exp) | map([(.[0] | exp), length]);
def count: count_by(.);
Expand Down
4 changes: 2 additions & 2 deletions pkg/interp/funcs_test.jq
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ include "funcs";
[[1,1,2], [[1,1],[2]]],
[[1,1,2,2], [[1,1],[2,2]]],
[[1,2,2,1], [[1],[2,2],[1]]]
][] | assert("\(.) | chunk"; .[1]; .[0] | chunk))
][] | assert("\(.) | streaks"; .[1]; .[0] | streaks))
,
([
[[{a:1},{a:1},{a:2}], [[{a:1},{a:1}],[{a:2}]]]
][] | assert("\(.) | chunk_by"; .[1]; .[0] | chunk_by(.a)))
][] | assert("\(.) | streaks_by"; .[1]; .[0] | streaks_by(.a)))
)
5 changes: 4 additions & 1 deletion pkg/interp/internal.jq
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def _finally(f; fin):

def _repeat_break(f):
try repeat(f)
catch if . == "break" then empty else error end;
catch
if . == "break" then empty
else error
end;

def _error_str: "error: \(.)";
def _errorln: ., "\n" | stderr;
Expand Down
31 changes: 7 additions & 24 deletions pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -419,24 +419,6 @@ def input:
# iterate all valid inputs
def inputs: _repeat_break(input);

def _inputs:
( options as $opts
| if $opts.null_input then null
elif $opts.string_input then inputs
# ( [inputs]
# | join("")
# | if $opts.slurp then .
# else
# ( rtrimstr("\n")
# | split("\n")[]
# )
# end
# )
elif $opts.slurp then [inputs]
else inputs
end
);

def input_filename: _input_filename;

def var: _variables;
Expand All @@ -450,17 +432,18 @@ def var($k): . as $c | var($k; $c);

# TODO: introspect and show doc, reflection somehow?
def help:
( builtins[]
, "^C interrupt"
, "^D exit REPL"
( "Type jq expression to evaluate"
, "\\t Auto completion"
, "Up/Down History"
, "^C Interrupt execution"
, "^D Exit REPL"
) | println;

def _main:
def _formats_list:
[ ["Name:", "Description:"]
, ( formats
[ ( formats
| to_entries[]
| [(.key+" "), .value.description]
| [(.key+" "), .value.description]
)
]
| table(
Expand Down
127 changes: 63 additions & 64 deletions pkg/interp/testdata/args.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -48,70 +48,69 @@ $ fq -n "[1,2,3]"
$ fq -nc "[1,2,3]"
[1,2,3]
$ fq --formats
Name: Description:
aac_frame Advanced Audio Coding frame
adts Audio Data Transport Stream
adts_frame Audio Data Transport Stream frame
apev2 APEv2 metadata tag
av1_ccr AV1 Codec Configuration Record
av1_frame AV1 frame
av1_obu AV1 Open Bitstream Unit
avc_annexb H.264/AVC Annex B
avc_au H.264/AVC Access Unit
avc_dcr H.264/AVC Decoder Configuration Record
avc_nalu H.264/AVC Network Access Layer Unit
avc_pps H.264/AVC Picture Parameter Set
avc_sei H.264/AVC Supplemental Enhancement Information
avc_sps H.264/AVC Sequence Parameter Set
bzip2 bzip2 compression
dns DNS packet
elf Executable and Linkable Format
exif Exchangeable Image File Format
flac Free Lossless Audio Codec file
flac_frame FLAC frame
flac_metadatablock FLAC metadatablock
flac_picture FLAC metadatablock picture
gif Graphics Interchange Format
gzip gzip compression
hevc_annexb H.265/HEVC Annex B
hevc_au H.265/HEVC Access Unit
hevc_dcr H.265/HEVC Decoder Configuration Record
hevc_nalu H.265/HEVC Network Access Layer Unit
icc_profile International Color Consortium profile
id3v1 ID3v1 metadata
id3v11 ID3v1.1 metadata
id3v2 ID3v2 metadata
jpeg Joint Photographic Experts Group file
json JSON
matroska Matroska file
mp3 MP3 file
mp3_frame MPEG audio layer 3 frame
mp4 MPEG-4 file and similar
mpeg_asc MPEG-4 Audio Specific Config
mpeg_es MPEG Elementary Stream
mpeg_pes MPEG Packetized elementary stream
mpeg_pes_packet MPEG Packetized elementary stream packet
mpeg_spu Sub Picture Unit (DVD subtitle)
mpeg_ts MPEG Transport Stream
ogg OGG file
ogg_page OGG page
opus_packet Opus packet
png Portable Network Graphics file
protobuf Protobuf
protobuf_widevine Widevine protobuf
pssh_playready PlayReady PSSH
raw Raw bits
tar Tar archive
tiff Tag Image File Format
vorbis_comment Vorbis comment
vorbis_packet Vorbis packet
vp8_frame VP8 frame
vp9_cfm VP9 Codec Feature Metadata
vp9_frame VP9 frame
vpx_ccr VPX Codec Configuration Record
wav WAV file
webp WebP image
xing Xing header
aac_frame Advanced Audio Coding frame
adts Audio Data Transport Stream
adts_frame Audio Data Transport Stream frame
apev2 APEv2 metadata tag
av1_ccr AV1 Codec Configuration Record
av1_frame AV1 frame
av1_obu AV1 Open Bitstream Unit
avc_annexb H.264/AVC Annex B
avc_au H.264/AVC Access Unit
avc_dcr H.264/AVC Decoder Configuration Record
avc_nalu H.264/AVC Network Access Layer Unit
avc_pps H.264/AVC Picture Parameter Set
avc_sei H.264/AVC Supplemental Enhancement Information
avc_sps H.264/AVC Sequence Parameter Set
bzip2 bzip2 compression
dns DNS packet
elf Executable and Linkable Format
exif Exchangeable Image File Format
flac Free Lossless Audio Codec file
flac_frame FLAC frame
flac_metadatablock FLAC metadatablock
flac_picture FLAC metadatablock picture
gif Graphics Interchange Format
gzip gzip compression
hevc_annexb H.265/HEVC Annex B
hevc_au H.265/HEVC Access Unit
hevc_dcr H.265/HEVC Decoder Configuration Record
hevc_nalu H.265/HEVC Network Access Layer Unit
icc_profile International Color Consortium profile
id3v1 ID3v1 metadata
id3v11 ID3v1.1 metadata
id3v2 ID3v2 metadata
jpeg Joint Photographic Experts Group file
json JSON
matroska Matroska file
mp3 MP3 file
mp3_frame MPEG audio layer 3 frame
mp4 MPEG-4 file and similar
mpeg_asc MPEG-4 Audio Specific Config
mpeg_es MPEG Elementary Stream
mpeg_pes MPEG Packetized elementary stream
mpeg_pes_packet MPEG Packetized elementary stream packet
mpeg_spu Sub Picture Unit (DVD subtitle)
mpeg_ts MPEG Transport Stream
ogg OGG file
ogg_page OGG page
opus_packet Opus packet
png Portable Network Graphics file
protobuf Protobuf
protobuf_widevine Widevine protobuf
pssh_playready PlayReady PSSH
raw Raw bits
tar Tar archive
tiff Tag Image File Format
vorbis_comment Vorbis comment
vorbis_packet Vorbis packet
vp8_frame VP8 frame
vp9_cfm VP9 Codec Feature Metadata
vp9_frame VP9 frame
vpx_ccr VPX Codec Configuration Record
wav WAV file
webp WebP image
xing Xing header
$ fq --options
{
"addrbase": 16,
Expand Down

0 comments on commit 25f5ad7

Please sign in to comment.