@@ -268,6 +268,12 @@ functions_browser() {
268268# EOF
269269# }
270270
271+
272+ # #################################################################################################################################
273+ # #################################################################################################################################
274+ # #################################################################################################################################
275+
276+
271277if [[ -n $BASH_VERSION ]]; then
272278 functions () {
273279 local func_name
@@ -330,4 +336,92 @@ if [[ -n $BASH_VERSION ]]; then
330336 echo
331337 done | less -R
332338 }
333- fi
339+ fi
340+
341+
342+ # #################################################################################################################################
343+ # #################################################################################################################################
344+ # #################################################################################################################################
345+
346+ # Function selector with Bash and Zsh support
347+ # Usage: fsel [-e|--exec] [-h|--help]
348+
349+ fsel () {
350+ local exec_mode=false
351+
352+ case " $1 " in
353+ -h|--help)
354+ echo " Usage: fsel [-e|--exec] [-h|--help]"
355+ echo " "
356+ echo " Select and show/execute shell functions interactively."
357+ echo " "
358+ echo " Options:"
359+ echo " -e, --exec Execute selected function"
360+ echo " -h, --help Show this help"
361+ echo " "
362+ echo " Examples:"
363+ echo " fsel Select and show function code"
364+ echo " fsel -e Select and execute function"
365+ return 0
366+ ;;
367+ -e|--exec)
368+ exec_mode=true
369+ ;;
370+ esac
371+
372+ # Check if fzf is installed
373+ if ! command -v fzf & > /dev/null; then
374+ echo " fzf is not installed. Install it with:" >&2
375+ echo " apt install fzf # Debian/Ubuntu" >&2
376+ echo " brew install fzf # macOS" >&2
377+ return 1
378+ fi
379+
380+ # Create temp file with function definitions
381+ local tmpfile
382+ tmpfile=$( mktemp /tmp/fsel_functions_XXXXXX)
383+
384+ # Detect shell and dump functions
385+ if [[ -n " $ZSH_VERSION " ]]; then
386+ for func in ${(ok)functions} ; do
387+ echo " ###FUNC:$func "
388+ print -r -- " $functions [$func ]"
389+ echo " ###END:$func "
390+ done | tee " $tmpfile " > /dev/null
391+ elif [[ -n " $BASH_VERSION " ]]; then
392+ declare -F | awk ' {print $3}' | sort | while IFS= read -r func; do
393+ echo " ###FUNC:$func "
394+ declare -f " $func " | tail -n +2
395+ echo " ###END:$func "
396+ done | tee " $tmpfile " > /dev/null
397+ else
398+ echo " Unsupported shell" >&2
399+ rm -f " $tmpfile "
400+ return 1
401+ fi
402+
403+ # Extract function names for fzf
404+ local func_names
405+ func_names=$( grep -a " ^###FUNC:" " $tmpfile " | sed ' s/^###FUNC://' )
406+
407+ # Select with fzf using the temp file for preview
408+ local selected
409+ selected=$( echo " $func_names " | fzf \
410+ --preview=" sed -n '/^###FUNC:{}$/,/^###END:{}$/p' \" $tmpfile \" | sed '1d;\$ d'" \
411+ --preview-window=right:60%:wrap)
412+
413+ rm -f " $tmpfile "
414+
415+ [[ -z " $selected " ]] && return 1
416+
417+ # Show or execute
418+ if $exec_mode ; then
419+ eval " $selected "
420+ else
421+ if [[ -n " $ZSH_VERSION " ]]; then
422+ print -r -- " $functions [$selected ]"
423+ else
424+ declare -f " $selected "
425+ fi
426+ fi
427+ }
0 commit comments