|
| 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