main
 1#!/usr/bin/env bash
 2# Search org files for raffi script filter.
 3# Outputs Alfred Script Filter JSON format.
 4# Usage: org-search-raffi <query>
 5set -euo pipefail
 6
 7query="${1:-}"
 8ORG_DIR="${HOME}/desktop/org"
 9
10if [[ -z "$query" ]]; then
11    jq -n '{"items": [{"title": "Type to search org files…", "subtitle": "Searches ~/desktop/org/", "arg": ""}]}'
12    exit 0
13fi
14
15items="[]"
16while IFS=: read -r file lineno content; do
17    [[ -z "$file" ]] && continue
18    relpath="${file/#"$ORG_DIR"\//}"
19    content_trimmed=$(echo "$content" | sed 's/^[[:space:]]*//' | head -c 100)
20    items=$(jq --arg title "$content_trimmed" --arg sub "$relpath:$lineno" --arg arg "${file}" \
21        '. + [{"title": $title, "subtitle": $sub, "arg": $arg}]' <<< "$items")
22done < <(rg --no-heading --line-number --max-count 30 -i "$query" "$ORG_DIR" --glob '*.org' 2>/dev/null | head -30)
23
24if [[ "$items" == "[]" ]]; then
25    jq -n --arg q "$query" '{"items": [{"title": "No results", "subtitle": "No matches for \"\($q)\" in org files", "arg": ""}]}'
26    exit 0
27fi
28
29jq -n --argjson items "$items" '{"items": $items}'