Skip to content

Instantly share code, notes, and snippets.

@cheeseonamonkey
Last active August 13, 2023 20:41
Show Gist options
  • Select an option

  • Save cheeseonamonkey/93421f008ed7911acea648874a2891f4 to your computer and use it in GitHub Desktop.

Select an option

Save cheeseonamonkey/93421f008ed7911acea648874a2891f4 to your computer and use it in GitHub Desktop.
Get Dirs Context
function print_files() {
# Check for help flag
if [[ $1 == "-h" || $1 == "--help" ]]; then
printf "Usage: print_files [-f filter] [regex]\n"
printf "Options:\n"
printf " -f filter Select files with fzf using the specified filter\n"
printf " -h Show this help message\n"
return
fi
# Check for fzf flag
if [[ $1 == "-f" ]]; then
filter="*$2"
shift 2
# Use fzf to select files with the specified filter
selected_files=$(find . -type f -name "$filter" -print0 | fzf --multi --read0)
else
# Use all files if no parameter provided
selected_files=$(find . -type f -print0)
fi
# Check if any files are selected
if [[ -z $selected_files ]]; then
printf "No files found.\n"
return
fi
# Check if regex is provided
if [[ -n $1 ]]; then
regex=$1
selected_files=$(printf "%s" "$selected_files" | grep -z $regex)
fi
# Loop over selected files
printf "%s" "$selected_files" | while IFS= read -r -d '' file; do
printf "### %s:\n" "$file"
printf '```\n'
cat "$file"
printf '```\n'
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment