dotfiles/fish/include/parent.fish

52 lines
1.6 KiB
Fish
Raw Normal View History

#!/usr/bin/env fish
# parent_script prints the parent script's filename
function parent_script
set -f trace (status stack-trace)
# By index:
# 1: in command substitution
# 2: parent.fish line
# 3: in function 'parent'
# 4: caller import line
# 5: in command substitution
# 6: caller "parent" invokation line
# 7: in function calling parent
# 8: jackpot! here's the parent of the caller
set -f parent_line $trace[8]
set -f parent_split (string split 'of file' $parent_line)
set -f parent (string trim $parent_split[2])
# Clean up the parent a lil bit
set -f parent (realpath (string replace '~' "$HOME" $parent))
set -f parent_parts (string split '/' $parent)
set -f parent (string join '/' $parent_parts[-1])
# remove .fish suffix
set -f parent (string replace '.fish' '' $parent)
echo $parent
end
function parent_script_dir
set -f trace (status stack-trace)
# By index:
# 1: in command substitution
# 2: parent.fish line
# 3: in function 'parent'
# 4: caller import line
# 5: in command substitution
# 6: caller "parent" invokation line
# 7: in function calling parent
# 8: jackpot! here's the parent of the caller
set -f parent_line $trace[8]
set -f parent_split (string split 'of file' $parent_line)
set -f parent (string trim $parent_split[2])
# qualify ~
set -f parent (string replace '~' "$HOME" $parent)
# Qualify the path to absolute and remove the filename
set -f parent_dir (realpath $parent)
set -f parent (string join '/' (string split '/' $parent_dir)[1..-2])
echo $parent
end