Skip to content

Commit 2877a27

Browse files
committed
tools: add fzf wrapping script
introduce iretis.sh (interactive retis) that essentially performs some minor checks and make retis fzf and bat play nice together. Signed-off-by: Paolo Valerio <pvalerio@redhat.com>
1 parent 971e6ba commit 2877a27

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

docs/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,28 @@ Output can then be piped through bat directly:
269269
$ retis print retis.data | bat --language retis
270270
```
271271

272+
## Interactive filtering with fzf
273+
274+
[`iretis.sh`](https://raw.githubusercontent.com/retis-org/retis/main/tools/iretis.sh)
275+
wraps `retis` and pipes the output into [fzf](https://github.com/junegunn/fzf)
276+
for interactive multi-event browsing and filtering. Set `COLORS=1` to enable
277+
syntax highlighting via [bat](https://github.com/sharkdp/bat) at the same time:
278+
279+
```none
280+
$ COLORS=1 iretis.sh collect ...
281+
$ COLORS=1 iretis.sh sort retis.data
282+
$ COLORS=1 iretis.sh print retis.data
283+
```
284+
285+
`RETIS_BIN` can be set to point to a specific retis binary (default: `retis`
286+
from `PATH`), which is useful when running a locally built binary or with
287+
[`retis_in_container.sh`](https://raw.githubusercontent.com/retis-org/retis/main/tools/retis_in_container.sh).
288+
289+
`ERR_LOG` can be set to a file path where retis stderr is captured and printed
290+
after fzf exits, keeping the fzf interface uncluttered. If set, stderr is also
291+
saved to the specified file:
292+
293+
```none
294+
$ ERR_LOG=/tmp/retis-err.log iretis.sh collect ...
295+
```
296+

tools/iretis.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
# iretis: run retis and interactively browse events with fzf.
3+
#
4+
# Usage: iretis.sh <collect|sort|print> [retis args...]
5+
#
6+
# Environment variables:
7+
# RETIS_BIN path to the retis binary (default: retis from PATH)
8+
# COLORS set to 1 to pipe output through bat for syntax highlighting
9+
# ERR_LOG path to a file where retis stderr is saved in addition to
10+
# being printed after fzf exits (default: not set)
11+
#
12+
# Note:
13+
# If you built bat cache on custom locations (e.g. you run collect with
14+
# sudo and your profile was built in the user's homedir) use
15+
# BAT_CACHE_PATH=$HOME/.cache/bat
16+
# Use BAT_THEME=<theme> to use a non-default color scheme.
17+
18+
set -e
19+
20+
RETIS_BIN=$(command -v "${RETIS_BIN:-retis}") \
21+
|| { echo "Error: retis not found in PATH or RETIS_BIN"; exit 2; }
22+
23+
_ERR_TMP=$(mktemp)
24+
25+
cleanup() {
26+
[[ -s "$_ERR_TMP" ]] && cat "$_ERR_TMP" >&2
27+
[[ -n "$ERR_LOG" ]] && mv "$_ERR_TMP" "$ERR_LOG" || rm -f "$_ERR_TMP"
28+
}
29+
trap cleanup EXIT
30+
31+
command -v fzf >/dev/null 2>&1 \
32+
|| { echo "Error: fzf not found in PATH"; exit 2; }
33+
34+
if [[ "${COLORS:-0}" -eq 1 ]]; then
35+
command -v bat >/dev/null 2>&1 \
36+
|| { echo "Error: bat not found in PATH (required for COLORS=1)"; exit 2; }
37+
fi
38+
39+
if [[ $# -lt 1 ]]; then
40+
echo "Usage: iretis.sh <collect|sort|print> [retis args...]"
41+
exit 2
42+
fi
43+
44+
_SUBCMD=""
45+
for arg in "$@"; do
46+
case "$arg" in
47+
collect|sort|print)
48+
_SUBCMD="$arg"
49+
break
50+
;;
51+
esac
52+
done
53+
54+
if [[ -z "$_SUBCMD" ]]; then
55+
echo "Error: one of collect, sort or print must be provided in the extended format"
56+
exit 2
57+
fi
58+
59+
fzf_cmd="fzf --read0 --multi-line --ansi --no-sort --tac"
60+
61+
if [[ "${COLORS:-0}" -eq 1 ]]; then
62+
"$RETIS_BIN" "$@" 2> $_ERR_TMP \
63+
| bat --language retis --color always --paging never -pp \
64+
| awk 'BEGIN{RS=""; ORS="\0"} {print; fflush()}' \
65+
| $fzf_cmd
66+
else
67+
"$RETIS_BIN" "$@" 2> $_ERR_TMP \
68+
| awk 'BEGIN{RS=""; ORS="\0"} {print; fflush()}' \
69+
| $fzf_cmd
70+
fi

0 commit comments

Comments
 (0)