ambevar-dotfiles/.scripts/zshcd

47 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
## Start zsh in the directory specified as first argument. Useful for file
## browsers to start shell in pointed folder.
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 1
fi
## We need realpath to prevent us from adding duplicates to the zsh
## DIRSTACKFILE.
if ! command -v realpath >/dev/null 2>&1; then
echo "'realpath' not found. Exiting."
exit 1
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 1
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 1
fi
## Prepend argument to zsh dirstack.
BUF="$(realpath "$1")
$(grep -v "$(realpath "$1")" "$ZDIRS")"
echo "$BUF" > "$ZDIRS"
zsh