Created
March 21, 2019 10:12
-
-
Save c02y/b2a9f78fc76106487ec5809b4c8e15cc to your computer and use it in GitHub Desktop.
tweak fu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fu -d 'fu command and prompt to ask to open it or not' | |
# $argv could be builtin keyword, function, alias, file(bin/script) in $PATH, abbr | |
# And they all could be defined in script or temporally (could be found in any file) | |
set found 0 | |
# Check `type` output, NOTE: `type` doesn't support abbr | |
if type $argv ^/dev/null # omit the result once error(abbr or not-a-thing) returned, $status = 0 | |
set found 1 # for not-a-thing | |
set result (type $argv) | |
end | |
# NOTE: $argv may also be defined as an abbr like rm command | |
abbr --show | grep "abbr -a -U -- $argv " # Space to avoid the extra abbr starting with $ARGV | |
if test $status = 0 | |
# in case $argv existes in both `type` and `abbr --show` | |
# function may be `function func` and `function func -d ...` | |
if test $found = 1 -a (echo (grep -w -E "^alias $argv |^function $argv |^function $argv\$" $FISH_CONFIG_PATH)) | |
echo "$argv is in both `type` and `abbr --list`, found definition in $FISH_CONFIG_PATH" | |
abbrc | |
# continue, use the result of `type` | |
else # only exists in `abbr --show` | |
set found 1 | |
set result (abbr --show | grep "abbr -a -U -- $argv ") | |
end | |
else if test $status != 0 -a $found != 1 | |
echo "$argv is not a thing!" | |
return | |
end | |
set result_1 (printf '%s\n' $result | head -1) | |
if test (echo $result_1 | grep -E "abbr -a -U -- $argv |is a function with definition") # defined in fish script | |
if test (echo $result_1 | grep -E "is a function with definition") | |
# 1. function or alias -- second line of output of fu ends with "$path @ line $num_line" | |
set -l result_2 (printf '%s\n' $result | sed -n "2p") | |
set def_file (echo $result_2 | awk -v x=4 '{print $x}') | |
if test "$def_file" = "-" # alias, no definition file is printed | |
set def_file $FISH_CONFIG_PATH | |
end | |
set num_line (grep -n -w -E "^alias $argv |^function $argv |^function $argv\$" $def_file | cut -d: -f1) | |
echo $num_line | |
if not test $num_line # empty | |
echo "$argv is an alias/functions in `alias/functions` but not defined in $def_file, may be defined temporally or in other file!" | |
if test $def_file = $FISH_CONFIG_PATH | |
functions -e $argv | |
echo "$argv is erased!" | |
end | |
return | |
end | |
else # 2. abbr | |
set def_file $FISH_CONFIG_PATH | |
set num_line (grep -n "^abbr $argv " $def_file | cut -d: -f1) | |
if not test $num_line # empty | |
echo "$argv is an abbr in `abbr --show` but not defined in $FISH_CONFIG_PATH, may be defined temporally or in other file!" | |
if test $def_file = $FISH_CONFIG_PATH | |
abbr -e $argv | |
echo "$argv is erased!" | |
end | |
return | |
end | |
end | |
echo | |
read -n 1 -p 'echo "Open the file containing the definition? [y/N]: "' -l answer | |
if test "$answer" = "y" -o "$answer" = " " | |
vim $def_file +$num_line | |
end | |
else if test (echo $result_1 | grep -i "is a builtin") | |
# 3. $argv in builtin like if | |
return | |
else # 4. $argv is a file in $PATH | |
set -l file_path (echo $result_1 | awk 'NF>1{print $NF}') | |
file $file_path | grep "symbolic link" # print only $argv is symbolic link | |
file (readlink -f $file_path) | grep -E "ELF|script|executable" # highlight | |
if test (file (readlink -f $file_path) | grep "script") # script can be open | |
echo | |
read -n 1 -p 'echo "Open the file for editing?[y/N]: "' -l answer | |
if test "$answer" = "y" -o "$answer" = " " | |
vim $file_path | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment