main
 1#!/usr/bin/env bash
 2# Search man pages for raffi script filter.
 3# Scans manpath directories directly (no apropos DB needed on NixOS).
 4# Outputs Alfred Script Filter JSON format.
 5# Usage: man-raffi [query]
 6set -euo pipefail
 7
 8query="${*:-}"
 9
10if [[ -z "$query" ]]; then
11    jq -n '{"items": [{"title": "Type to search man pages…", "subtitle": "Search manual pages", "arg": ""}]}'
12    exit 0
13fi
14
15# Convert spaces to wildcards so "git commit" matches "git-commit"
16pattern="${query// /*}"
17
18items="[]"
19count=0
20while IFS= read -r path; do
21    [[ -z "$path" ]] && continue
22    file=$(basename "$path")
23    # Strip compression extensions and extract name.section
24    name="${file%.gz}"
25    name="${name%.zst}"
26    name="${name%.bz2}"
27    name="${name%.xz}"
28    section="${name##*.}"
29    name="${name%.*}"
30    items=$(jq --arg title "${name}(${section})" --arg sub "$path" --arg arg "$name" \
31        '. + [{"title": $title, "subtitle": $sub, "arg": $arg}]' <<< "$items")
32    count=$((count + 1))
33    [[ $count -ge 30 ]] && break
34man_dirs=(/run/current-system/sw/share/man "/etc/profiles/per-user/${USER}/share/man")
35while IFS= read -r d; do
36    man_dirs+=("$d")
37done < <(manpath 2>/dev/null | tr ':' '\n')
38
39done < <(find -L "${man_dirs[@]}" -type f -iname "*${pattern}*" 2>/dev/null | sort -u)
40
41if [[ "$items" == "[]" ]]; then
42    jq -n --arg q "$query" '{"items": [{"title": "No results", "subtitle": "No man pages matching \"\($q)\"", "arg": ""}]}'
43    exit 0
44fi
45
46jq -n --argjson items "$items" '{"items": $items}'