Skip to content

Instantly share code, notes, and snippets.

@100ideas
Last active October 16, 2018 21:52
Show Gist options
  • Save 100ideas/4dc9712acd4d7e773ba91097b55204c2 to your computer and use it in GitHub Desktop.
Save 100ideas/4dc9712acd4d7e773ba91097b55204c2 to your computer and use it in GitHub Desktop.
fish shell script to print list of user-defined Fish functions (including any executable files in first path of $fish_user_paths)
# author @100ideas
# latest revision 2018-10-16
function lsfuncs --description 'Print user-defined Fish functions (including any executable files in first path of $fish_user_paths).'
# NOT CURRENTLY USED
# eventually would be handy to return info on single command glob if provided via fish_opt & argparse
# i.e. `apropos` mode
# http://fishshell.com/docs/current/commands.html#fish_opt
set -l options (fish_opt -s a -l all)
argparse $options -- $argv
if test -n "$_flag_a"
# use fish built-in 'functions' command to get list of *.fish user scripts stored in conventional places
for fishfunc in (functions -a)
# and then again to get description string set by '--description' flag in script source
# i.e. 'function lsfuncs --description '<THIS STRING>'
set_color yellow
printf '%-24s - %.80s\n' "$fishfunc" (set_color normal; functions --details -v $fishfunc | tail -n 1)
end
else
# I also store some scripts in a separate directory (repo) that's always firstmost in my PATH, so do the
# same thing for any *.fish scripts over there
set_color brblue
printf "\n$fish_function_path[1]:\n"
for fishfunc in (ls $fish_function_path[1]/*.fish | sed -E "s#.*fish\/functions\/(.*)\.fish#\1#")
set_color yellow
printf '%-24s - %.80s\n' "$fishfunc" (set_color normal; functions --details -v $fishfunc | tail -n 1)
end
# I keep general-purpose scripts & tools in ~/.config/bin (symlinked to ~/dev/bin; with all of ~/.config kept in version control). This path is always the first entry in my $fish_user_paths or bash $PATH
set_color brblue
printf "\n$fish_user_paths[1]:\n"
# loop through all paths that are executable and are not directories
for userfunc in (find $fish_user_paths[1]/* -d 0 -perm +755 -not -type d)
# is it a script or is it a binary?
set is_text (file -Ib $userfunc | grep '^.*text\/')
set_color yellow
if test -n "$is_text" # it's a script
printf '%-24s - %.80s\n' (echo $userfunc | sed -E "s#.*bin\/(.*)#\1#") \
# print first comment line (assumes comments start with with '#')
(set_color normal; sed -E '/^#!/d; s/^\s*# (.*)$/\1/p; s/_*description[_ =]*//p; d' $userfunc | head -n 1)
else # it's binary
printf '%-24s - %.80s\n' (echo $userfunc | sed -E "s#.*bin\/(.*)#\1#") \
# try to exec binary with '-h' help flag then return first line
(set_color normal; sh -c "$userfunc -h" | awk 'length($0) > 30' | head -n 1 | string trim --left)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment