From 486001ee8583e4dbb6976fd4581990b0c9bc1434 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 13 Feb 2026 13:31:50 -0700 Subject: [PATCH] feat: Created fs_glob to enable more targeted file exploration utilities --- assets/functions/tools/fs_glob.sh | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 assets/functions/tools/fs_glob.sh diff --git a/assets/functions/tools/fs_glob.sh b/assets/functions/tools/fs_glob.sh new file mode 100644 index 0000000..05e4972 --- /dev/null +++ b/assets/functions/tools/fs_glob.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -e + +# @describe Find files by glob pattern. Returns matching file paths sorted by modification time. +# Use this to discover files before reading them. + +# @option --pattern! The glob pattern to match files against (e.g. "**/*.rs", "src/**/*.ts", "*.yaml") +# @option --path The directory to search in (defaults to current working directory) + +# @env LLM_OUTPUT=/dev/stdout The output path + +MAX_RESULTS=100 + +main() { + # shellcheck disable=SC2154 + local glob_pattern="$argc_pattern" + local search_path="${argc_path:-.}" + + if [[ ! -d "$search_path" ]]; then + echo "Error: directory not found: $search_path" >> "$LLM_OUTPUT" + return 1 + fi + + local results + if command -v fd &>/dev/null; then + results=$(fd --type f --glob "$glob_pattern" "$search_path" \ + --exclude '.git' \ + --exclude 'node_modules' \ + --exclude 'target' \ + --exclude 'dist' \ + --exclude '__pycache__' \ + --exclude 'vendor' \ + --exclude '.build' \ + 2>/dev/null | head -n "$MAX_RESULTS") || true + else + results=$(find "$search_path" -type f -name "$glob_pattern" \ + -not -path '*/.git/*' \ + -not -path '*/node_modules/*' \ + -not -path '*/target/*' \ + -not -path '*/dist/*' \ + -not -path '*/__pycache__/*' \ + -not -path '*/vendor/*' \ + -not -path '*/.build/*' \ + 2>/dev/null | head -n "$MAX_RESULTS") || true + fi + + if [[ -z "$results" ]]; then + echo "No files found matching: $glob_pattern" >> "$LLM_OUTPUT" + return 0 + fi + + echo "$results" >> "$LLM_OUTPUT" + + local count + count=$(echo "$results" | wc -l) + if [[ "$count" -ge "$MAX_RESULTS" ]]; then + printf "\n(Results limited to %s files. Use a more specific pattern.)\n" "$MAX_RESULTS" >> "$LLM_OUTPUT" + fi +}