# shellcheck shell=sh # shellcheck disable=SC2001 disable=SC2034 disable=SC2046 disable=2164 JUMP_LIST=~/.config/z/jump_list _z_exec() { sqlite3 "$JUMP_LIST" "$@" return $? } _z_completions() { complete $(_z_exec "select alias from jump_list order by alias") } function completion/z { array OPTIONS \ "h:; show help text" \ "l:; list aliases" \ "a:; add a new alias" \ "r:; remove alias" command -f completion//parseoptions -n argnum="${WORDS[#]}" if [ "$ARGOPT" = "-" ] && [ "$argnum" -eq 1 ]; then command -f completion//completeoptions elif [ -z "$ARGOPT" ] && [ "$argnum" -eq 1 ]; then _z_completions elif [ "$ARGOPT" = "r" ] && [ "$argnum" -eq 2 ]; then _z_completions elif [ "${WORDS[2]}" = "-a" ] && [ "$argnum" -eq 3 ]; then complete -T -S '/' -d fi } ! [ -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=$(realpath "$dir") 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 }