ambevar-dotfiles/.scripts/zshcd

44 lines
957 B
Bash
Executable File

#!/bin/sh
## Start zsh in the directory specified as first argument.
if [ ! -d "$1" ]; then
zsh
exit
fi
## Default files.
ZDIRS="$HOME/.cache/zsh/dirs"
## Fetch file location dynamically.
if [ ! -f ~/.zshrc ]; then
echo '~/.zshrc not found. Exiting.'
exit
fi
if ! command -v realpath; then
echo "'realpath' not found. Exiting."
exit
fi
SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)"
[ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")"
if [ ! -d "$SHELL_DIR" ]; then
echo 'SHELL_DIR not found. Could not search for DIRSTACKFILE. Exiting.'
exit
fi
ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)"
[ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")"
if [ ! -f "$ZDIRS" ]; then
echo 'DIRSTACKFILE not found. Exiting.'
exit
fi
## Prepend argument to zsh dirstack.
BUF="$(realpath "$1")
$(grep -v "$(realpath "$1")" "$ZDIRS")"
echo "$BUF" > "$ZDIRS"
zsh