1
0
Fork 0
dotfiles/.config/zsh/cd_hooks/university
2026-01-06 07:36:53 +03:00

73 lines
1.7 KiB
Bash

local DIRNAME="$HOME/University/"
mode::enable() {
local mode_path="$DIRNAME/$1/.env"
export MODE__ACTIVE_MODE="$1"
export MODE__OLD_PS1="$PS1"
export PS1="($1) $PS1"
. "$mode_path"
}
mode::get_functions() {
perl -ne '/^(?>function\s+)?([\x21-\x7e]+)\s*\(\)/ && print "$1\n"' < "$1"
}
mode::get_variables() {
perl -ne '/^(?>declare\s+(?>--?\w+\s*)+\s*)?\s*([\x21-\x3c\x3e-\x7e]+)=.*$/ && print "$1\n"' < "$1"
}
mode::get_aliases() {
perl -ne '/^(?:noglob\s+)?alias ([\x21-\x3c\x3e-\x7e]+)=(.*)\n/ && print "$1=$2\n"' < "$1"
}
mode::disable() {
export PS1="$MODE__OLD_PS1"
local mode_path="$DIRNAME/$MODE__ACTIVE_MODE/.env"
unset -v "MODE__OLD_PS1"
unset -v "MODE__ACTIVE_MODE"
for mode_variable in ${(@f)"$(mode::get_variables "$mode_path")"}; do
unset "$mode_variable"
done
for mode_function in ${(@f)"$(mode::get_functions "$mode_path")"}; do
unset -f "$mode_function"
done
for mode_alias in ${(@f)"$(mode::get_aliases "$mode_path")"}; do
unalias "${mode_alias%=*}"
done
}
cd::enter() {
[ -n "$MODE__ACTIVE_MODE" ] && mode::disable
local module_name="$(cut -d/ -f1 <<<"${PWD#"$DIRNAME"}" )"
[ ! -f "$DIRNAME/$module_name/.env" ] && return
mode::enable "$module_name"
}
cd::leave() {
[ -n "$MODE__ACTIVE_MODE" ] && mode::disable
}
m() {
[ -z "$MODE__ACTIVE_MODE" ] && return 1
local mode_path="$DIRNAME/$MODE__ACTIVE_MODE/.env"
for type in Aliases Functions Variables; do
local arr=(${(@f)"$(mode::get_"${type:l}" "$mode_path")"})
[ "${#arr}" -gt 0 ] && {
echo "$type:"
printf '%s\n' "${arr[@]}"
echo
}
done | head -n -1
}
# vim: ft=zsh