Skip to content

Commit 6c06ae0

Browse files
A Dynamic Collection of Shell Scripts with Educational Purpose
1 parent 60422d7 commit 6c06ae0

4 files changed

Lines changed: 173 additions & 4 deletions

File tree

aliases/functions.fish

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
alias cm='commands'
1+
alias cm='commands'
2+
alias functions_select='fsel'

aliases/functions.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
alias cm='commands'
1+
alias cm='commands'
2+
alias functions_select='fsel'

functions/functions_browser.fish

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License: GPLv3
33
# Credits: Felipe Facundes
44

5-
function fzf_functions_browser
5+
function functions_browser
66

77
function __cleanup
88
printf '\e[?7h\e[?25h\e[2J\e[;r\e[?1049l'
@@ -230,4 +230,77 @@ function fzf_functions_browser
230230
end
231231

232232
__cleanup
233+
end
234+
235+
##################################################################################################################################
236+
##################################################################################################################################
237+
##################################################################################################################################
238+
239+
# Function selector for Fish shell
240+
# Usage: fsel [-e|--exec] [-h|--help]
241+
242+
function fsel
243+
set -l exec_mode false
244+
245+
switch "$argv[1]"
246+
case -h --help
247+
echo "Usage: fsel [-e|--exec] [-h|--help]"
248+
echo ""
249+
echo "Select and show/execute shell functions interactively."
250+
echo ""
251+
echo "Options:"
252+
echo " -e, --exec Execute selected function"
253+
echo " -h, --help Show this help"
254+
echo ""
255+
echo "Examples:"
256+
echo " fsel Select and show function code"
257+
echo " fsel -e Select and execute function"
258+
return 0
259+
case -e --exec
260+
set exec_mode true
261+
end
262+
263+
# Check if fzf is installed
264+
if not command -v fzf &>/dev/null
265+
echo "fzf is not installed. Install it with:" >&2
266+
echo " apt install fzf # Debian/Ubuntu" >&2
267+
echo " brew install fzf # macOS" >&2
268+
return 1
269+
end
270+
271+
# Create temp file with function definitions
272+
set -l tmpfile (mktemp /tmp/fsel_functions_XXXXXX)
273+
274+
# Get all function names and dump definitions
275+
set -l funcs (functions --all | string match -r '^[^_,].*' | sort)
276+
277+
for func in $funcs
278+
echo "###FUNC:$func"
279+
functions "$func"
280+
echo "###END:$func"
281+
end | tee "$tmpfile" >/dev/null
282+
283+
# Extract function names for fzf (one per line)
284+
set -l func_names (grep -a "^###FUNC:" "$tmpfile" | sed 's/^###FUNC://')
285+
286+
# Build preview command with expanded tmpfile path
287+
set -l preview_cmd "sed -n '/^###FUNC:{}\$/,/^###END:{}\$/p' $tmpfile | sed '1d;\$d'"
288+
289+
# Select with fzf using the temp file for preview
290+
set -l selected (printf "%s\n" $func_names | fzf \
291+
--preview="$preview_cmd" \
292+
--preview-window=right:60%:wrap)
293+
294+
rm -f "$tmpfile"
295+
296+
if test -z "$selected"
297+
return 1
298+
end
299+
300+
# Show or execute
301+
if $exec_mode
302+
eval "$selected"
303+
else
304+
functions "$selected"
305+
end
233306
end

functions/functions_browser.sh

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ functions_browser() {
268268
# EOF
269269
# }
270270

271+
272+
##################################################################################################################################
273+
##################################################################################################################################
274+
##################################################################################################################################
275+
276+
271277
if [[ -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

Comments
 (0)