ollie/data/tools/gui_files

70 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# ollie:parallel read
# args_json: {"type":"object","required":["query"],"properties":{"query":{"type":"string","description":"Search query"},"type":{"type":"string","description":"Filter by type: document|image|video|audio|folder"},"path":{"type":"string","description":"Search directory path"},"limit":{"type":"string","description":"Max results (default: 50)"}}}
# ollie:prompt
# ## gui_files
#
# Search files using Baloo (KDE file indexer).
#
# ```
# gui_files(query="project report", type="document")
# ```
# ollie:end
set -euo pipefail
source "$(dirname "$0")/_lib/args.sh"
query=$(arg_require "query")
file_type=$(arg_get "type" "")
path_filter=$(arg_get "path" "")
limit=$(arg_get "limit" "50")
# Build baloosearch command
cmd=(baloosearch)
case "$file_type" in
document|doc) cmd+=(-t Document) ;;
image|img) cmd+=(-t Image) ;;
video|vid) cmd+=(-t Video) ;;
audio|snd) cmd+=(-t Audio) ;;
folder|dir) cmd+=(-t Folder) ;;
"") ;;
*) cmd+=(-t "$file_type") ;;
esac
if [ -n "$path_filter" ]; then
cmd+=(-d "$path_filter")
fi
cmd+=(-l "$limit")
cmd+=("$query")
if command -v baloosearch &>/dev/null; then
"${cmd[@]}" 2>/dev/null
exit $?
fi
if command -v baloosearch6 &>/dev/null; then
cmd[0]="baloosearch6"
"${cmd[@]}" 2>/dev/null
exit $?
fi
if gdbus introspect --session --dest org.kde.baloo --object-path /org/kde/baloo &>/dev/null 2>&1; then
result=$(gdbus call --session \
--dest org.kde.baloo \
--object-path /org/kde/baloo \
--method org.kde.baloo.main.search "$query" "$limit" 2>/dev/null)
echo "$result" | sed "s/^(\[//;s/\],)$//;s/'//g;s/, /\n/g"
exit 0
fi
if command -v locate &>/dev/null; then
echo "# (using locate fallback - results may be stale)" >&2
locate -i "$query" | head -n "$limit"
exit 0
fi
echo "ERROR: Baloo not available and no fallback found" >&2
exit 1