added yash alias jump script

This commit is contained in:
Rudis Muiznieks 2025-03-18 17:45:37 -05:00
parent b1aa6d93b2
commit d7eabb19a8
Signed by: rudism
GPG key ID: CABF2F86EF7884F9
2 changed files with 89 additions and 1 deletions

View file

@ -13,8 +13,9 @@ HISTSIZE=100000
PS1P="\fi"
FCEDIT=hx
# prompt
# prompt and z
. ~/skynet/yash/polyglot.sh
. ~/skynet/yash/z.sh
# environment setup
export PURE_GIT_PULL=0

87
yash/z.sh Normal file
View file

@ -0,0 +1,87 @@
JUMP_LIST=~/.config/z/jump_list
_z_exec() {
sqlite3 "$JUMP_LIST" "$@"
return $?
}
! [ -d ~/.config/z ] && mkdir -p ~/.config/z
! [ -f "$JUMP_LIST" ] && _z_exec 'create table jump_list (alias text primary key, directory text not null)'
_z_help() {
echo 'help: z -h'
echo 'goto: z alias'
echo 'list: z -l'
echo 'add: z -a alias /path/to/dir'
echo 'remove: z -r alias'
}
_z_list() {
_z_exec "select alias || ' -> ' || directory from jump_list order by alias"
}
_z_add() {
alias=$1
dir=$2
if [ -z "$dir" ]; then
dir=$(pwd)
fi
if [ ! -d "$dir" ]; then
echo "Directory not found: $dir"
return 1
fi
fullpath=$(cd "$dir"; pwd)
if [ ! -d "$fullpath" ]; then
echo "Could not change to directory: $fullpath"
return 1
else
alias_esc=$(echo "$alias" | sed "s/'/''/g")
fullpath_esc=$(echo "$fullpath" | sed "s/'/''/g")
if ! _z_exec "insert into jump_list values ('$alias_esc', '$fullpath_esc')"; then
echo "Error adding alias"
return 1
fi
fi
}
_z_remove() {
alias=$1
alias_esc=$(echo "$alias" | sed "s/'/''/g")
rows=$(_z_exec "delete from jump_list where alias='$alias_esc'; select changes()")
if [ "$rows" -eq 0 ]; then
echo "Alias not found: $alias"
return 1
fi
}
_z_goto() {
alias=$1
alias_esc=$(echo "$alias" | sed "s/'/''/g")
dir=$(_z_exec "select directory from jump_list where alias='$alias_esc'")
if [ -z "$dir" ]; then
echo "Alias not found: $alias"
return 1
elif [ ! -d "$dir" ]; then
echo "Directory not found: $dir"
return 1
else
cd "$dir"
fi
}
z() {
if [ $# -eq 0 ]; then
_z_help
return
fi
cmd=$1
shift
case $cmd in
'-h') _z_help;;
'-l') _z_list;;
'-a') _z_add "$@";;
'-r') _z_remove "$@";;
-*) echo "Unknown command: $cmd"; return 1;;
*) _z_goto "$cmd";;
esac
}